• 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.

    '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="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 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="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>
  • 相关阅读:
    Android学习笔记一:项目目录结构
    ReactNative踩坑日志——OnPress随着render()执行被自动调用?
    ReactNative踩坑日志——使用async/await语法解决网络请求的异步导致的指令执行顺序错乱问题
    ReactNative踩坑日志——代码执行方式(面向对象)
    ReactNative踩坑日志——fetch如何向服务器传递参数
    ReactNative踩坑日志——函数绑定this
    ReactNative踩坑日志——如何实现删除scrollview中的视图
    ReactNative踩坑日志——页面跳转之——Undefined is not an Object(evaluating this2.props.navigation.navigate)
    Git使用笔记
    CSS拾遗
  • 原文地址:https://www.cnblogs.com/jasonLiu2018/p/11976622.html
Copyright © 2020-2023  润新知