diagro/lib_php_chart

Library for creating ChartJS in PHP.

1.3.2 2022-07-27 21:23 UTC

This package is auto-updated.

Last update: 2024-06-09 17:25:59 UTC


README

68747470733a2f2f64696167726f2e62652f6173736574732f696d672f64696167726f2d6c6f676f2e737667

PHP chart library Diagro library PHP chartjs

Description

Create charts with ChartJS through PHP. You make the chart in PHP code and serialize it to Javascript. In Javascript you create the chart by deserialize the output of the PHP Chart class.

The Manager class holds the graphs and is responsible for rendering.

$("canvas[id^='chart_']").each(function(idx, el) {
	new Chart(
		$(el).attr('id'), 
		$.parseJSON(atob($(el).data('chart')))
	);
});

If you use Laravel. Simply register the Manager class in the AppServiceProvider.

public function register()
{
	$this->app->singleton('charts', \Diagro\Chart\Manager::class);
}

In a blade view:

{{ app('charts')->render('chart_id') }}

Or register a stringable blade like:

Blade::stringable(\Diagro\Chart\Chart::class, function($chart) {
    return app('charts')->render($chart->id);
});

Example:

$chart = new Chart('messages_per_time', Chart::TYPE_BAR);
$chart->labels = array_keys($per_datetime);
$chart->datasets[0] = new \Diagro\Chart\Datasets\Bar('Aantal per tijdstip');
$chart->datasets[0]->data = array_values($per_datetime);
$chart->datasets[0]->background_color = [
	new \Diagro\Chart\Color\RGBA(255,99,132,0.2),
	new \Diagro\Chart\Color\RGBA(54,162,235,0.2)
];
$chart->datasets[0]->border_color = [
	new \Diagro\Chart\Color\RGBA(255,99,132,1),
	new \Diagro\Chart\Color\RGBA(54,162,235,1)
];
$chart->datasets[0]->border_width = 1;

$chart->options->plugins->legend->display = false;