javascript - 为 Charts.js 添加 for 循环/foreach 循环

标签 javascript foreach chart.js

我尝试在下面的代码中添加 foreach 或 for 循环,以便为 Charts.js 创建多个数据集。这将允许我在此折线图上创建多条线。

我有一个 PHP 对象,我可以对其进行编码以稍后填充变量,但是如何以及在哪里注入(inject)循环以仅创建多个数据集?

<script>
var chart1Handler = function() {
var data = {
    labels: {!! json_encode($month_array) !!},
    datasets: [{
            label:'',
            fillColor: 'rgba(220,220,220,0.2)',
            strokeColor: 'rgba(220,220,220,1)',
            pointColor: 'rgba(220,220,220,1)',
            pointStrokeColor: '#fff',
            pointHighlightFill: '#fff',
            pointHighlightStroke: 'rgba(220,220,220,1)',
            data: {{ json_encode($new_taco) }}
    }]
};

var options = {

    maintainAspectRatio: false,

    // Sets the chart to be responsive
    responsive: true,

    ///Boolean - Whether grid lines are shown across the chart
    scaleShowGridLines: true,

    //String - Colour of the grid lines
    scaleGridLineColor: 'rgba(0,0,0,.05)',

    //Number - Width of the grid lines
    scaleGridLineWidth: 1,

    //Boolean - Whether the line is curved between points
    bezierCurve: false,

    //Number - Tension of the bezier curve between points
    bezierCurveTension: 0.4,

    //Boolean - Whether to show a dot for each point
    pointDot: true,

    //Number - Radius of each point dot in pixels
    pointDotRadius: 4,

    //Number - Pixel width of point dot stroke
    pointDotStrokeWidth: 1,

    //Number - amount extra to add to the radius to cater for hit detection outside the drawn point
    pointHitDetectionRadius: 20,

    //Boolean - Whether to show a stroke for datasets
    datasetStroke: true,

    //Number - Pixel width of dataset stroke
    datasetStrokeWidth: 2,

    //Boolean - Whether to fill the dataset with a colour
    datasetFill: true,

    // Function - on animation progress
    onAnimationProgress: function() {
    },

    // Function - on animation complete
    onAnimationComplete: function() {
    },

    //String - A legend template
    legendTemplate: '<ul class="tc-chart-js-legend"><% for (var i=0; i<datasets.length; i++){%><li><span style="background-color:<%=datasets[i].strokeColor%>"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>'
};
// Get context with jQuery - using jQuery's .get() method.
var ctx = $("#chart1").get(0).getContext("2d");
// This will get the first returned node in the jQuery collection.
var chart1 = new Chart(ctx).Line(data, options);
//generate the legend
var legend = chart1.generateLegend();
//and append it to your page somewhere
$('#chart1Legend').append(legend);

};

</script>

最佳答案

您可以在 PHP 代码中创建数据集数组并将其作为 json 传递给 JS。然后您只需要在使用之前对其进行解析即可。

在 PHP 中:

$datasets = [
    [
        'label'=>'',
        'fillColor'=> 'rgba(220,220,220,0.2)',
        'strokeColor'=> 'rgba(220,220,220,1)',
        'pointColor'=> 'rgba(220,220,220,1)',
        'pointStrokeColor'=> '#fff',
        'pointHighlightFill'=> '#fff',
        'pointHighlightStroke'=> 'rgba(220,220,220,1)',
        'data' => [1,2,3]
    ],
    [
        'label'=>'',
        'fillColor'=> 'rgba(220,220,220,0.2)',
        'strokeColor'=> 'rgba(220,220,220,1)',
        'pointColor'=> 'rgba(220,220,220,1)',
        'pointStrokeColor'=> '#fff',
        'pointHighlightFill'=> '#fff',
        'pointHighlightStroke'=> 'rgba(220,220,220,1)',
        'data' => [1,2,3]
    ]
];
$datasets = json_encode($datasets);

在 JS 中:

var data = {
    labels: {!! json_encode($month_array) !!},
    datasets: JSON.parse('<?=$datasets?>')
};

顺便说一句,我认为值得一提的是,JS 数组的处理方式与 JSON 字符串不同,尽管它们看起来非常相似。因此,尽管我没有看到太多实现细节,但我认为您需要传递一个数组而不是 JSON 作为 labels 的值。您也可以使用与此处的数据集相同的方法。

关于javascript - 为 Charts.js 添加 for 循环/foreach 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38488121/

相关文章:

javascript - Cordova视频方向问题android

javascript - 带有 knockout 绑定(bind)的嵌套 foreach ?

javascript - JavaScript 中二维数组的 forEach() 和 Apply() 方法

chart.js - 在 ng2-charts 中使用 chartjs-plugin-annotation

javascript - 在 Chart.js 中使用和读取 Knockout.js 中的变量

chart.js - 当在任意值之上/之下绘制时,如何更改 Chart.js 线条的颜色?

javascript - 根据背景颜色更改颜色

javascript - jQuery $ ('body' ).click() 可以捕获我单击了没有类/id 的嵌套元素吗?

javascript - 将图像上传到 S3 存储桶将其存储为应用程序/八位字节流,但我想将其存储为图像以使用它(JavaScript/节点)

javascript - 用不同的值填充数组的数组