Create chart with amcharts using nested Json

亡梦爱人 提交于 2019-12-12 03:36:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!