Derafu: Chart - PHP Chart Library
Derafu Chart is a PHP library for creating diverse charts. The library is designed to generate charts entirely in PHP using GD or other renderers. It supports a variety of chart types and is easy to extend.
Features
- π Multiple chart types: Bar, Line, Pie, Radar, Waterfall, and more.
- βοΈ Fully customizable options.
- π¦ Lightweight and easy to integrate.
- πΌοΈ Output in PNG and flexible rendering options.
- π‘ Extensible via custom renderers.
- 𧩠Encodable to JSON for interoperability.
- π·οΈ Open-source under the MIT license.
Installation
You can install the library using Composer:
composer require derafu/chart
Usage
You can choose the style that best suits your coding preferences or project requirements:
- Creating a Chart with the Factory.
- Creating a Chart with Method Chaining.
Both approaches allow flexibility depending on whether you prefer array-based configuration or object-oriented method chaining.
Creating a Chart with the Factory
Hereβs how to create a simple bar chart using the ChartFactory
:
use Derafu\Chart\ChartFactory;
use Derafu\Chart\Enum\ChartType;
$chart = (new ChartFactory())->renderFromArray([
'type' => ChartType::BAR,
'title' => 'Ventas',
'label_x' => 'Mes',
'label_y' => 'Ventas',
'datasets' => [
[
'points' => [
'Enero' => 10000,
'Febrero' => 12000,
'Marzo' => 1200,
'Abril' => 1350,
'Mayo' => 5000,
'Junio' => 5000,
'Julio' => 5000,
'Agosto' => 5000,
'Septiembre' => 15000,
],
],
],
]);
file_put_contents('chart.png', $chart);
Creating a Chart with Method Chaining
Hereβs how to create the same bar chart using method chaining:
use Derafu\Chart\Chart;
use Derafu\Chart\Dataset;
use Derafu\Chart\Enum\ChartType;
$chart = (new Chart(ChartType::BAR))
->setTitle('Ventas')
->setLabelX('Mes')
->setLabelY('Ventas')
->addDataset(
(new Dataset())
->setLabel('2024')
->setColor('blue')
->addPoints([
'Enero' => 10000,
'Febrero' => 12000,
'Marzo' => 1200,
'Abril' => 1350,
'Mayo' => 5000,
'Junio' => 5000,
'Julio' => 5000,
'Agosto' => 5000,
'Septiembre' => 15000,
])
);
file_put_contents('chart.png', $chart->render());
Chart Types
Here is an overview of the chart types supported, with examples.
1. Area Chart
Displays continuous data trends over time or categories. Ideal for showing volume and trends.
2. Bar Chart
Shows discrete data comparisons using vertical bars. Perfect for categorical comparisons.
3. Bubble Chart
Highlights relationships and distributions. Bubble size represents a third dimension.
4. Horizontal Bar Chart
Provides an alternative for displaying bar data, especially useful when labels are long.
Text Output Example:
Chart name: horizontal_bar
Ventas
Enero β ββββββββββββββββββββββββββ 10.000
Febrero β βββββββββββββββββββββββββββββββ 12.000
Marzo β βββ 1.200
Abril β βββ 1.350
Mayo β βββββββββββββ 5.000
Junio β βββββββββββββ 5.000
Julio β βββββββββββββ 5.000
Agosto β βββββββββββββ 5.000
Septiembre β ββββββββββββββββββββββββββββββββββββββ 15.000
βββββββββββββββββββββββββββββββββββββββββββββββββββββ
0 4K 8K 12K 16K 20K
5. Pie Chart
Represents proportions in a dataset. Great for visualizing percentages.
6. Radar Chart
Compares multiple variables across multiple dimensions. Ideal for skill or performance analysis.
7. Scatter Plot
Shows relationships between two variables using points. Commonly used for distribution analysis.
8. Waterfall Chart
Visualizes cumulative changes in value over a sequence. Useful for profit and loss analysis.
Roadmap
- π Export charts to JSON (e.g., Chart.js, regression libraries).
- π Support trendlines and regression models.
- π Add SVG rendering support.
- π Add more internationalization options.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This package is open-sourced software licensed under the MIT license.
Happy charting!