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.

'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:

<canvas id="myChart" width="" height=""></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: [, , , , , ],
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:
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
});
</script>

Then show it on the page

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

拓展:

  折线图和柱状图一起使用

  

<canvas id="myChart1" width="" height=""></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: [, , , , , ],
backgroundColor:'rgba(54, 162, 235, 0.1)',
borderColor:'rgba(255,99,132,1)',
borderWidth:
},{
label: '# of Votes',
          //数据类型 柱状图
type: "bar",
data: [, , , , , ],
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:
}]
}, options: {
        //显示数值
"animation": {
"duration": ,
"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 - );
});
});
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
});
</script>
05-22 02:52