尝试在 javascript 中运行几个 for 循环来填充图表的信息,但出现错误用户选择一定数量的食物,然后填充具有食物名称的食物数组,然后是对应的蛋白质卡路里和脂肪数组对每一种食物。
这是我现在拥有的代码。目前我收到一个错误,上面写着 Uncaught SyntaxError
: Unexpected token var
:
var fat = $.parseJSON('<?php print(json_encode($fat, true)); ?>');
var calorie = $.parseJSON('<?php print(json_encode($calorie, true)); ?>');
var protein = $.parseJSON('<?php print(json_encode($protein, true)); ?>');
var food = $.parseJSON('<?php print(json_encode($food, true)); ?>');
series: [{
for (var i=0;i<food.length;i++){
name: food[i],
data: for (var j=0;j<fat.length;j++){
calorie[j], protein[j], fat[j]
}
}
}]
我从一个网站上复制了图表,他们有这样的系列
series: [{
name: 'London',
data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]
}, {
name: 'Berlin',
data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]
}]
我将如何修复我的 for 循环以使其工作。
script type="text/javascript">
$(function () {
var chart;
var fat = $.parseJSON('<?php print(json_encode($fat, true)); ?>');
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'column'
},
title: {
text: 'MComparing foods is Awesome'
},
tooltip: {
formatter: function() {
return ''+
this.x +': '+ this.y +' mm';
}
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series : []
for (var i=0; i<food.length; i++)
{
series.push({
name: food[i],
data: [calorie[i], protein[i], fat[i]]});
}
});
});
});
</script>
和之前
series: [{
name: 'Tokyo',
data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
name: 'New York',
data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]
}, {
name: 'London',
data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]
}, {
name: 'Berlin',
data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]
}],
});