我要做的是有一个看起来像4个圆环图的小提琴输出
http://jsfiddle.net/chaninlee/zZYxY/
不幸的是,我在这里“作弊”是因为我写道:
{label : featureSet.features[0].attributes['STATE_NAME'] ,value : featureSet.features[0].attributes['MED_AGE']},
{label : featureSet.features[1].attributes['STATE_NAME'] ,value : featureSet.features[1].attributes['MED_AGE']},
{label : featureSet.features[2].attributes['STATE_NAME'] ,value : featureSet.features[2].attributes['MED_AGE']},
{label : featureSet.features[3].attributes['STATE_NAME'] ,value : featureSet.features[3].attributes['MED_AGE']}
我以为我可以通过编写如下代码来正确返回所有输出:
http://jsfiddle.net/chaninlee/T8XZM/
看起来像是在使用,
{label : featureSet.features[i].attributes['STATE_NAME'] ,value : featureSet.features[i].attributes['MED_AGE']}
将在我的情况下循环0-3,但使用最后[3]输出,因此整个圆环图表由一个州和中位年龄表示。
我想知道如何做到这一点,所以我不必为每个[i]
输入内容答案 0 :(得分:0)
目前的代码是:
for (var i=0; i<featureSet.features.length;i++)
{
// Each loop re-assigns a new object to the SAME variable
// which overwrites the previous value ..
output = {label : featureSet.features[i].attributes['STATE_NAME'],
value : featureSet.features[i].attributes['MED_AGE']};
console.log(output);
}
// Only the CURRENT (LAST) value assigned to the output variable
// inside the loop is used to create an array of one element.
Morris.Donut({
element: 'donut-example',
data: [output]
});
代码看起来应该更像这样:
var data = [];
for (var i=0; i<featureSet.features.length;i++)
{
// Create new row/output object ..
var output = {label : featureSet.features[i].attributes['STATE_NAME'],
value : featureSet.features[i].attributes['MED_AGE']};
// .. and then store the created object in an array
data.push(output);
}
Morris.Donut({
element: 'donut-example',
// Use created array
data: data
});