问题
I am trying to push data dynamically during runtime for the Animated Time-Line Pie Chart
Here is the code:
var array = new Array();
function pushvalue(curyear,curmonth,monvalue){
array.push({year: curyear, month: curmonth, count: moncount});
}
var chartdata = new Array();
for (var i=1; i < array.length; i++){
chartdata = {
“Year”: array[i].year: [
{ “Month”: array[i].month, “Count”: array[i].count }, ] };
}
The value for array is got from input from file.
The problem I get is the chartdata can’t get the value assigned to it with the objects year, it shows error as expected a ‘}’ or syntax error if I include or exclude ‘:’ after the ”Year”: array[i].year
May I know what am I doing wrong and how to fix it? Thank you!
回答1:
The syntax for assigning a new year is wrong. You can't do "Year": varname: [ ... ] in one line. You'll want to change it to something like this:
for (var i = 0; i < array.length; ++i) {
if (!chartData[array[i].year]) {
chartData[array[i].year] = [];
}
chartData[array[i].year].push({"Month": array[i].month, "Count": array[i].count });
}
Demo using some fake values: http://jsfiddle.net/9428r6fw/4/
Also note that your pushvalue
method is wrong - your parameter for the count is named monvalue
but you're trying to push moncount
来源:https://stackoverflow.com/questions/46475345/how-to-provide-data-to-animated-time-line-pie-amcharts