vichaunter/chartjs

将原始数据解析为ChartJS的PHP包

0.0.1 2018-02-21 17:51 UTC

This package is not auto-updated.

Last update: 2024-10-02 20:54:25 UTC


README

使用示例

首先,需要从cdn加载js文件,你可以这样获取

        $chartJS = new ChartFactory();
        $jsUrl = $chartJS->getJsFileUrl();

在页面末尾加载,在其他图表脚本之前

<script src="{$jsUrl}"></script>

注意:如果你不需要在模板中从其他文件加载,可以从任何新的XXXChart()中获取getJsFileUrl。

然后,你会生成图表

        $statsByDate = Customers::getDataList(); //in this case return [total,day]
        
        $barChart = new BarChart();
        $barChartData = $barChart->getData();
//        $barChartOptions = $barChart->getOptions(); // you can configure colors, sizes, etc.
    
        $barChartDataset = $barChartData->newDataset();
        foreach($statsByDay as $day){
            $barChartData->addLabel($day['date']);
            $barChartDataset->setLabel('Label for legend for this dataset color')->addData($day['total']);
        }
        
        $barChartHtmlCanvas = $barChart->getHtmlCanvas();
        $barChartHtmlScript = $barChart->getHtmlScript();

然后在body中添加canvas,其中图表应该出现,并且在页面中包含之前的$chartJS脚本之后。