Derafu: Chart - PHP Chart Library

GitHub last commit CI Workflow GitHub code size in bytes GitHub Issues Total Downloads Monthly Downloads

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.

Area Chart

2. Bar Chart

Shows discrete data comparisons using vertical bars. Perfect for categorical comparisons.

Bar Chart

3. Bubble Chart

Highlights relationships and distributions. Bubble size represents a third dimension.

Bubble Chart

4. Horizontal Bar Chart

Provides an alternative for displaying bar data, especially useful when labels are long.

Horizontal Bar Chart

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.

Pie Chart

6. Radar Chart

Compares multiple variables across multiple dimensions. Ideal for skill or performance analysis.

Radar Chart

7. Scatter Plot

Shows relationships between two variables using points. Commonly used for distribution analysis.

Scatter Plot

8. Waterfall Chart

Visualizes cumulative changes in value over a sequence. Useful for profit and loss analysis.

Waterfall Chart

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!