欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Chart.js在Laravel項(xiàng)目中的應(yīng)用示例

 更新時間:2017年09月09日 16:06:44   作者:ChainZhang  
本篇文章主要介紹了Chart.js在Laravel項(xiàng)目中的應(yīng)用示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

介紹

Chart.js是一個HTML5圖表庫,使用canvas元素來展示各式各樣的客戶端圖表,支持折線圖、柱形圖、雷達(dá)圖、餅圖、環(huán)形圖等, 本文將介紹如何在laravel項(xiàng)目中使用chart.js

安裝

可以通過以下命令在 npm 或 bower 中來安裝chart.js。

npm install chart.js --save
bower install chart.js --save

可以在你的項(xiàng)目中使用 CDN link。

https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.js
https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.bundle.min.js
https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.js
https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js

可以從GitHub里面下載最新版本的 Chart.js 從github下載的話,里面也有很多例子可以學(xué)習(xí)。

我這邊選擇的是下載了幾個js文件,然后放置于Laravel項(xiàng)目的public/js目錄下面,如下圖:

使用

在html中任何你希望的位置加入以下代碼, 這個canvas就是圖表的位置。:

<canvas id="my_chart" width="300" height="300"></canvas>

然后在controller中實(shí)現(xiàn)一個數(shù)據(jù)查詢的方法,且要返回一個json字符串, 具體的數(shù)據(jù)根據(jù)你的需求而定,這邊我只查了一組數(shù)據(jù),制作一個餅圖,也可以要多組數(shù)據(jù)制作折線圖,柱狀圖等。

  public function GetChartData(){
    $my_data = array();
    array_push($today_data, VisitCapacity::where('my_data','>=', Carbon::today())->where('site',1)->count());
    array_push($today_data, VisitCapacity::where('my_data','>=', Carbon::today())->where('site',2)->count());
    array_push($today_data, VisitCapacity::where('my_data','>=', Carbon::today())->where('site',3)->count());
    array_push($today_data, VisitCapacity::where('my_data','>=', Carbon::today())->where('site',4)->count());
    Log::info(json_encode($my_data));
    return $my_data;
  }

添加路由:

 Route::get('get_chart_data', 'Member\UserController@GetChartData');

js實(shí)現(xiàn):

$.get('get_chart_data',function (data, status) {
var ctx = document.getElementById("my_chart").getContext("2d");
      var my_chart = new Chart(ctx,{
        type: 'pie',
        data: {
          labels: [
            "首頁文章列表",
            "分類文章列表",
            "文章詳情",
            "關(guān)于我",
          ],
          datasets: [{
            data: data,
            backgroundColor: [
              window.chartColors.red,
              window.chartColors.orange,
              window.chartColors.purple,
              window.chartColors.green,
            ],
          }]
        },
        options: {
          responsive: true,
        }
      });
});

顏色的定義:

window.chartColors = {
  red: 'rgb(255, 99, 132)',
  orange: 'rgb(255, 159, 64)',
  yellow: 'rgb(255, 205, 86)',
  green: 'rgb(75, 192, 192)',
  blue: 'rgb(54, 162, 235)',
  purple: 'rgb(153, 102, 255)',
  grey: 'rgb(201, 203, 207)'
};

結(jié)果如下圖:

將js中的type的值pie改成doughnut,結(jié)果如下圖:

更多的使用見官方文檔:http://chartjs.cn/docs/#line-chart-introduction

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論