Syncing axis zooms across multiple charts

This demo shows how we can sync zoom of an X-axes across multiple charts.

Code

The function that performs syncing:

function syncAxes(targetChart) {
  let targetAxis = targetChart.xAxes.getIndex(0);
  if (targetAxis._skipSync != true) {
    let start = targetAxis.get("start");
    let end = targetAxis.get("end");
    am5.array.each(chartsToSync, function(chart) {
      if (chart != targetChart) {
        let axis = chart.xAxes.getIndex(0);
        axis._skipSync = true;
        axis.setAll({
          start: start,
          end: end
        })
        axis._skipSync = false;
      }
    });
  }
}
function syncAxes(targetChart) {
  var targetAxis = targetChart.xAxes.getIndex(0);
  if (targetAxis._skipSync != true) {
    var start = targetAxis.get("start");
    var end = targetAxis.get("end");
    am5.array.each(chartsToSync, function(chart) {
      if (chart != targetChart) {
        var axis = chart.xAxes.getIndex(0);
        axis._skipSync = true;
        axis.setAll({
          start: start,
          end: end
        })
        axis._skipSync = false;
      }
    });
  }
}

Using events to monitor changes to start and end settings on each axis:

// Set up syncing
xAxis.on("start", function() {
  syncAxes(chart);
});
xAxis.on("end", function() {
  syncAxes(chart);
});
// Set up syncing
xAxis.on("start", function() {
  syncAxes(chart);
});
xAxis.on("end", function() {
  syncAxes(chart);
});

Demo

See the Pen
Syncing axis zooms across multiple charts
by amCharts team (@amcharts)
on CodePen.0

Posted in Uncategorized