Skip to content

xanpena/svg-chart-builder

Repository files navigation

SVGChartBuilder

SVGChartBuilder is a PHP library that allows you to generate SVG-based charts for use in your backend applications.

Requirements

PHP 8.0 or later

Installation

You can install SVGChartBuilder via Composer:

To install using Composer, run the following command in your terminal:

    composer require xanpena/svg-chart-builder

Usage

SVGChartBuilder provides several types of charts that you can create:

Example

To create a bar chart, use the following code:

  • Tipo Bar chart
use Xanpena\SVGChartBuilder\SVGChartBuilder;

$data = [
    16,
    18,
    40,
    // ... other data ...
];

$options = [
    'labels' => [
        'math',
        'literature',
        'english',
        // ... other data ...
    ],
    'colors' => [
        '#CDDC39',
        '#00BCD4',
        '#9E9E9E',
        // ... other data ...
    ],
    'axisColors' => [
        'x' => 'red',
        'y' => 'blue'
    ],
    'labelsColor' => 'orange',
    'dataColor' => 'white',
];

$chartBuilder = new SVGChartBuilder(SVGChartBuilder::CHART_TYPE_BAR, $data, $options);
$svg = $chartBuilder->create();
echo $svg;
  • Tipo Horizontal bar chart
use Xanpena\SVGChartBuilder\SVGChartBuilder;

$data = [
    16,
    18,
    40,
    // ... other data ...
];

$options = [
    'labels' => [
        'math',
        'literature',
        'english',
        // ... other data ...
    ],
    'colors' => [
        '#CDDC39',
        '#00BCD4',
        '#9E9E9E',
        // ... other data ...
    ],
    'axisColors' => [
        'x' => 'red',
        'y' => 'blue'
    ],
    'labelsColor' => 'orange',
    'dataColor' => 'white',
];

$chartBuilder = new SVGChartBuilder(SVGChartBuilder::CHART_TYPE_HORIZONTALBAR, $data, $options);
$svg = $chartBuilder->create();
echo $svg;
  • Tipo Doughnut chart
use Xanpena\SVGChartBuilder\SVGChartBuilder;

$data = [
    16,
    18,
    40,
    // ... other data ...
];

$options = [
    'labels' => [
        'math',
        'literature',
        'english',
        // ... other data ...
    ],
    'colors' => [
        '#CDDC39',
        '#00BCD4',
        '#9E9E9E',
        // ... other data ...
    ],
    'labelsColor' => 'white'
];

$chartBuilder = new SVGChartBuilder(SVGChartBuilder::CHART_TYPE_DOUGHNUT, $data, $options);
$svg = $chartBuilder->create();
echo $svg;
  • Tipo Pie chart
use Xanpena\SVGChartBuilder\SVGChartBuilder;

$data = [
    16,
    18,
    40,
    // ... other data ...
];

$options = [
    'labels' => [
        'math',
        'literature',
        'english',
        // ... other data ...
    ],
    'colors' => [
        '#CDDC39',
        '#00BCD4',
        '#9E9E9E',
        // ... other data ...
    ],
    'labelsColor' => 'white'
];

$chartBuilder = new SVGChartBuilder(SVGChartBuilder::CHART_TYPE_PIE, $data, $options);
$svg = $chartBuilder->create();
echo $svg;
  • Tipo Line chart
use Xanpena\SVGChartBuilder\SVGChartBuilder;

$data = [
    'math' => [
        11,
        17,
        15,
        // ... other data ...
    ],
    'literature' => [
        21,
        21,
        23,
        // ... other data ...
    ],
    'english' => [
        14,
        9,
        18,
        // ... other data ...
    ]
    // ... other data ...
];

$options = [
    'labels' => [
        '2020/2021',
        '2021/2022',
        '2023/2024',
        // ... other data ...
    ],
    'colors' => [
        '#CDDC39',
        '#00BCD4',
        '#9E9E9E',
        // ... other data ...
    ],
    'axisColors' => [
        'x' => 'red',
        'y' => 'blue'
    ],
    'labelsColor' => 'orange',
];

$chartBuilder = new SVGChartBuilder(SVGChartBuilder::CHART_TYPE_LINE, $data, $options);
$svg = $chartBuilder->create();
echo $svg;

Chart Types

SVGChartBuilder supports the following chart types:

SVGChartBuilder::BAR_CHART: Bar chart
SVGChartBuilder::HORIZONTALBAR_CHART: Horizontal bar chart
SVGChartBuilder::DOUGHNUT_CHART: Doughnut chart
SVGChartBuilder::PIE_CHART: Pie chart
SVGChartBuilder::CHART_TYPE_LINE: Line chart

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please using the issue tracker.

Credits