使用嵌套的Json使用amcharts创建图表 [英] Create chart with amcharts using nested Json

查看:100
本文介绍了使用嵌套的Json使用amcharts创建图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用AmCharts创建一个序列图,但是我无法绘制它来绘制嵌套的bySexeDto数组的nbre值.

I need to create a serial chart with AmCharts but I can't get it to plot the nested bySexeDto array's nbre value.

这是我的 JSON结构.

如何获得AmCharts来绘制嵌套结构?

How do I get AmCharts to plot nested structures?

推荐答案

在绘制图表时,AmCharts不会查看嵌套的JSON结构.您必须将bySexeDto字段中的嵌套数组作为其自己的单独元素移到顶层.

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.

这里有一个示例,将您的数组放到一个类似于{"category": "...", "value0": ..., "value1": ...}

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;
});

演示

如果您需要原始数组中的其他字段,请修改代码以将其包含在newDataElement结构中.

If you need extra fields from the original array, modify the code to include them in the newDataElement structure.

这篇关于使用嵌套的Json使用amcharts创建图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆