Skip to content Skip to sidebar Skip to footer

Amcharts 4 Getting Nested Array Data

I am using amcharts 4 and wanted to use my nested data, I know amcharts doesn't support nested data so I added a on parseended event to change the structure of my json. I can get t

Solution 1:

Fixed it by removing the [key] from the nested item:

var colorSet = new am4core.ColorSet();
      colorSet.saturation = 0.4;
      chart.dataSource.url = "https://localhost:44321/api/upload/readFile";
      chart.dataSource.events.on("parseended", function (ev) {
        // parsed data is assigned to data source's `data` property
        var data = ev.target.data;
        var newData = [];
        data.forEach(function (dataItem) {
          var newDataItem = {};
          Object.keys(dataItem).map(function (key) {
            if (typeof dataItem[key] === "object") {
              newDataItem["_id"] = dataItem[key]["@id"];
              dataItem[key]["Column"].forEach(function (nestedItem, index) {
                Object.keys(dataItem).map(function () {
                  newDataItem[nestedItem["@name"] + index] = nestedItem["#text"];
                })
              })           
            } else {
              newDataItem[key] = dataItem[key];
            }
          });
          newData.push(newDataItem);
        });
        chart.dataSource.data = newData
        console.log(JSON.stringify(chart.dataSource.data));
        });

Post a Comment for "Amcharts 4 Getting Nested Array Data"