如何在Chart.js Laravel中将起始值设置为“ 0”

时间:2019-05-08 10:53:28

标签: javascript php laravel chart.js

我在Laravel项目中使用Chart.js,但我不知道如何将图表设置为零。

$chartjs = app()->chartjs
    ->name('lineChartTest')
    ->type('bar')
    ->size(['width' => 400, 'height' => 200])
    ->labels(['02', '03', '04', '05', '06', '07', '08', '09', '10', '11'])
    ->datasets([
        [
            'label' => 'My Second Dataset',
            'backgroundColor' => 'rgba(138, 22, 54, 0.31)',
            'borderColor' => 'rgba(138, 125, 154, 0.7)',
            'pointBorderColor' => 'rgba(38, 285, 14, 0.7)',
            'pointBackgroundColor' => 'rgba(38, 185, 154, 0.7)',
            'pointHoverBackgroundColor' => '#fff',
            'pointHoverBorderColor' => 'rgba(120,20,220,1)',
            'data' => [
                $articles['02']->count(), $articles['03']->count(), $articles['04']->count(), $articles['05']->count(),
                $articles['06']->count(), $articles['07']->count(), $articles['08']->count(), $articles['09']->count(),
                $articles['10']->count(), $articles['11']->count()
            ],
            'borderWidth' => 3,
        ]

    ])
    ->options([]);

2 个答案:

答案 0 :(得分:2)

您必须设置beginAtZero选项:

$chartOptions = [
    'scales' => [
        'yAxes' => [
            [
                'ticks' => [
                    'beginAtZero' => true,
                ],
            ],
        ],
    ],
];

->options($chartOptions);

答案 1 :(得分:0)

如果您希望y轴从零开始,则可以将其包含在选项中

options: {
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero: true
            }
        }]
    }
}