如何在Laravel中使用Chartjs和JSON数据显示图表

时间:2018-08-30 06:32:01

标签: javascript laravel chart.js

我已经从mysql数据创建了JSON,并带有以下查询和结果:

SELECT
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 1 THEN 1 ELSE 0 END ) ) AS `Januari`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 2 THEN 1 ELSE 0 END ) ) AS `Pebruari`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 3 THEN 1 ELSE 0 END ) ) AS `Maret`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 4 THEN 1 ELSE 0 END ) ) AS `April`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 5 THEN 1 ELSE 0 END ) ) AS `Mei`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 6 THEN 1 ELSE 0 END ) ) AS `Juni`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 7 THEN 1 ELSE 0 END ) ) AS `Juli`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 8 THEN 1 ELSE 0 END ) ) AS `Agustus`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 9 THEN 1 ELSE 0 END ) ) AS `September`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 10 THEN 1 ELSE 0 END ) ) AS `Oktober`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 11 THEN 1 ELSE 0 END ) ) AS `November`,
    sum( ( CASE MONTH ( `posts`.`created_at` ) WHEN 12 THEN 1 ELSE 0 END ) ) AS `Desember`,
    sum( ( CASE YEAR ( `posts`.`created_at` ) WHEN YEAR ( now( ) ) THEN 1 ELSE 0 END ) ) AS `TOTAL` 
FROM
    `posts` 
WHERE
    ( YEAR ( `posts`.`created_at` ) = YEAR ( now( ) ) )

和JSON导致laravel {{route('chart.all')}}显示如下:

[{"Januari":0,"Pebruari":0,"Maret":0,"April":0,"Mei":0,"Juni":0,"Juli":0,"Agustus":11,"September":0,"Oktober":0,"November":0,"Desember":0,"TOTAL":0}]

我在php文件中放入的