2

How can I add extra data to the json object in AMchart?
When my obj is simple everything parses:
var data = [ {year: "1930",italy: 4,germany: 5.1,uk: 3}, {year: "1934",italy: 1,germany: 2,uk: 6}, {year: "1938",italy: 2,germany: 3,uk: 1} ];

But when it looks like this it does not:
var data = [ {year:"1930", italy:[{aa:20,bb:"21"}], germany:[{aa:30,bb:"44"}], uk:[{aa:40,bb:"77"}] } ];

This is the code that is responsible for parsing:

   var series = chart.series.push(
    am5xy.LineSeries.new(root, {
      name: name,
      xAxis: xAxis,
      yAxis: yAxis,
      valueYField: field, // << -- issue
      categoryXField: "year",
      tooltip: am5.Tooltip.new(root, {
        pointerOrientation: "horizontal",
        labelText: "[bold]{name}[/]\n{categoryX}: {valueY}"
      })
    })
  );

I can get the correct values using this but when i enter the values and replace field above the code does not parse correct,

  if(chart.series._values[0]){
    var data1 = chart.series._values[0]._dataItems[0].dataContext;
    
    if(data1[field][0])
      var valY = data1[field][0].aa;
      
    //console.log( valY );
  }

here is my complete code:

am5.ready(function() {

var root = am5.Root.new("chartdiv");
root._logo.dispose();

root.setThemes([
  am5themes_Animated.new(root)
]);

var chart = root.container.children.push(
  am5xy.XYChart.new(root, {
    panX: true,
    panY: true,
    wheelX: "panX",
    wheelY: "zoomX",
    layout: root.verticalLayout,
    pinchZoomX:true
  })
);

// Add cursor
// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
  behavior: "none"
}));
cursor.lineY.set("visible", false);

// The data
var data = [    {year:"1930", italy:[{aa:20,bb:"21"}], germany:[{aa:30,bb:"44"}], uk:[{aa:40,bb:"77"}] } ];

/* var data = [ {year: "1930",italy: 4,germany: 5.1,uk: 3},
  {year: "1934",italy: 1,germany: 2,uk: 6},
  {year: "1938",italy: 2,germany: 3,uk: 1} ]; */

// Create axes
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xRenderer = am5xy.AxisRendererX.new(root, {});
xRenderer.grid.template.set("location", 0.5);
xRenderer.labels.template.setAll({
  location: 0.5,
  multiLocation: 0.5
});

var xAxis = chart.xAxes.push(
  am5xy.CategoryAxis.new(root, {
    categoryField: "year",
    renderer: xRenderer,
    tooltip: am5.Tooltip.new(root, {})
  })
);

xAxis.data.setAll(data);

var yAxis = chart.yAxes.push(
  am5xy.ValueAxis.new(root, {
    maxPrecision: 1,
    renderer: am5xy.AxisRendererY.new(root, {
      inversed: false
    })
  })
);

function createSeries(name, field) { 

  if(chart.series._values[0]){
    var data1 = chart.series._values[0]._dataItems[0].dataContext;
    
    if(data1[field][0])
      var valY = data1[field][0].aa;
      
    //console.log( valY );
  }

  var series = chart.series.push(
    am5xy.LineSeries.new(root, {
      name: name,
      xAxis: xAxis,
      yAxis: yAxis,
      valueYField: field, // issue when replced with: valY
      categoryXField: "year",
      tooltip: am5.Tooltip.new(root, {
        pointerOrientation: "horizontal",
        labelText: "[bold]{name}[/]\n{categoryX}: {valueY}"
      })
    })
  );
  
  series.bullets.push(function() {
    return am5.Bullet.new(root, {
      sprite: am5.Circle.new(root, {
        radius: 5,
        fill: series.get("fill")
      })
    });
  });


  series.set("setStateOnChildren", true);
  series.states.create("hover", {});

  series.mainContainer.set("setStateOnChildren", true);
  series.mainContainer.states.create("hover", {});

  series.strokes.template.states.create("hover", {
    strokeWidth: 4
  });

  series.data.setAll(data);
  series.show(1000);
}


createSeries("Italy", "italy");
createSeries("Germany", "germany");
createSeries("UK", "uk");


var legend = chart.children.push(
  am5.Legend.new(root, {
    centerX: am5.percent(90),
    x: am5.percent(90),
    centerY: am5.percent(105),
    y: am5.percent(105),
  })
);

// Make series change state when legend item is hovered
legend.itemContainers.template.states.create("hover", {});

legend.itemContainers.template.events.on("pointerover", function(e) {
  e.target.dataItem.dataContext.hover();
});
legend.itemContainers.template.events.on("pointerout", function(e) {
  e.target.dataItem.dataContext.unhover();
});

legend.data.setAll(chart.series.values);

chart.appear(1000, 100);


});
t q
  • 4,593
  • 8
  • 56
  • 91

1 Answers1

2

It appears amcharts doesn't offer structured access to sub-objects in data through its data fields.

Thus, the only solution available is to preprocess the data; for your data structure, to cover both cases you could use something like

series.data.setAll(data
   .map(o=>Object.fromEntries(Object.entries(o)
       .map(([k, v])=>[k, v?.[0]?.aa ?? v]))
   )
)

Snippet:

var root = am5.Root.new("chartdiv");
root._logo.dispose();

root.setThemes([
    am5themes_Animated.new(root)
]);

var chart = root.container.children.push(
    am5xy.XYChart.new(root, {
        panX: true,
        panY: true,
        wheelX: "panX",
        wheelY: "zoomX",
        layout: root.verticalLayout,
        pinchZoomX:true
    })
);

// Add cursor
// https://www.amcharts.com/docs/v5/charts/xy-chart/cursor/
var cursor = chart.set("cursor", am5xy.XYCursor.new(root, {
    behavior: "none"
}));
cursor.lineY.set("visible", false);

// The data
var data = [{year:"1930", italy:[{aa:20,bb:"21"}], germany:[{aa:30,bb:"44"}], uk:[{aa:40,bb:"77"}] },
    {year: "1934", italy: 1,germany: 2,uk: 6}, {year: "1938",italy: 2,germany: 3,uk: 1}];
//var data = [ {year: "1930",italy: 4,germany: 5.1,uk: 3}, {year: "1934",italy: 1,germany: 2,uk: 6}, {year: "1938",italy: 2,germany: 3,uk: 1} ];

// Create axes
// https://www.amcharts.com/docs/v5/charts/xy-chart/axes/
var xRenderer = am5xy.AxisRendererX.new(root, {});
xRenderer.grid.template.set("location", 0.5);
xRenderer.labels.template.setAll({
    location: 0.5,
    multiLocation: 0.5
});

var xAxis = chart.xAxes.push(
    am5xy.CategoryAxis.new(root, {
        categoryField: "year",
        renderer: xRenderer,
        tooltip: am5.Tooltip.new(root, {})
    })
);

xAxis.data.setAll(data);

var yAxis = chart.yAxes.push(
    am5xy.ValueAxis.new(root, {
        maxPrecision: 1,
        renderer: am5xy.AxisRendererY.new(root, {
            inversed: false
        })
    })
);

function createSeries(name, field) {
    var series = chart.series.push(
        am5xy.LineSeries.new(root, {
            name: name,
            xAxis: xAxis,
            yAxis: yAxis,
            valueYField: field,
            categoryXField: "year",
            tooltip: am5.Tooltip.new(root, {
                pointerOrientation: "horizontal",
                labelText: "[bold]{name}[/]\n{categoryX}: {valueY}"
            })
        })
    );

    series.bullets.push(function() {
        return am5.Bullet.new(root, {
            sprite: am5.Circle.new(root, {
                radius: 5,
                fill: series.get("fill")
            })
        });
    });


    series.set("setStateOnChildren", true);
    series.states.create("hover", {});

    series.mainContainer.set("setStateOnChildren", true);
    series.mainContainer.states.create("hover", {});

    series.strokes.template.states.create("hover", {
        strokeWidth: 4
    });
    series.data.setAll(data.map(o=>Object.fromEntries(Object.entries(o).map(([k, v])=>[k, v?.[0]?.aa ?? v]))))
    series.show(1000);
}


createSeries("Italy", "italy");
createSeries("Germany", "germany");
createSeries("UK", "uk");


var legend = chart.children.push(
    am5.Legend.new(root, {
        centerX: am5.percent(90),
        x: am5.percent(90),
        centerY: am5.percent(105),
        y: am5.percent(105),
    })
);

// Make series change state when legend item is hovered
legend.itemContainers.template.states.create("hover", {});

legend.itemContainers.template.events.on("pointerover", function(e) {
    e.target.dataItem.dataContext.hover();
});
legend.itemContainers.template.events.on("pointerout", function(e) {
    e.target.dataItem.dataContext.unhover();
});

legend.data.setAll(chart.series.values);

chart.appear(1000, 100);
 #chartdiv {
     width: 100%;
     height: 300px;
 }
<div id="chartdiv"></div>
<script src="https://cdn.amcharts.com/lib/5/index.js"></script>
<script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
<script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>
kikon
  • 3,670
  • 3
  • 5
  • 20
  • thank you so much for this. How would I access `bb` from your map? – t q May 20 '23 at 20:56
  • 1
    Where there's `v?.[0]?.aa` in my code you can replace it with `v?.[0]?.bb` or possibly `parrseFloat( v?.[0]?.bb)` because at `bb` there are strings. But if you want to use both somehow, let me know how and I'll add that version – kikon May 20 '23 at 21:01
  • Yes I would like to access both and in the end there might be multiple elements in the json object. – t q May 20 '23 at 21:03
  • Do you want to create a series for `aa` and a different series for `bb` or do you want to combine them somehow (e.g., average) in the same series? Maybe you can create a set with more data and explore the possibilities. – kikon May 20 '23 at 21:08
  • they would be different series. The whole purpose of this was to add more values on hover to give each country more unique information. – t q May 20 '23 at 21:11
  • the values can all be string if that makes the code more streamline – t q May 20 '23 at 21:13
  • 1
    This [fiddle](https://jsfiddle.net/tjzL1oq3/1) contains a version that combines all the keys that may exist in `italy[0]`, or `uk[0]` but draws the point at the position specified by `aa`, or if the format is `italy: value` (no object) that `value` is used for both positioning and tooltip. – kikon May 20 '23 at 22:39
  • 1
    You may fork the fiddle and add/edit data as a base for changing the behavior. – kikon May 20 '23 at 22:40