您好我为我的服务器监控创建了一些图表。我用Php获取数据并将其作为数组传递给javasccript:
<script>
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var obj = JSON.parse('<?php echo json_encode($names) ?>');
var datenbarchart = obj.join(",");
var obj1 = JSON.parse('<?php echo json_encode($nameserver) ?>');
var barChartData = {
labels : [ obj1[0], obj1[1], obj1[2], obj1[3], obj1[4], obj1[5], obj1[6]],
datasets : [
{
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data : [datenbarchart]
}
]
}
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartData, {
responsive : true
});
}
</script>
问题部分是,我尝试将第一个数组与join方法分开,以获得由","
分隔的字符串:
var datenbarchart = obj.join(",");
当我将其传递给Chartjs数据时,通常数据看起来像这样:data: [60,50,40,50]
它没有显示图表中的任何数据。是不是可以用字符串来做,因为每个条都需要整个字符串?
答案 0 :(得分:1)
根据http://www.chartjs.org/docs/ 数据字段是一个数组,所以你的语法应该是:
var datenbarchart = obj;
...
var barChartData = {
labels : [ obj1[0], obj1[1], obj1[2], obj1[3], obj1[4], obj1[5], obj1[6]],
datasets : [
{
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data : datenbarchart
}
]
}