2016-07-20 231 views
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> 

回答

2

你可以在你的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?>') 
}; 

BTW,我認爲這是值得一提的是JS數組沒有被對待的方式作爲一個JSON字符串相同,甚至儘管它們看起來很相似。所以,即使我沒有看到有關實現的更多細節,我假設您需要傳遞一個數組而不是JSON作爲labels的值。您也可以使用與數據集相同的方法。

+0

這應該絕對有效,我會在這裏稍微更新以確保它確實。就標籤屬性而言,這將永遠只是一個完整的日曆年,所以我現在可以用硬編碼。謝謝你,我會讓你知道它是如何結束的! –

+0

很快。我們很高興去。謝謝@trajchevska,你搖滾! –

+0

很高興幫助! – trajchevska