In the previous tutorials we showed you how to use <SciChartReact/> and <SciChartGroup/> to dynamically add and remove charts to a group in React.
As a basis for this tutorial, use Tutorial 04 - Adding & Removing Charts. We're going to build directly on top of this code, so go ahead and copy to a new project folder.
Also, for the project setup with webpack and npm, check out Tutorial 01 - Understanding the scichart-react boilerplate which explains how to start from scratch!
Refactoring the App.jsx Code
Starting with the App.jsx code from Tutorial 04, let's begin by refactoring the code. We want to extract the chart initialization into a separate code file, as well as create a data manager to provide data to the chart on initialization.
Creating a DataManager Class
Add a new file to src/RandomWalkGenerator.js and paste in the following code. This is a JavaScript version of the RandomWalkGenerator.ts from Github which simply generates a randomised waveform based on some parameters.
Also we're going to add a DataManager class, which will manage and fetch data by ID for charts. This can be extended later when you're working on your projects & apps to fetch data remotely, and it returns a promise to allow for asynchronous requests to your server or database for fetching and caching of data. This simulates a 100ms delay to account for requests and can be extended as you wish.
Refactoring App.jsx to load data from DataManager
First we're going to refactor App.jsx to extract the chart initialization logic into a new file: initChart.js. We'll also add a setData function and some chart modifiers to provide zooming, panning and rollover tooltip behaviours. Go ahead and remove simpleChart fuhction from App.jsx and add a new file called initChart.js.
Note that here we've extended the previous chart initialization function with some extra behaviours:
- We now pass in
chartIdas well aschartGroupIdto the function. - We create a chart with a border, so we can differentiate between different charts in the group.
- We remove the
chartTitle, instead setting the title on the xAxis to save space. - We set
yAxis.autoRange = EAutoRange.Always. This will be important later. - We add some chart modifiers to provide zooming, panning and tooltip behaviour.
modifierGroupis passed to theRolloverModifierto share mouse events across charts in the same group. - We create and return a
setDatafunction, which adds a series to the chart
Next, update the App.jsx to use DataManager. Using <SciChartReact onInit/> we can fetch data asynchronously then call setData to add a series to the chart.
Note that onInit cannot be an async function, so we use fetch().then() in order to set the data on the chart.
Synchronizing Zoom, Pan and Tooltip Operations Across Charts
The next step in the tutorial is to synchronize Zoom, Pan and Tooltip operations across charts. To do this we're going to use the AxisSynchronizer class found in the SciChart.js demo - Sync Multi Chart example, as well as the modifierGroup property found on ChartModifierBase.
You'll notice in the initChart() function modifierGroup is set here:
This ensures that mouse events from one ChartModifier are passed to others in the same group. This will partly tooltips across charts but will not synchronize everything - such as axis sizes or axis.visibleRange. To do this, we need some further logic, found in AxisSynchronizer.
Go ahead and add a new class file to the project, AxisSynchronizer.js. Add the following code:
Next, we ned to update App.jsx where the <SciChartReact/> component is declared to use onInit and onDelete to add/remove the chart xAxis from the AxisSynchronizer. This will ensure that all visibleRanges on the axis are synchronized and that all charts zoom and pan together in the group.
Where axisSynchronizer is declared at the top of the App() React Component as follows:
Here's the completed App.jsx output and initChart function.
And here's the final output of the tutorial:

Click the Add Chart, Remove Chart button. Now zoom on the chart by dragging the mouse. Use the mouse-wheel to zoom and double-click to reset zoom. All charts should be synchronized, tooltips should track the same x-Value across charts, and zooming/panning in the x-direction should match.