PHP code example of bbsnly / chartjs-php

1. Go to this page and download the library: Download bbsnly/chartjs-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bbsnly / chartjs-php example snippets


use Bbsnly\ChartJs\Chart;
use Bbsnly\ChartJs\Config\Data;
use Bbsnly\ChartJs\Config\Dataset;
use Bbsnly\ChartJs\Config\Options;

$chart = new Chart;
$chart->type = 'line';

$data = new Data();
$data->labels = ['Red', 'Green', 'Blue'];

$dataset = new Dataset();
$dataset->data = [5, 10, 20];
$data->datasets[] = $dataset;

$chart->data($data);

$options = new Options();
$options->responsive = true;
$chart->options($options);

$chart->get(); // Chart config as a PHP array
$chart->toJson(); // Chart config as JSON — use this when feeding Chart.js
$chart->toHtml('my_chart'); // HTML and JavaScript that render the chart

$chart->beginAtZero(); // options.scales.y.beginAtZero = true

use Bbsnly\ChartJs\Config\JsExpression;

$options = new Options();
$options->plugins = [
    'tooltip' => [
        'callbacks' => [
            'label' => new JsExpression('function (context) { return " $" + context.parsed.y; }'),
        ],
    ],
];
shell
composer