Angular Chart with Multiple X Axes

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

Fullscreen

Edit

 Edit

Docs

drawExample.ts

index.tsx

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

Angular Chart with Multiple X Axes

Overview

This example demonstrates how to create a high performance Angular chart using SciChart.js with multiple X and Y axes. It showcases an implementation where two distinct sets of axes are defined — one pair for a blue series and one pair for an orange series — enabling the visualization of different data series on designated axes. The chart incorporates interactive modifiers and custom annotations to enhance interactivity and user experience.

Technical Implementation

The chart is initialized by creating a SciChartSurface with a custom theme and configuring two sets of numeric axes with unique identifiers. Two sets of NumericAxis are added: the primary (blue) pair are aligned using axis.alignment = EAxisAlignment.Bottom and EAxisAlignment.Left, and the secondary (orange) set aligned to the EAxisAlignment.Top and EAxisAlignment.Right. Data is generated through a random walk generator, and each data series is bound to its corresponding axis by setting a unique on axis.id. Series can then be registered on a specific axis by setting FastLineRenderableSeries.xAxisId and yAxisId. Interactive features such as axis drag, panning, and zooming are provided by modifiers such as YAxisDragModifier and XAxisDragModifier. Detailed configuration for setting up multiple axes is explained in the Tutorial 08 - Adding Multiple Axis documentation.

Features and Capabilities

This example provides several advanced features and customizations:

  • Multi-axis Configuration: Independent configuration of axis alignments and styles allows for flexible data representation.
  • Interactive Modifiers: Drag, pan, and zoom capabilities are implemented to enable dynamic chart interactivity. For more details on interactive modifiers, refer to the Tutorial 03 - Adding Zooming, Panning Behavior guide.
  • Custom Annotations: Both native and text annotations are added to the chart to provide contextual information about the data series.
  • High Performance Rendering: The chart leverages WebAssembly for rendering, achieving optimal performance with complex data sets. More performance details are available in the SciChart.js Performance Demo article.

Integration and Best Practices

In Angular applications, it is crucial to manage resources effectively. This example illustrates how to initialize the chart within an Angular context using the ScichartAngularComponent from scichart-angular and emphasizes proper cleanup of the SciChartSurface to prevent memory leaks, as outlined in the Memory Best Practices guide. The implementation also demonstrates best practices for binding data series to specific axes and customizing axis styling, ensuring that developers can tailor the chart to meet their unique project requirements.

angular Chart Examples & Demos

See Also: Chart Axis APIs (11 Demos)

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

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.