问题
I need to create a serial chart with AmCharts but I can't get it to plot the nested bySexeDto array's nbre value.
Here's my JSON structure.
How do I get AmCharts to plot nested structures?
回答1:
AmCharts doesn't look at nested JSON structures when drawing the chart. You have to move the nested array in the bySexeDto
field into the top level as its own separate element.
Here's an example that takes your array and flattens it into a an array of objects resembling {"category": "...", "value0": ..., "value1": ...}
//remap the array by flattening it into a compatible AmCharts dataProvider array with a matching categoryField and valueFields for each graph.
//this returns an array of {"category": "...", "value0": ..., "value1": ..., /* etc ... */}
var processedChartData = rawData.map(function(rawDataElement) {
var newDataElement = { "category": rawDataElement.libelleAr };
rawDataElement.bySexeDto.forEach(function(nestedElement, index) {
newDataElement["value" + index] = nestedElement.nbre;
newDataElement["subcategory" + index] = nestedElement.libelleAr; //for balloonText purposes
});
return newDataElement;
});
Demo
If you need extra fields from the original array, modify the code to include them in the newDataElement structure.
来源:https://stackoverflow.com/questions/43225959/create-chart-with-amcharts-using-nested-json