-1

尝试在 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]   
            }],
});
4

2 回答 2

4

一个对象的属性不能是一个for循环,就像你试图做的那样。它根本不是有效的 JavaScript 语法。(当然,在 Python 中,列表推导会起作用——但这不是 Python。)

您需要创建一个数组,然后将其分配为属性的值:

var series = [];

for (var i=0; i<food.length; i++) {

    series.push({
        name: food[i],
        data: [calorie[i], protein[i], fat[i]]
    });
}

您问题中的代码表明对 JavaScript 语法缺乏基本的理解;我建议阅读有关对象文字的内容。

于 2012-12-09T06:12:22.917 回答
-1

嗨,我实际上还没有尝试过,但我在想类似的事情:

series = new Array();
for (i=0;i<food.length;i++) {
    item = new Object();
    item.name = food[i];
    item.data = new Array();
    item.data = item.data.concat(calorie,protein,fat);
    series.concat(item);
}

好的,感谢马特的建议:

series = [];
for (i=0;i<food.length;i++) {
    item = {name: food[i], data: Array.concat(calorie,protein,fat)};
    series.push(item);
}
于 2012-12-09T06:23:36.613 回答