I am trying to create a bar chart using AmCharts 5 where the segments for each column are sorted from smallest to largest, so that the largest segment is always placed at the top of the column.
My chart code so far looks like this:
// Get chart div by id
var chartDiv = document.getElementById(selector);
const data = JSON.parse(chartDiv.getAttribute('data-chart'));
const fontSize = 12;
const markerSize = 12;
var root = am5.Root.new(selector);
var chart = root.container.children.push(
am5xy.XYChart.new(root, {
panY: false,
layout: root.verticalLayout,
dy: 10,
})
);
// Create X-axis
let xAxis = chart.xAxes.push(
am5xy.CategoryAxis.new(root, {
categoryField: "week_ending_date",
renderer: am5xy.AxisRendererX.new(root, {})
})
);
let xRenderer = xAxis.get("renderer");
xRenderer.labels.template.setAll({
fontSize: fontSize
});
xAxis.data.setAll(data);
// Create Y-Axis
var yAxis = chart.yAxes.push(
am5xy.ValueAxis.new(root, {
renderer: am5xy.AxisRendererY.new(root, {}),
})
);
let yRenderer = yAxis.get("renderer");
yRenderer.labels.template.setAll({
fontSize: fontSize
});
// Add series
// https://www.amcharts.com/docs/v5/charts/xy-chart/series/
function makeSeries(name, fieldName, colorHex) {
var series = chart.series.push(am5xy.ColumnSeries.new(root, {
name: name,
stacked: true,
xAxis: xAxis,
yAxis: yAxis,
baseAxis: xAxis,
valueYField: fieldName,
categoryXField: "week_ending_date",
fill: colorHex,
}));
series.columns.template.setAll({
tooltipText: "{name} - {valueY}",
tooltipY: am5.percent(90)
});
series.data.setAll(data);
series.bullets.push(function() {
return am5.Bullet.new(root, {
sprite: am5.Label.new(root, {
text: "{valueX}",
fill: root.interfaceColors.get("alternativeText"),
centerY: am5.p50,
centerX: am5.p50,
populateText: true,
fontSize: 12,
})
});
});
legend.data.push(series);
}
// Add series for each site in the data
// Get all site names
let sites = Object.keys(data[0])
// Remove first value (week_ending_date)
sites.shift()
sites.forEach((site, index) => {
makeSeries(site, site, COLOR_SCHEME[index]);
})
And my data looks like this:
[
{
"week_ending_date": "2022-10-02",
"site A": 100,
"site B": 150,
"site C": 200,
},
{
"week_ending_date": "2022-10-09",
"site A": 400,
"site B": 150,
"site C": 50,
},
...
So in my first column, I would want site C to appear at the top of it, and in the second column I want site A to be at the top.
Anyone know if this is possible and how to achieve it?
Thanks in advance.
I've tried searching through the documentation but I cant find any properties that would allow me to reorder segments.