Angular Chart with Secondary Y Axes

Demonstrates how to create a Angular Chart with Secondary Y axis using SciChart.js, High Performance JavaScript Charts

Fullscreen

Edit

 Edit

Docs

drawExample.ts

angular.ts

RandomWalkGenerator.ts

theme.ts

Copy to clipboard
Minimise
Fullscreen
1import {
2    EAxisAlignment,
3    ECoordinateMode,
4    EHorizontalAnchorPoint,
5    ELabelAlignment,
6    EllipsePointMarker,
7    EMultiLineAlignment,
8    ENumericFormat,
9    EVerticalAnchorPoint,
10    EWrapTo,
11    FastLineRenderableSeries,
12    MouseWheelZoomModifier,
13    NativeTextAnnotation,
14    NumberRange,
15    NumericAxis,
16    SciChartSurface,
17    TextAnnotation,
18    XAxisDragModifier,
19    XyDataSeries,
20    YAxisDragModifier,
21    ZoomExtentsModifier,
22    ZoomPanModifier,
23} from "scichart";
24import { RandomWalkGenerator } from "../../../ExampleData/RandomWalkGenerator";
25import { appTheme } from "../../../theme";
26
27const ID_Y_AXIS_2 = "yAxis2";
28
29export const drawExample = async (rootElement: string | HTMLDivElement) => {
30    // Create the SciChartSurface with theme
31    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
32        theme: appTheme.SciChartJsTheme,
33    });
34
35    // Add a primary X,Y Axis pair
36    sciChartSurface.xAxes.add(
37        new NumericAxis(wasmContext, {
38            axisAlignment: EAxisAlignment.Bottom,
39            axisTitle: "Shared X Axis",
40            axisTitleStyle: {
41                color: appTheme.VividGreen,
42                fontSize: 30,
43            },
44            labelStyle: {
45                color: appTheme.VividGreen,
46            },
47            backgroundColor: appTheme.VividGreen + "22",
48            axisBorder: {
49                borderTop: 1,
50                color: appTheme.VividGreen,
51            },
52        })
53    );
54
55    sciChartSurface.yAxes.add(
56        new NumericAxis(wasmContext, {
57            axisAlignment: EAxisAlignment.Left,
58            axisTitle: "Y Axis Left",
59            axisTitleStyle: {
60                color: appTheme.VividSkyBlue,
61                fontSize: 30,
62            },
63            labelStyle: {
64                color: appTheme.VividSkyBlue,
65            },
66            growBy: new NumberRange(0.1, 0.2),
67            backgroundColor: appTheme.VividSkyBlue + "22",
68            axisBorder: {
69                borderRight: 1,
70                color: appTheme.VividSkyBlue,
71            },
72        })
73    );
74
75    // generate some data
76    let data = new RandomWalkGenerator().Seed(7331).getRandomWalkSeries(100);
77
78    // Add the first line series on the primary X,Y axis
79    // This occurs be default as FastLineRenderableSeries XAxisId and YAxisId are set to a default value
80    sciChartSurface.renderableSeries.add(
81        new FastLineRenderableSeries(wasmContext, {
82            stroke: appTheme.VividSkyBlue,
83            strokeThickness: 3,
84            dataSeries: new XyDataSeries(wasmContext, { xValues: data.xValues, yValues: data.yValues }),
85            pointMarker: new EllipsePointMarker(wasmContext, {
86                width: 5,
87                height: 5,
88                fill: appTheme.VividGreen,
89                stroke: appTheme.VividGreen,
90            }),
91        })
92    );
93
94    // Add a secondary Y Axis
95    sciChartSurface.yAxes.add(
96        new NumericAxis(wasmContext, {
97            id: ID_Y_AXIS_2,
98            axisTitleStyle: {
99                color: appTheme.VividOrange,
100                fontSize: 30,
101            },
102            labelStyle: {
103                color: appTheme.VividOrange,
104                alignment: ELabelAlignment.Right,
105            },
106            axisAlignment: EAxisAlignment.Right,
107            axisTitle: "Y Axis Right",
108            labelFormat: ENumericFormat.Decimal,
109            labelPrecision: 2,
110            growBy: new NumberRange(0.1, 0.2),
111            backgroundColor: appTheme.VividOrange + "22",
112            axisBorder: {
113                borderLeft: 1,
114                color: appTheme.VividOrange,
115            },
116        })
117    );
118
119    // Create some data & series for that axis
120    data = new RandomWalkGenerator().Seed(1209).getRandomWalkSeries(100);
121
122    // The second line series we specify X/Y axis ids to bind this to the correct axis
123    sciChartSurface.renderableSeries.add(
124        new FastLineRenderableSeries(wasmContext, {
125            stroke: appTheme.VividOrange,
126            strokeThickness: 3,
127            dataSeries: new XyDataSeries(wasmContext, { xValues: data.xValues, yValues: data.yValues }),
128            yAxisId: ID_Y_AXIS_2,
129            pointMarker: new EllipsePointMarker(wasmContext, {
130                width: 5,
131                height: 5,
132                fill: appTheme.VividOrange,
133                stroke: appTheme.VividOrange,
134            }),
135        })
136    );
137
138    // Optional: Add some interactivity modifiers to enable zooming and panning
139    sciChartSurface.chartModifiers.add(
140        new YAxisDragModifier(),
141        new XAxisDragModifier(),
142        new ZoomPanModifier({ enableZoom: true }),
143        new MouseWheelZoomModifier(),
144        new ZoomExtentsModifier()
145    );
146
147    // Add a title over the chart with information
148    sciChartSurface.annotations.add(
149        new NativeTextAnnotation({
150            x1: 0.01,
151            y1: 0.01,
152            xCoordinateMode: ECoordinateMode.Relative,
153            yCoordinateMode: ECoordinateMode.Relative,
154            horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
155            verticalAnchorPoint: EVerticalAnchorPoint.Top,
156            fontSize: 18,
157            opacity: 0.55,
158            textColor: appTheme.ForegroundColor,
159            text: "SciChart.js supports unlimited X,Y axis. Drag an axis to see the series scale",
160            wrapTo: EWrapTo.ViewRect,
161            multiLineAlignment: EMultiLineAlignment.Left,
162        })
163    );
164
165    sciChartSurface.annotations.add(
166        new TextAnnotation({
167            x1: 10,
168            y1: 1.1,
169            fontSize: 18,
170            textColor: appTheme.VividSkyBlue,
171            horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
172            verticalAnchorPoint: EVerticalAnchorPoint.Bottom,
173            text: "Blue series is bound to the Shared X-Axis, and Left Y-Axis",
174        })
175    );
176
177    // Note annotations need X,Y Axis ID as well in multi-axis scenarios
178    sciChartSurface.annotations.add(
179        new TextAnnotation({
180            x1: 5,
181            y1: -1.2,
182            fontSize: 18,
183            textColor: appTheme.VividOrange,
184            yAxisId: ID_Y_AXIS_2,
185            text: "Orange series is bound to the Shared X-Axis, and Right Y-Axis",
186        })
187    );
188
189    return { sciChartSurface, wasmContext };
190};
191

Angular Chart with Secondary Y Axes

Overview

This example demonstrates how to integrate SciChart.js into an Angular standalone component using the ScichartAngularComponent. The implementation creates a high-performance chart featuring a shared X axis and two distinct Y axes (one on the left and a secondary one on the right). This dual-axis setup enables binding different data series with unique styling, making it ideal for applications requiring multi-axis data visualization.

Technical Implementation

Within an Angular environment, the chart is initialized by calling SciChartSurface.create() with a WASM context to ensure optimal rendering performance. The primary X and left Y axes are defined first, and a secondary right Y axis is then added and configured with its own settings. Data series are bound to the appropriate axes via axis.id and the FastLineRenderableSeries.yAxisId property, while custom point markers and annotations provide additional visual context. Interactive modifiers, such as YAxisDragModifier, XAxisDragModifier, ZoomPanModifier, MouseWheelZoomModifier, and ZoomExtentsModifier, are incorporated to facilitate intuitive chart interactivity. For further details on configuring multiple axes, refer to the Tutorial 08 - Adding Multiple Axis resource.

Features and Capabilities

This example showcases several advanced features including real-time data visualization, customized axis styling, and the use of intuitive interactive chart controls. Custom point markers are created with the EllipsePointMarker, and annotations (both native and text based) add informative overlays to the chart. The usage of a performance-optimized WASM context underlines the example's capability to handle complex data sets efficiently. Additional customization options are available through detailed configuration settings as described in the Secondary and Multiple Axis documentation.

Integration and Best Practices

Integrating SciChart.js within Angular is streamlined by utilizing the ScichartAngularComponent, making the setup process straightforward. Developers are encouraged to consult the Getting Started with SciChart JS guide and the scichart-angular package readme for best practices on projects. The example demonstrates efficient use of interactive chart modifiers and performance optimization techniques, ensuring that resource management and user experience remain a priority. For example, the use of YXAxisDragModifier illustrates how interactive controls can enhance chart functionality in an Angular context.

angular Chart Examples & Demos

See Also: Chart Axis APIs (11 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 Vertical Charts | Angular Charts | SciChart.js Demo

Angular Vertical Charts

Demonstrates alignment of Axis to create a vertical chart with SciChart.js - JavaScript Charts.

Angular Chart with Central Axes | Angular Charts | SciChart.js

Angular Chart with Central Axes

Demonstrates Central Axes on a Angular Chart using SciChart.js. SciChart supports unlimited left, right, top, bottom X, Y axis with configurable layout

Angular Chart with Static X Axis | Angular Charts | SciChart.js

Angular Chart with Static X Axis

Demonstrates isStaticAxis on a Angular Chart using SciChart.js.

Angular Chart with Vertically Stacked Axes | SciChart.js

Angular Chart with Vertically Stacked Axes

Demonstrates Vertically Stacked Axes on a Angular Chart using SciChart.js, allowing data to overlap

Angular Chart with Logarithmic Axis Example | SciChart.js

Angular Chart with Logarithmic Axis Example

Demonstrates Logarithmic Axis on a Angular Chart using SciChart.js. SciChart supports logarithmic axis with scientific or engineering notation and positive and negative values

NEW!
Angular Chart with DiscontinuousDateAxis  Comparison

DiscontinuousDateAxis Comparison with Angular

NEW!
Angular Chart with BaseValue Axes | SciChart.js Demo

Angular Chart with BaseValue Axes

Demonstrates BaseValue Axes on an Angular Chart using SciChart.js to create non-linear and custom-scaled axes

Draw Angular Chart Behind Axis | Angular Charts | SciChart.js

Draw Angular Chart Behind Axis

Demonstrates the option of the transparent Axes customization on a Angular Chart using SciChart.js.

Axis Types | Angular Charts | SciChart.js Demo

Axis Types

Demonstrates how to use arbitrary text for axis labels, rather than formatted data values, using the new TextLabelProvider

Angular Chart Axis Layout Options | SciChart.js Demo

Angular Chart Axis Layout Options

Demonstrates outer, inner, central and stacked axes, and use of axis alignment to create vertical charts

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