Live Sorting of Radar Columns

A column chart bent into a ring that keeps itself sorted. Every 1.5 seconds, eleven countries get new values, and the columns slide into their new places, biggest first.

  • Try it:
  • Hover a column to read its value
  • Watch the columns re-sort
Get the code JavaScript React, Vue, Angular, Svelte JSON
About this chart

Why sort a chart live

Sorting turns values that keep changing into a ranking people can follow: the leader always starts at the top, and a column that climbs or falls slides past its neighbors. It suits scores, votes or sales that update while people watch. Bent into a ring, the chart also fits a square space, like a dashboard tile.

Good for

  • Live rankings: votes, scores, sales
  • Dashboards on a wall screen
  • Showing who overtakes whom

Think twice when

  • Reading exact values: the columns keep moving
  • Long names: there is little room around the ring
  • Printed reports: use a sorted bar chart
For developers

Get the code.

Built with amCharts 5, a JavaScript library. The JavaScript includes your theme and renderer (canvas or SVG); the JSON also follows the changes you make with the controls.

// Create root element
// https://docs.amcharts.com/v5/getting-started/#root-element
var root = am5.Root.new("chartdiv");

// Set themes
// https://docs.amcharts.com/v5/concepts/themes/
root.setThemes([
  am5themes_Animated.new(root),
  am5themes_ResponsiveMax.new(root)
]);

// Create chart
// https://docs.amcharts.com/v5/charts/xy-chart/
var chart = root.container.children.push(am5radar.RadarChart.new(root, {
  panX: false,    // no dragging to pan, sideways...
  panY: false,    // ...or up and down...
  wheelX: "none", // ...and the mouse wheel...
  wheelY: "none", // ...does nothing
  // a gap at the top leaves room for the value labels
  startAngle: -84,
  endAngle: 264,               // the circle runs 348 degrees, leaving a 12-degree gap
  innerRadius: am5.percent(40) // a hole in the middle, 40% of the radius
}));

// We don't want zoom-out button to appear while animating, so we hide it
chart.zoomOutButton.set("forceHidden", true);

// Create axes
// https://docs.amcharts.com/v5/charts/xy-chart/axes/
var xRenderer = am5radar.AxisRendererCircular.new(root, {
  minGridDistance: 30 // at least 30px between the country labels around the circle
});

xRenderer.grid.template.set("visible", false); // no lines from the center between the countries

var xAxis = chart.xAxes.push(am5xy.CategoryAxis.new(root, {
  maxDeviation: 0.3,
  categoryField: "country",
  renderer: xRenderer
}));

var yRenderer = am5radar.AxisRendererRadial.new(root, {});
// value labels centered in the gap at the top
yRenderer.labels.template.set("centerX", am5.p50);

var yAxis = chart.yAxes.push(am5xy.ValueAxis.new(root, {
  maxDeviation: 0.3,
  min: 0, // the columns start at zero
  renderer: yRenderer
}));

// Add series
// https://docs.amcharts.com/v5/charts/xy-chart/series/
var series = chart.series.push(am5radar.RadarColumnSeries.new(root, {
  name: "Series 1",
  xAxis: xAxis,
  yAxis: yAxis,
  valueYField: "value",
  categoryXField: "country",
  // Each column takes its own color from the series palette
  colorByDataItem: true
}));

// Rounded corners for columns
series.columns.template.setAll({
  cornerRadius: 5,
  tooltipText:"{categoryX}: {valueY}" // hover a column to see its country and value
});

// Set data
var data = [{
  "country": "USA",
  "value": 2025
}, {
  "country": "China",
  "value": 1882
}, {
  "country": "Japan",
  "value": 1809
}, {
  "country": "Germany",
  "value": 1322
}, {
  "country": "UK",
  "value": 1122
}, {
  "country": "France",
  "value": 1114
}, {
  "country": "India",
  "value": 984
}, {
  "country": "Spain",
  "value": 711
}, {
  "country": "Netherlands",
  "value": 665
}, {
  "country": "South Korea",
  "value": 443
}, {
  "country": "Canada",
  "value": 441
}];

xAxis.data.setAll(data);
series.data.setAll(data);

// update data with random values each 1.5 sec
setInterval(function () {
  updateData();
}, 1500)

// moves every value randomly by up to 200, then re-sorts the columns
function updateData() {
  am5.array.each(series.dataItems, function (dataItem) {
    var value = dataItem.get("valueY") + Math.round(Math.random() * 400 - 200);
    if (value < 0) { // no negative values
      value = 10;
    }
    // both valueY and valueYWorking change; only valueYWorking is animated
    dataItem.set("valueY", value);
    dataItem.animate({
      key: "valueYWorking",
      to: value,
      duration: 600,                       // 0.6 seconds...
      easing: am5.ease.out(am5.ease.cubic) // ...slowing down at the end
    });
  })

  sortCategoryAxis();
}

// Get series item by category
function getSeriesItem(category) {
  for (var i = 0; i < series.dataItems.length; i++) {
    var dataItem = series.dataItems[i];
    if (dataItem.get("categoryX") == category) {
      return dataItem;
    }
  }
}

// Axis sorting
function sortCategoryAxis() {

  // Sort by value
  series.dataItems.sort(function (x, y) {
    return y.get("valueY") - x.get("valueY"); // descending
  })

  // Go through each axis item
  am5.array.each(xAxis.dataItems, function (dataItem) {
    // get corresponding series item
    var seriesDataItem = getSeriesItem(dataItem.get("category"));

    if (seriesDataItem) {
      // get index of series data item
      var index = series.dataItems.indexOf(seriesDataItem);
      // calculate delta position
      var deltaPosition = (index - dataItem.get("index", 0)) / series.dataItems.length;
      // set index to be the same as series data item index
      dataItem.set("index", index);
      // set deltaPosition instantly
      dataItem.set("deltaPosition", -deltaPosition);
      // animate delta position to 0
      dataItem.animate({
        key: "deltaPosition",
        to: 0,
        duration: 1000,
        easing: am5.ease.out(am5.ease.cubic)
      })
    }
  });

  // Sort axis items by index.
  // This changes the order instantly, but as deltaPosition is set,
  // they keep in the same places and then animate to true positions.
  xAxis.dataItems.sort(function (x, y) {
    return x.get("index") - y.get("index");
  });
}

// Accessibility: keyboard and screen reader support
// https://docs.amcharts.com/v5/concepts/accessibility/
chart.setAll({
  focusable: true, // Tab reaches the chart, and a screen reader names it
  role: "figure",
  ariaLabel: "Radial column chart of eleven countries with values that change at random every 1.5 seconds, sorted from the biggest. Tab to the columns, then move between them with the arrow keys"
});
// the columns: one Tab stop (a focusable group), the arrow keys step through them
series.columns.template.setAll({
  isMeasured: false, // radar columns have no width or height of their own: without this, they can't take focus
  focusable: true,
  role: "figure",
  focusableGroup: "columns",
  hoverOnFocus: true, // a focused column shows its tooltip
  ariaLabel: "{categoryX}: {valueY}"
});

// Make stuff animate on load
// https://docs.amcharts.com/v5/concepts/animations/
series.appear(1000);
chart.appear(1000, 100);