In this tutorial we're going to show how to modify the initChart callback to pass properties, functions or data back into onInit in scichart-react. This is so you can connect to other UI in your application such as buttons, controls to manipulate the chart, update data or modify chart state.
The previous tutorial Tutorial 01 - Understanding the scichart-react boilerplate should serve as a reference for the initial project setup. Go ahead and copy the boilerplate to a new folder or project. You can get the code from here: Boilerplates/scichart-react
To begin with, copy the code from Boilerplates/scichart-react into a new folder. Open this folder in webstorm or vscode and perform npm install.
Accessing & Controlling the Chart via Nested Components
We'd like to modify App.jsx as follows.
- Delete the
chartConfigfrom the scichart-react boilerplate. We're going to useinitChartfunctions instead. - Modify the
initChartfunction to intialize a chart, but we also want to return extra functions to control the chart. These are going to be passed through toinitResultwhich can be accessed inonInitor viaSciChartSurfaceContext. See more below:
- Modify the
function App()React Component to return aSciChartReactelement as well as some extra buttons. These are added as nested elements inside theSciChartReactelement, meaning they can gain access toconst initResult = useContext(SciChartSurfaceContext)which contains thesciChartSurfaceas well as additional functions and props returned frominitChartlike theaddData,enableTooltipandgetTooltipEnabledfunctions.
Now run the app. You should see this behaviour

Accessing the Chart via Non-Nested Components
In the previous example, we accessed the initResult and functions to add data and control the chart state via buttons added as nested components to <SciChartReact>
What if you wanted to access the chart via non-nested components, e.g. buttons or a toolbar elsewhere in the application DOM? In this case you would need to store the initResult in a state somewhere to access the chart. Other than that, the principle is the same. initResult returns the sciChartSurface as well as any other objects or functions that you decide to return, so you can modify the chart data, state or appearance at any time.
Let's build on the previous tutorial by creating a simple toolbar to turn on/off zooming, panning and tooltip behaviours and show how to access initResult and modify chart state elsewhere in your app, outside the <SciChartReact/> component.
First start by modifying your initChart function. We're going to put this into a separate file, called initChart.js.
This code has been updated as follows. We've deleted the addData, enableTooltip, getTooltipEnabled functions returned from initChart. Instead, we've added a RolloverModifier, RubberBandXyZoomModifier, ZoomPanModifier and ZoomExtentsModifier to the chart. These are retuend from initChart and will be stored in the initResult which we can access later.
In this example, the isEnabled properties of rollover, zoompan and rubberbandzoom have been set to [false, false, true]. By default (unless you change modifier.executeOn, which allows you assign a different mouse-button to each modifier), each of these chart modifiers will trigger on the mouse-left button. One way of handling this is to selectively enable & disable zooming or panning behaviours, so in the second part of this tutorial
It's not possible for all three modifiers to be enabled at the same time, as each requires the mouse left button down event, so we're going to create a toolbar that updates the chart state to enable zoom, pan or tooltip independently.
Next, modify your App.jsx. We are going to create some toggle buttons to provide the toolbar behaviour & UI, and pass chart state between them.
In the code above, <SciChartReact/> calls initChart as before, but now we have an onInit callback. Here we store the initResult in a state object which you can see declared at the top of function App().
| App() |
Copy Code
|
|---|---|
function App() {
const [chartState, setChartState] = useState(null); // useState to store initResult once we have it
return (
<ChartContext.Provider value={{ chartState, setChartState }}> // ChartContext allows sharing of state object
<div className="App">
|
|
We've wrapped the whole React component in a <ChartContext/>, part of the React Context API, which allows you to share state across multiple nested components. At any point within this <ChartContext/> you can access chartState and setChartState which contains the intResult.
Next, we add three ToggleButton components and a <button> in order to provide the toolbar behaviour.
How the ToggleButton React Component works:
- It can access the
chartStatefrom theChartContextthat we created. - In the props passed in are a label and
modifierKey. It can usemodifierKeyto access the correct chartModifier fromchartState. - It returns a
<button>with a CSS class which is selected based on themodifier.isEnabledproperty - Last but not least,
handleClickupdates themodifier.isEnabledproperty. This also callssetChartState()to update the context and trigger other modifier toggle buttons to show as enabled/disabled.
One last thing, we extracted the CSS for the buttons into a stylesheet, styles.css.
To load this we had to install style-loader and css-loader packages and update webpack.config.js with a new rule and a test for *.css files. You can find the required changes below.
| npm |
Copy Code
|
|---|---|
npm install --save-dev style-loader css-loader |
|
| styles.css |
Copy Code
|
|---|---|
/* Toggle Button (Windows/macOS-style) */ .toggle-button { background-color: #e0e0e0; /* Default OFF color */ color: #333; border: none; padding: 8px 16px; border-radius: 2px; cursor: pointer; font-size: 14px; font-weight: bold; transition: background 0.2s, color 0.2s, box-shadow 0.2s; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2); outline: none; } .toggle-button.active { background-color: #007aff; /* ON color */ color: white; box-shadow: 0 2px 5px rgba(0, 122, 255, 0.5); } /* Normal Button (Zoom to Fit) */ .normal-button { background-color: #e0e0e0; /* Default OFF color */ color: #333; border: none; padding: 8px 16px; border-radius: 2px; cursor: pointer; font-size: 14px; font-weight: bold; transition: background 0.2s, color 0.2s, box-shadow 0.2s; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); outline: none; } .normal-button:hover { background-color: #55aaff; } |
|
| Example Title |
Copy Code
|
|---|---|
const path = require("path"); const CopyPlugin = require("copy-webpack-plugin"); const webpack = require("webpack"); module.exports = { mode: "production", entry: "./src/index.jsx", module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: { loader: "babel-loader", }, }, { test: /\.css$/, // Add CSS Loader Rule use: ["style-loader", "css-loader"], }, ], }, // ... |
|
Here's the completed tutorial output, with the three modifiers enabled/disabled via toolbar buttons.
