Drag React Chart Axis to Scale or Pan

Demonstrates how to scale or pan the Axis on a React Chart using SciChart.js, High Performance JavaScript Charts

Fullscreen

Edit

 Edit

Docs

drawExample.ts

index.tsx

theme.ts

Copy to clipboard
Minimise
Fullscreen
1import {
2    EAxisAlignment,
3    EColor,
4    ELabelAlignment,
5    ENumericFormat,
6    FastLineRenderableSeries,
7    NumericAxis,
8    SciChartSurface,
9    XyDataSeries,
10    ZoomExtentsModifier,
11    EDragMode,
12    YAxisDragModifier,
13    XAxisDragModifier,
14    SciChartJsNavyTheme,
15} from "scichart";
16
17export const drawExample = async (rootElement: string | HTMLDivElement) => {
18    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
19        theme: new SciChartJsNavyTheme(),
20    });
21
22    const ID_X_AXIS_2 = "xAxis2";
23    const ID_Y_AXIS_2 = "yAxis2";
24
25    const setXAxis1 = () => {
26        const xAxis = new NumericAxis(wasmContext);
27        xAxis.axisAlignment = EAxisAlignment.Top;
28        xAxis.axisTitle = "Drag the X-Axis to Pan";
29        xAxis.labelProvider.numericFormat = ENumericFormat.Decimal;
30        xAxis.labelProvider.precision = 0;
31        sciChartSurface.xAxes.add(xAxis);
32    };
33
34    const setXAxis2 = () => {
35        const xAxis = new NumericAxis(wasmContext, {
36            id: ID_X_AXIS_2,
37        });
38        xAxis.axisAlignment = EAxisAlignment.Bottom;
39        xAxis.axisTitle = "Drag the X-Axis to Pan";
40        xAxis.labelProvider.numericFormat = ENumericFormat.Decimal;
41        xAxis.labelProvider.precision = 0;
42        sciChartSurface.xAxes.add(xAxis);
43    };
44
45    const setYAxis1 = () => {
46        const yAxis = new NumericAxis(wasmContext);
47        yAxis.axisAlignment = EAxisAlignment.Left;
48        yAxis.axisTitle = "Drag the Y-Axis to Scale";
49        yAxis.labelProvider.numericFormat = ENumericFormat.Decimal;
50        yAxis.labelProvider.precision = 0;
51        sciChartSurface.yAxes.add(yAxis);
52    };
53
54    const setYAxis2 = () => {
55        const yAxis = new NumericAxis(wasmContext, {
56            id: ID_Y_AXIS_2,
57        });
58        yAxis.axisAlignment = EAxisAlignment.Right;
59        yAxis.axisTitle = "Drag the Y-Axis to Scale";
60        yAxis.labelProvider.numericFormat = ENumericFormat.Decimal;
61        yAxis.labelProvider.precision = 0;
62        yAxis.labelStyle = { alignment: ELabelAlignment.Right };
63        sciChartSurface.yAxes.add(yAxis);
64    };
65
66    const setSeries1 = () => {
67        const lineSeries = new FastLineRenderableSeries(wasmContext, { stroke: EColor.Red });
68        lineSeries.strokeThickness = 3;
69        sciChartSurface.renderableSeries.add(lineSeries);
70        const lineData = new XyDataSeries(wasmContext);
71
72        // Define line data
73        const iStep = 20;
74        const fAmpltude = 100.0;
75        const fFrequency = 1.0;
76        for (let i = 0; i < 500 + iStep; i += iStep) {
77            lineData.append(i, Math.sin((fFrequency * i * Math.PI) / 180.0) * fAmpltude);
78        }
79
80        lineSeries.dataSeries = lineData;
81    };
82
83    const setSeries2 = () => {
84        const lineSeries = new FastLineRenderableSeries(wasmContext, { stroke: EColor.Purple });
85        lineSeries.xAxisId = ID_X_AXIS_2;
86        lineSeries.yAxisId = ID_Y_AXIS_2;
87        lineSeries.strokeThickness = 3;
88        sciChartSurface.renderableSeries.add(lineSeries);
89        const lineData = new XyDataSeries(wasmContext);
90
91        // Define line data
92        const iStep = 10;
93        const fAmpltude = 200.0;
94        const fFrequency = 1.5;
95        for (let i = 0; i < 1.5 * 500 + iStep; i += iStep) {
96            lineData.append(i, Math.sin((fFrequency * (i - 100) * Math.PI) / 180.0) * fAmpltude);
97        }
98
99        lineSeries.dataSeries = lineData;
100    };
101
102    setXAxis1();
103    setXAxis2();
104    setYAxis1();
105    setYAxis2();
106
107    setSeries1();
108    setSeries2();
109
110    // Add modifiers to enable YAxis and XAxis drag with dragMode as scale or pan
111    sciChartSurface.chartModifiers.add(
112        new XAxisDragModifier({ dragMode: EDragMode.Panning }),
113        new YAxisDragModifier({ dragMode: EDragMode.Scaling })
114    );
115
116    // Add a modifier to zoom to fit on double-click
117    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
118
119    sciChartSurface.zoomExtents();
120
121    return { sciChartSurface };
122};
123

Drag Axis To Scale - React

Overview

This example demonstrates how to implement interactive axis scaling and panning within a React application using SciChart.js. The chart is configured with two X-axes and two Y-axes, where the top and bottom X-axes enable panning and the left and right Y-axes support scaling. The high-performance WebGL rendering of SciChart.js ensures a smooth, responsive experience.

Technical Implementation

In this example the chart is initialized using the <SciChartReact/> component, which passes a custom draw function that creates a SciChartSurface, multiple NumericAxis instances, FastLineRenderableSeries, and XyDataSeries. YAxisDragModifier and XAxisDragModifier instances are added to allow for panning on the X-Axis and scaling on the Y-Axis. Customization of axis alignment, numeric formatting, and theming with SciChartJsNavyTheme enables a tailored interaction experience. A ZoomExtentsModifier is also added to reset the view and zoom-to-fit via double-click.

Features and Capabilities

The chart demonstrates several key features: real-time axis interaction through dragging to pan and scale, a multi-axis configuration with independent axis settings, and dual renderable series showcasing dynamic data generation. The integration of FastLineRenderableSeries and XyDataSeries emphasizes efficient rendering capabilities, aligning with performance optimization strategies outlined in the React Charts with SciChart.js guide.

Integration and Best Practices

By leveraging <SciChartReact/> for initialization, this example follows best practices for embedding SciChart.js into React applications. The implementation emphasizes proper resource management and lifecycle considerations consistent with recommendations found in Creating a SciChart React Component from the Ground Up. Developers are encouraged to explore additional interaction and performance optimization techniques to further enhance real-time charting experiences in React. This comprehensive example provides a solid foundation for building interactive, high-performance charts with SciChart.js in the React ecosystem.

react Chart Examples & Demos

See Also: Zoom and Pan a Chart (9 Demos)

React Chart with Multiple X Axes | React Charts | SciChart.js

React Chart with Multiple X Axes

Demonstrates Multiple X & Y Axis on a React Chart using SciChart.js. SciChart supports unlimited left, right, top, bottom X, Y axis with configurable alignment and individual zooming, panning

React Chart with Secondary Y Axes | SciChart.js Demo

React Chart with Secondary Y Axes

Demonstrates Secondary Y Axis on a React Chart using SciChart.js. SciChart supports unlimited, multiple left, right, top, bottom X, Y axis with configurable alignment and individual zooming, panning

Zoom and Pan a Realtime React Chart | SciChart.js Demo

Zoom and Pan a Realtime React Chart

Demonstrates how to zoom and pan a realtime React Chart while it is updating, with SciChart.js ZoomState API

Zoom and Pan with React Chart multiple Modifiers | SciChart

Zoom and Pan with React Chart multiple Modifiers

Demonstrates how to use multiple Zoom and Pan Modifiers on a React Chart with SciChart.js

Zoom and Pan with Overview Chart | React Charts | SciChart.js

Zoom and Pan with Overview Chart

Demonstrates how to zoom and pan with an Overview Chart

Virtualized React Charts: Load Data on Zoom/Pan | SciChart

Virtualized React Charts: Load Data on Zoom/Pan

shows how to load data on zoom/pan and how to create an overview chart for this case.

React Polar Modifiers | Polar Interactivity Modifiers

React Polar Modifiers | Polar Interactivity Modifiers Demo

Explore SciChart's Polar Interactivity Modifiers including zooming, panning, and cursor tracking. Try the demo to trial the Polar Chart Behavior Modifiers.

NEW!
High Precision Date Axis | React Charts | SciChart.js Demo

High Precision Date Axis

Demonstrates 64-bit precision Date Axis in SciChart.js handling Nanoseconds to Billions of Years

NEW!
React Overview for SubCharts with Range Selection | SciChart

React Overview for SubCharts with Range Selection

Demonstrates how to build synchronized multi-panel charts with an overview range selector using SciChart.js in React

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL.