Zoom and Pan a Realtime Angular Chart

Zoom the real-time chart below by dragging on the surface. Right click and drag to pan. Then double-click to reset zoom and start automatically scrolling again.

Fullscreen

Edit

 Edit

Docs

drawExample.ts

angular.ts

theme.ts

Copy to clipboard
Minimise
Fullscreen
1import {
2    EExecuteOn,
3    EllipsePointMarker,
4    EZoomState,
5    FastLineRenderableSeries,
6    NumberRange,
7    NumericAxis,
8    RubberBandXyZoomModifier,
9    SciChartSurface,
10    XyDataSeries,
11    XyScatterRenderableSeries,
12    ZoomExtentsModifier,
13    ZoomPanModifier,
14} from "scichart";
15
16import { appTheme } from "../../../theme";
17
18export const drawExample = async (rootElement: string | HTMLDivElement) => {
19    // Create the SciChartSurface in the div 'scichart-root'
20    // The SciChartSurface, and webassembly context 'wasmContext' are paired. This wasmContext
21    // instance must be passed to other types that exist on the same surface.
22    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
23        theme: appTheme.SciChartJsTheme,
24    });
25
26    // Create an X,Y Axis and add to the chart
27    const xAxis = new NumericAxis(wasmContext, { labelPrecision: 0 });
28    const yAxis = new NumericAxis(wasmContext);
29
30    sciChartSurface.xAxes.add(xAxis);
31    sciChartSurface.yAxes.add(yAxis);
32
33    // Create a Scatter series, and Line series and add to chart
34    const scatterSeries = new XyScatterRenderableSeries(wasmContext, {
35        pointMarker: new EllipsePointMarker(wasmContext, { width: 7, height: 7, fill: "White", stroke: "SteelBlue" }),
36    });
37    const lineSeries = new FastLineRenderableSeries(wasmContext, { stroke: "#4083B7", strokeThickness: 2 });
38    sciChartSurface.renderableSeries.add(lineSeries, scatterSeries);
39
40    const initialDataPointsCount = 1000;
41
42    // if a required size of data series is known or it is expected to grow,
43    // setting a capacity value may give some efficiency boost
44    const initialCapacity = 2000;
45
46    // Create and populate some XyDataSeries with static data
47    // Note: you can pass xValues, yValues arrays to constructors, and you can use appendRange for bigger datasets
48    const scatterData = new XyDataSeries(wasmContext, { dataSeriesName: "Cos(x)", capacity: initialCapacity });
49    const lineData = new XyDataSeries(wasmContext, { dataSeriesName: "Sin(x)", capacity: initialCapacity });
50
51    for (let i = 0; i < initialDataPointsCount; i++) {
52        lineData.append(i, Math.sin(i * 0.05));
53        scatterData.append(i, Math.cos(i * 0.05));
54    }
55
56    // Assign these dataseries to the line/scatter renderableseries
57    scatterSeries.dataSeries = scatterData;
58    lineSeries.dataSeries = lineData;
59
60    //  We disable ZoomExtends animation
61    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier({ isAnimated: false }));
62    // Realtime zooming example
63    sciChartSurface.chartModifiers.add(new RubberBandXyZoomModifier());
64    // Realtime panning example
65    sciChartSurface.chartModifiers.add(
66        new ZoomPanModifier({ executeCondition: { button: EExecuteOn.MouseRightButton } })
67    );
68
69    // Part 2: Appending data in realtime
70    //
71    let timerId: NodeJS.Timeout;
72
73    const updateDataFunc = () => {
74        // Append another data-point to the chart. We use dataSeries.count()
75        // to determine the current length before appending
76        // NOTE: sometimes appending data points in bulk with appendRange can be more efficient
77        const i = lineData.count();
78        lineData.append(i, Math.sin(i * 0.05));
79        scatterData.append(i, Math.cos(i * 0.05));
80
81        // If the zoomState is not UserZooming, then we are viewing the extents of the data
82        // In this case, we want to scroll the chart by setting visibleRange = NumberRange(i-1000, i)
83        if (sciChartSurface.zoomState !== EZoomState.UserZooming) {
84            xAxis.visibleRange = new NumberRange(i - 1000, i);
85        }
86    };
87
88    const startUpdate = () => {
89        // Repeat at 60Hz
90        timerId = setInterval(updateDataFunc, 16);
91        // Warning, this will repeat forever, it's not best practice!
92    };
93
94    const stopUpdate = () => {
95        clearTimeout(timerId);
96    };
97
98    return { sciChartSurface, controls: { startUpdate, stopUpdate } };
99};
100

Realtime Chart Zoom and Pan - Angular Example

Overview

This example demonstrates a real-time chart in an Angular application using SciChart.js. The purpose is to display continuously updating sine and cosine data while allowing the user to interact with the chart through zooming and panning.

Technical Implementation

The implementation uses a standalone Angular component that integrates the scichart-angular for chart integration to Angular. The chart is asynchronously initialized via the drawExample function, which creates a SciChartSurface with an associated WebAssembly context. It sets up numeric axes, a line series, and a scatter series, and assigns interactive modifiers such as the RubberBandXyZoomModifier for zooming and the ZoomPanModifier for right-click panning. Real-time data updates are implemented using setInterval at approximately 60Hz, reflecting best practices discussed in the Adding Realtime Updates tutorial.

Features and Capabilities

The example offers advanced real-time capabilities by dynamically appending new data points to both the sine (line) and cosine (scatter) series. It automatically adjusts the visibleRange of the x-axis when the user is not actively zooming (denoted by zoomState property), ensuring smooth scrolling but allowing zooming to freeze scrolling.

Integration and Best Practices

The Angular integration leverages input bindings, the chart initialization function is passed to the ScichartAngularComponent, enabling asynchronous creation of the chart. Additionally, careful attention is paid to lifecycle management, ensuring appropriate resource cleanup and efficient real-time rendering. This example serves as a robust template for integrating high-performance, interactive charts within Angular applications using SciChart.js.

angular Chart Examples & Demos

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

Angular Chart with Multiple X Axes | SciChart.js Demo

Angular Chart with Multiple X Axes

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

Angular Chart with Secondary Y Axes | SciChart.js Demo

Angular Chart with Secondary Y Axes

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

Drag Angular Chart Axis to Scale or Pan | SciChart.js

Drag Angular Chart Axis to Scale or Pan

Demonstrates how to Zoom, Scale or Pan individual Axis on a Angular Chart with SciChart.js AxisDragModifiers

Zoom and Pan with Angular Chart multiple Modifiers | SciChart

Zoom and Pan with Angular Chart multiple Modifiers

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

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

Zoom and Pan with Overview Chart

Demonstrates how to zoom and pan with an Overview Chart

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

Virtualized Angular Charts: Load Data on Zoom/Pan

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

Angular Polar Modifiers | Polar Interactivity Modifiers

Angular 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 | Angular Charts | SciChart.js Demo

High Precision Date Axis

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

NEW!
Angular Overview for SubCharts with Range Selection

Angular Overview for SubCharts with Range Selection

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

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