laravel-admin 添加圖表 Chartjs

github地址:https://github.com/laravel-admin-extensions/chartjs

Installation

composer require laravel-admin-ext/chartjs php artisan vendor:publish --tag=laravel-admin-chartjs

 

onfiguration

Open config/admin.php, add configurations that belong to this extension at extensions section.php

'extensions' => [ 'chartjs' => [ // Set to `false` if you want to disable this extension
            'enable' => true, ] ]

Usage

Create a view in views directory like resources/views/admin/chartjs.blade.php, and add following codes:laravel

<canvas id="myChart" width="400" height="400"></canvas>
<script> $(function () { var ctx = document.getElementById("myChart").getContext('2d'); var myChart = new Chart(ctx, { type: 'bar', data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: '# of Votes', data: [12, 19, 3, 5, 2, 3], backgroundColor: [ 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)' ], borderColor: [ 'rgba(255,99,132,1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)' ], borderWidth: 1 }] }, options: { scales: { yAxes: [{ ticks: { beginAtZero:true } }] } } }); }); </script>

Then show it on the pagegit

class ChartjsController extends Controller { public function index(Content $content) { return $content ->header('Chartjs') ->body(new Box('Bar chart', view('admin.chartjs'))); } }

 

拓展:github

  折線圖和柱狀圖一塊兒使用canvas

  

<canvas id="myChart1" width="400" height="400"></canvas>
<script> $(function () { var ctx = document.getElementById("myChart1").getContext('2d'); var myChart = new Chart(ctx, { type: 'bar', data: { labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"], datasets: [{ label: '# of Votes',
          //數據類型 折線圖 type:
"line", data: [12, 19, 3, 5, 2, 3], backgroundColor:'rgba(54, 162, 235, 0.1)', borderColor:'rgba(255,99,132,1)', borderWidth: 1 },{ label: '# of Votes',
          //數據類型 柱狀圖 type:
"bar", data: [16, 13, 11, 15, 2, 3], backgroundColor: [ 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)' ], borderColor: [ 'red', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)' ], borderWidth: 1 }] }, options: {
        //顯示數值
"animation": { "duration": 1, "onComplete": function() { var chartInstance = this.chart, ctx = chartInstance.ctx; ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontSize, Chart.defaults.global.defaultFontStyle, Chart.defaults.global.defaultFontFamily);
            //設置字體顏色 ctx.fillStyle
= "black"; ctx.textAlign = 'center'; ctx.textBaseline = 'bottom'; this.data.datasets.forEach(function(dataset, i) { var meta = chartInstance.controller.getDatasetMeta(i); meta.data.forEach(function(bar, index) { var data = dataset.data[index]; ctx.fillText(data, bar._model.x, bar._model.y - 5); }); }); } }, scales: { yAxes: [{ ticks: { beginAtZero:true } }] } } }); }); </script>
相關文章
相關標籤/搜索