0
我试图在foreach中添加或为以下代码中的for循环创建charts.js的多个数据集。这将允许我在该线图上创建多行。为Charts.js添加for循环/ foreach循环
我有一个PHP对象,我可以编码以便稍后填写变量,但是如何以及在哪里可以注入一个循环以仅创建多个数据集?
<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>
这应该绝对有效,我会在这里稍微更新以确保它确实。就标签属性而言,这将永远只是一个完整的日历年,所以我现在可以用硬编码。谢谢你,我会让你知道它是如何结束的! –
很快。我们很高兴去。谢谢@trajchevska,你摇滚! –
很高兴帮助! – trajchevska