xanpena/svg-chart-builder

PHP SVG Chart Builder

1.3 2023-10-18 06:20 UTC

This package is auto-updated.

Last update: 2024-05-18 07:37:57 UTC


README

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