### Manage Datasets for Derafu Charts (PHP) Source: https://context7.com/derafu/chart/llms.txt Illustrates how to manage datasets for charts, including creating standard datasets with labels, colors, and points, and adding them to a chart object. It also shows how to use a fluent API for adding points and retrieving all datasets from a chart. The example renders a bar chart with two datasets. ```php 'Sales 2024', 'color' => ColorPalette::SUCCESS, 'points' => [ 'Q1' => 25000, 'Q2' => 30000, 'Q3' => 28000, 'Q4' => 35000, ], ]); // Add to chart $chart->addDataset($dataset1); // Or use newDataset method for fluent API $dataset2 = $chart->newDataset([ 'label' => 'Sales 2023', 'color' => ColorPalette::INFO, ]); $dataset2->addPoint('Q1', 22000) ->addPoint('Q2', 25000) ->addPoint('Q3', 24000) ->addPoint('Q4', 30000); // Get all datasets $allDatasets = $chart->getDatasets(); echo "Total datasets: " . count($allDatasets); $pngData = $chart->render(); file_put_contents('datasets.png', $pngData); ``` -------------------------------- ### Manage Colors in Derafu Charts (PHP) Source: https://context7.com/derafu/chart/llms.txt Demonstrates how to manage colors using hex values or predefined palettes in the Derafu library. It covers creating custom colors, accessing RGB components, using built-in palettes, calculating negative colors, and applying custom colors to chart datasets. The example shows how to render a bar chart with a custom hex color. ```php toHex(); // Output: FF5733 echo $customColor; // Output: #FF5733 // Access RGB components [$r, $g, $b] = $customColor->toRGB(); echo "Red: {$r}, Green: {$g}, Blue: {$b}"; // Use predefined color palette $primaryColor = new Color(ColorPalette::PRIMARY); $dangerColor = new Color(ColorPalette::DANGER); // Get negative (complementary) color $negativeColor = $primaryColor->getNegative(); // Get color by index from color group $color1 = ColorPalette::getColorByIndex(0, ColorGroup::PROFESSIONAL); $color2 = ColorPalette::getColorByIndex(1, ColorGroup::PROFESSIONAL); $color3 = ColorPalette::getColorByIndex(2, ColorGroup::PROFESSIONAL); // Use in chart use Derafu\Chart\ChartFactory; use Derafu\Chart\Enum\ChartType; $factory = new ChartFactory(); $chart = $factory->renderFromArray([ 'type' => ChartType::BAR, 'title' => 'Custom Colors', 'datasets' => [ [ 'color' => '#FF5733', // Custom hex color 'points' => ['A' => 10, 'B' => 20], ], ], ]); file_put_contents('custom_colors.png', $chart); ``` -------------------------------- ### Create Horizontal Bar Chart using Fluent API - PHP Source: https://context7.com/derafu/chart/llms.txt This example utilizes the object-oriented fluent API of the Derafu Chart library to create a horizontal bar chart. It demonstrates instantiating the `Chart` class, setting properties like title and labels, and adding datasets with points using method chaining. The chart is then rendered to PNG and saved. Requires Derafu Chart and GD extension. ```php setTitle('Product Performance') ->setLabelX('Sales') ->setLabelY('Product'); $dataset = $chart->newDataset(['color' => ColorPalette::SUCCESS]); $dataset->addPoint('Product A', 45000); $dataset->addPoint('Product B', 52000); $dataset->addPoint('Product C', 38000); $dataset->addPoint('Product D', 61000); $pngData = $chart->render(); file_put_contents('products.png', $pngData); ``` -------------------------------- ### Chart Constructor and Fluent API Source: https://context7.com/derafu/chart/llms.txt Enables chart creation using an object-oriented fluent interface, offering more granular control and type safety compared to array-based configurations. This approach allows for step-by-step building and modification of chart properties and datasets. ```APIDOC ## Chart Constructor and Fluent API ### Description Creates a chart using an object-oriented fluent interface. This approach provides more control and type safety compared to array-based configuration. ### Method `new Chart(ChartType $type)` ### Parameters #### Constructor - **type** (Enum\ChartType) - Required - The type of chart to create (e.g., ChartType::HORIZONTAL_BAR). #### Fluent Methods - **setTitle(string $title)**: Sets the chart title. - **setLabelX(string $label)**: Sets the label for the X-axis. - **setLabelY(string $label)**: Sets the label for the Y-axis. - **newDataset(array $config = [])**: Creates and returns a new dataset object. Accepts an optional configuration array (e.g., `['color' => ColorPalette::SUCCESS]`). - **addPoint(string $label, float|int $value)**: Adds a data point to the dataset. - **render(string $format = 'png', int $quality = 90)**: Renders the chart to binary image data. Returns the binary string. ### Request Example ```php setTitle('Product Performance') ->setLabelX('Sales') ->setLabelY('Product'); $dataset = $chart->newDataset(['color' => ColorPalette::SUCCESS]); $dataset->addPoint('Product A', 45000); $dataset->addPoint('Product B', 52000); $dataset->addPoint('Product C', 38000); $dataset->addPoint('Product D', 61000); $pngData = $chart->render(); file_put_contents('products.png', $pngData); ?> ``` ### Response #### Success Response (Chart Object Rendering) - **Binary Image Data** (string) - The raw binary data of the generated chart image (e.g., PNG, JPEG) when the `render()` method is called. #### Response Example (The `render()` method returns binary image data. The example above shows how to save this data to a file.) ``` -------------------------------- ### Configure Chart Appearance with ChartOptions Source: https://context7.com/derafu/chart/llms.txt Demonstrates how to configure chart appearance using the ChartOptions class, including size, margins, colors, fonts, and image format. This is useful for customizing the visual output of charts. ```php renderFromArray([ 'type' => ChartType::BAR, 'title' => 'Quarterly Revenue', 'label_x' => 'Quarter', 'label_y' => 'Revenue ($)', 'datasets' => [ [ 'points' => [ 'Q1 2024' => 125000, 'Q2 2024' => 142000, 'Q3 2024' => 138000, 'Q4 2024' => 165000, ], ], ], 'options' => $options, ]); file_put_contents('quarterly_chart.png', $imageData); ?> ``` -------------------------------- ### Create Pie Chart for Proportional Data Source: https://context7.com/derafu/chart/llms.txt Illustrates how to create a pie chart to visualize proportional data, showing the relative size of different categories that make up a whole. This is best suited for a limited number of categories. ```php renderFromArray([ 'type' => ChartType::PIE, 'title' => 'Market Share', 'datasets' => [ [ 'points' => [ 'Product A' => 35, 'Product B' => 28, 'Product C' => 22, 'Product D' => 15, ], ], ], 'options' => new ChartOptions( size: new Size(500, 400), margin: new Margin(10, 10, 10, 10), labelsOnGridColor: ColorPalette::WHITE ), ]); file_put_contents('pie.png', $chart); ?> ``` -------------------------------- ### Create Bar Chart from Array Configuration - PHP Source: https://context7.com/derafu/chart/llms.txt This snippet demonstrates how to create a bar chart using the `ChartFactory::createFromArray()` method. It takes an array defining the chart type, title, labels, and datasets. The generated chart is then rendered to PNG binary data and saved to a file. Dependencies include the Derafu Chart library and the GD extension. ```php createFromArray([ 'type' => ChartType::BAR, 'title' => 'Monthly Sales', 'label_x' => 'Month', 'label_y' => 'Revenue', 'datasets' => [ [ 'label' => '2024 Sales', 'points' => [ 'January' => 10000, 'February' => 12000, 'March' => 15000, 'April' => 13500, 'May' => 18000, ], ], ], ]); // Render to PNG binary data $pngData = $chart->render(); file_put_contents('sales_chart.png', $pngData); ``` -------------------------------- ### Create Multi-Dataset Line Chart for Comparison Source: https://context7.com/derafu/chart/llms.txt Generates a line chart with multiple datasets, allowing for direct comparison between different data series. Each dataset can have a unique label and color. ```php createFromArray([ 'type' => ChartType::LINE, 'title' => 'Annual Sales Comparison', 'label_x' => 'Month', 'label_y' => 'Sales', 'datasets' => [ [ 'label' => '2024', 'color' => ColorPalette::PRIMARY, 'points' => [ 'Jan' => 10000, 'Feb' => 12000, 'Mar' => 11500, 'Apr' => 13500, 'May' => 15000, 'Jun' => 16000, ], ], [ 'label' => '2023', 'color' => ColorPalette::SECONDARY, 'points' => [ 'Jan' => 8000, 'Feb' => 9500, 'Mar' => 9000, 'Apr' => 10500, 'May' => 11000, 'Jun' => 12000, ], ], ], ]); $pngData = $chart->render(); file_put_contents('comparison.png', $pngData); ?> ``` -------------------------------- ### Render Line Chart Directly from Array Configuration - PHP Source: https://context7.com/derafu/chart/llms.txt This code shows how to directly render a line chart to image data using `ChartFactory::renderFromArray()`. This method is a shortcut that bypasses the creation of a chart object, suitable for immediate output. It takes an array configuration for chart properties and datasets, and outputs PNG data. Requires Derafu Chart and GD extension. ```php renderFromArray([ 'type' => ChartType::LINE, 'title' => 'Temperature Trends', 'label_x' => 'Day', 'label_y' => 'Temperature (°C)', 'datasets' => [ [ 'label' => 'City A', 'color' => ColorPalette::DANGER, 'points' => [ 'Mon' => 22, 'Tue' => 24, 'Wed' => 23, 'Thu' => 26, 'Fri' => 25, ], ], [ 'label' => 'City B', 'color' => ColorPalette::INFO, 'points' => [ 'Mon' => 18, 'Tue' => 19, 'Wed' => 20, 'Thu' => 22, 'Fri' => 21, ], ], ], ]); // Output directly to browser header('Content-Type: image/png'); echo $imageData; ``` -------------------------------- ### Generate Bubble Chart using PHP Source: https://context7.com/derafu/chart/llms.txt Generates a bubble chart to visualize project risk analysis. Data points include x-position (Timeline), y-value (Impact), and size (bubble radius). The chart is saved to a PNG file. Requires the Derafu Chart library. ```php renderFromArray([ 'type' => ChartType::BUBBLE, 'title' => 'Project Risk Analysis', 'label_x' => 'Timeline', 'label_y' => 'Impact', 'datasets' => [ [ 'label' => 'Project A', 'color' => ColorPalette::RICH_BLUE, 'points' => [ 'Phase 1' => [80, 120], // Impact=80, Size=120 'Phase 2' => [90, 50], // Impact=90, Size=50 'Phase 3' => [75, 200], // Impact=75, Size=200 ], ], [ 'label' => 'Project B', 'color' => ColorPalette::COPPER, 'points' => [ 'Phase 1' => [60, 80], 'Phase 2' => [70, 150], 'Phase 3' => [85, 100], ], ], ], ]); $pngData = $chart->render(); file_put_contents('bubble.png', $pngData); ``` -------------------------------- ### Generate Donut Chart using PHP Source: https://context7.com/derafu/chart/llms.txt Creates a donut chart with a specified title and datasets. It uses ChartFactory to render the chart from an array and outputs the image data directly to the browser. Dependencies include the Derafu Chart library. ```php renderFromArray([ 'type' => ChartType::DONUT, 'title' => 'Budget Allocation', 'datasets' => [ [ 'points' => [ 'Salaries' => 50000, 'Operations' => 25000, 'Marketing' => 15000, 'R&D' => 10000, ], ], ], 'options' => new ChartOptions( size: new Size(600, 400) ), ]); header('Content-Type: image/png'); echo $imageData; ``` -------------------------------- ### ChartFactory::renderFromArray() Source: https://context7.com/derafu/chart/llms.txt Directly renders a chart to binary image data without returning the chart object. This one-liner method combines chart creation and rendering, suitable for scenarios where immediate image output is needed without further object manipulation. ```APIDOC ## ChartFactory::renderFromArray() ### Description Directly renders a chart to binary image data without returning the chart object. This one-liner method combines chart creation and rendering for scenarios where you don't need to manipulate the chart object. ### Method `renderFromArray` ### Parameters #### Request Body - **type** (Enum\ChartType) - Required - The type of chart to create (e.g., ChartType::LINE). - **title** (string) - Optional - The title of the chart. - **label_x** (string) - Optional - The label for the X-axis. - **label_y** (string) - Optional - The label for the Y-axis. - **datasets** (array) - Required - An array of dataset configurations. - **label** (string) - Optional - The label for the dataset. - **color** (Enum\ColorPalette|string) - Optional - The color of the dataset. - **points** (array) - Required - An associative array of data points where keys are labels and values are numerical data. ### Request Example ```php renderFromArray([ 'type' => ChartType::LINE, 'title' => 'Temperature Trends', 'label_x' => 'Day', 'label_y' => 'Temperature (°C)', 'datasets' => [ [ 'label' => 'City A', 'color' => ColorPalette::DANGER, 'points' => [ 'Mon' => 22, 'Tue' => 24, 'Wed' => 23, 'Thu' => 26, 'Fri' => 25, ], ], [ 'label' => 'City B', 'color' => ColorPalette::INFO, 'points' => [ 'Mon' => 18, 'Tue' => 19, 'Wed' => 20, 'Thu' => 22, 'Fri' => 21, ], ], ], ]); // Output directly to browser header('Content-Type: image/png'); echo $imageData; ?> ``` ### Response #### Success Response (200 OK) - **Binary Image Data** (string) - The raw binary data of the generated chart image (e.g., PNG, JPEG). #### Response Example (The response is binary image data. The example above shows how to echo it directly after setting the appropriate Content-Type header.) ``` -------------------------------- ### Create Area Chart with PHP Source: https://context7.com/derafu/chart/llms.txt Renders an area chart, which displays data with filled regions below the lines, emphasizing volume and cumulative totals. The function accepts chart configuration parameters and outputs the chart as a PNG file. It supports multiple datasets. ```php renderFromArray([ 'type' => ChartType::AREA, 'title' => 'User Growth', 'label_x' => 'Month', 'label_y' => 'Active Users', 'datasets' => [ [ 'label' => '2024', 'color' => ColorPalette::PRIMARY, 'points' => [ 'Jan' => 1000, 'Feb' => 1200, 'Mar' => 1500, 'Apr' => 1800, 'May' => 2200, 'Jun' => 2800, ], ], [ 'label' => '2023', 'color' => ColorPalette::SECONDARY, 'points' => [ 'Jan' => 800, 'Feb' => 900, 'Mar' => 1100, 'Apr' => 1300, 'May' => 1600, 'Jun' => 2000, ], ], ], ]); file_put_contents('area.png', $chart->render()); ``` -------------------------------- ### Generate Radar Chart using PHP Source: https://context7.com/derafu/chart/llms.txt Constructs a radar chart for skills assessment, comparing Senior and Junior Developers across various technical skills. The resulting chart is saved as a PNG file and requires the Derafu Chart library. ```php renderFromArray([ 'type' => ChartType::RADAR, 'title' => 'Skills Assessment', 'datasets' => [ [ 'label' => 'Senior Developer', 'color' => ColorPalette::INFO, 'points' => [ 'Backend' => 90, 'Frontend' => 75, 'DevOps' => 85, 'Database' => 80, 'Architecture' => 85, 'Testing' => 70, ], ], [ 'label' => 'Junior Developer', 'color' => ColorPalette::DANGER, 'points' => [ 'Backend' => 60, 'Frontend' => 65, 'DevOps' => 40, 'Database' => 50, 'Architecture' => 30, 'Testing' => 45, ], ], ], ]); file_put_contents('radar.png', $chart->render()); ``` -------------------------------- ### Generate Stacked Bar Chart for Composition Source: https://context7.com/derafu/chart/llms.txt Creates a stacked bar chart where different datasets are stacked vertically within each bar. This visualization is effective for showing both the total value and the contribution of each part. ```php renderFromArray([ 'type' => ChartType::STACKED_BAR, 'title' => 'Revenue by Department', 'label_x' => 'Quarter', 'label_y' => 'Revenue ($)', 'datasets' => [ [ 'label' => 'Engineering', 'color' => ColorPalette::getColorByIndex(0), 'points' => [ 'Q1' => 50000, 'Q2' => 55000, 'Q3' => 60000, 'Q4' => 65000, ], ], [ 'label' => 'Sales', 'color' => ColorPalette::getColorByIndex(1), 'points' => [ 'Q1' => 75000, 'Q2' => 80000, 'Q3' => 78000, 'Q4' => 85000, ], ], [ 'label' => 'Marketing', 'color' => ColorPalette::getColorByIndex(2), 'points' => [ 'Q1' => 25000, 'Q2' => 30000, 'Q3' => 32000, 'Q4' => 35000, ], ], ], ]); file_put_contents('stacked.png', $chart); ?> ``` -------------------------------- ### ChartFactory::createFromArray() Source: https://context7.com/derafu/chart/llms.txt Creates a chart object from an array configuration. This method is ideal for quickly generating charts with minimal code by defining chart type, title, labels, datasets, and options within an associative array. ```APIDOC ## ChartFactory::createFromArray() ### Description Creates a chart object from an array configuration. This is the primary method for quickly generating charts with minimal code. It accepts chart type, title, labels, datasets, and options as array keys. ### Method `createFromArray` ### Parameters #### Request Body - **type** (Enum\ChartType) - Required - The type of chart to create (e.g., ChartType::BAR). - **title** (string) - Optional - The title of the chart. - **label_x** (string) - Optional - The label for the X-axis. - **label_y** (string) - Optional - The label for the Y-axis. - **datasets** (array) - Required - An array of dataset configurations. - **label** (string) - Optional - The label for the dataset. - **color** (Enum\ColorPalette|string) - Optional - The color of the dataset. - **points** (array) - Required - An associative array of data points where keys are labels and values are numerical data. ### Request Example ```php createFromArray([ 'type' => ChartType::BAR, 'title' => 'Monthly Sales', 'label_x' => 'Month', 'label_y' => 'Revenue', 'datasets' => [ [ 'label' => '2024 Sales', 'points' => [ 'January' => 10000, 'February' => 12000, 'March' => 15000, 'April' => 13500, 'May' => 18000, ], ], ], ]); // Render to PNG binary data $pngData = $chart->render(); file_put_contents('sales_chart.png', $pngData); ?> ``` ### Response #### Success Response (Chart Object) - **Chart Object** - An instance of the generated chart object that can be further manipulated or rendered. #### Response Example (The method returns a chart object, not a direct JSON response. The example above shows how to use the returned object.) ``` -------------------------------- ### Create Waterfall Chart with PHP Source: https://context7.com/derafu/chart/llms.txt Generates a waterfall chart to visualize the cumulative effect of sequential positive and negative values. It takes an array of chart configurations, including type, title, labels, and datasets with points. The output is saved as a PNG image. ```php renderFromArray([ 'type' => ChartType::WATERFALL, 'title' => 'Profit & Loss Analysis', 'label_x' => 'Category', 'label_y' => 'Amount ($)', 'datasets' => [ [ 'label' => 'Finance 2024', 'color' => ColorPalette::GREEN, 'points' => [ 'Starting Balance' => 50000, 'Revenue' => 30000, 'Operating Costs' => -20000, 'Taxes' => -15000, 'Investments' => -10000, 'Other Income' => 5000, 'Dividends' => -8000, ], ], ], 'options' => new ChartOptions( labelXAngle: 15 ), ]); file_put_contents('waterfall.png', $chart->render()); ``` -------------------------------- ### Generate Scatter Chart using PHP Source: https://context7.com/derafu/chart/llms.txt Creates a scatter plot to illustrate the relationship between Age and Monthly Income. Data is grouped by industry (Technology, Finance) and saved as a PNG file. This function depends on the Derafu Chart library. ```php renderFromArray([ 'type' => ChartType::SCATTER, 'title' => 'Age vs Income', 'label_x' => 'Age', 'label_y' => 'Monthly Income (USD)', 'datasets' => [ [ 'label' => 'Technology', 'color' => ColorPalette::INFO, 'points' => [ '25' => 3000, '30' => 5500, '35' => 7000, '40' => 9000, '45' => 12000, ], ], [ 'label' => 'Finance', 'color' => ColorPalette::SUCCESS, 'points' => [ '25' => 3500, '30' => 6000, '35' => 8500, '40' => 11000, '45' => 14000, ], ], ], ]); file_put_contents('scatter.png', $chart->render()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.