Full Chart using Builder API

Demonstrates how to use the Builder Api to create a Fully Configured 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    SciChartSurface,
3    build2DChart,
4    registerAllTypes,
5    EAxisType,
6    ELabelProviderType,
7    EAxisAlignment,
8    NumberRange,
9    ESeriesType,
10    GradientParams,
11    EPointMarkerType,
12    EAnnotationType,
13    ECoordinateMode,
14    EHorizontalAnchorPoint,
15    EVerticalAnchorPoint,
16    EChart2DModifierType,
17    Point,
18} from "scichart";
19import { appTheme } from "../../theme";
20
21// The Builder API is lean by default in v6. This chart is defined with type-strings only,
22// so register the built-in types before building it
23registerAllTypes();
24
25export const drawExample = async (rootElement: string | HTMLDivElement) => {
26    // Create a chart using the Builder-API, an api that allows defining a chart
27    // with javascript-objects or JSON
28    return await build2DChart(rootElement, {
29        // Set theme
30        surface: { theme: appTheme.SciChartJsTheme },
31        // Add XAxis
32        xAxes: [
33            {
34                type: EAxisType.CategoryAxis,
35                options: {
36                    axisTitle: "X Axis Title",
37                    labelProvider: {
38                        type: ELabelProviderType.Text,
39                        options: {
40                            labels: { 1: "one", 2: "two", 3: "three", 4: "four", 5: "five" },
41                        },
42                    },
43                },
44            },
45        ],
46        // Add multiple Y-Axis
47        yAxes: [
48            {
49                type: EAxisType.NumericAxis,
50                options: {
51                    id: "y1",
52                    axisTitle: "Left Axis",
53                    axisAlignment: EAxisAlignment.Left,
54                    visibleRange: new NumberRange(0, 20),
55                    zoomExtentsToInitialRange: true,
56                },
57            },
58            {
59                type: EAxisType.NumericAxis,
60                options: {
61                    id: "y2",
62                    axisTitle: "Right Axis",
63                    axisAlignment: EAxisAlignment.Right,
64                    visibleRange: new NumberRange(0, 800),
65                    labelPrecision: 0,
66                    zoomExtentsToInitialRange: true,
67                },
68            },
69        ],
70        // Add series. More than one can be set in an array
71        series: [
72            {
73                // each series has type, options in the builder-API
74                type: ESeriesType.SplineMountainSeries,
75                options: {
76                    yAxisId: "y1",
77                    stroke: appTheme.VividSkyBlue,
78                    strokeThickness: 5,
79                    fillLinearGradient: new GradientParams(new Point(0, 0), new Point(0, 1), [
80                        { color: appTheme.VividTeal, offset: 0.2 },
81                        { color: "Transparent", offset: 1 },
82                    ]),
83                },
84                xyData: { xValues: [1, 2, 3, 4, 5], yValues: [8, 6, 7, 2, 16] },
85            },
86            {
87                type: ESeriesType.BubbleSeries,
88                options: {
89                    yAxisId: "y2",
90                    pointMarker: {
91                        type: EPointMarkerType.Ellipse,
92                        options: {
93                            width: 100,
94                            height: 100,
95                            strokeThickness: 10,
96                            fill: appTheme.PaleSkyBlue,
97                            stroke: appTheme.VividSkyBlue,
98                        },
99                    },
100                },
101                xyzData: {
102                    xValues: [1, 2, 3, 4, 5],
103                    yValues: [320, 240, 280, 80, 640],
104                    zValues: [20, 40, 20, 30, 35],
105                },
106            },
107        ],
108        // Add annotations
109        annotations: [
110            {
111                type: EAnnotationType.SVGTextAnnotation,
112                options: { text: "Labels", yAxisId: "y1", x1: 0, y1: 10, yCoordinateMode: ECoordinateMode.DataValue },
113            },
114            {
115                type: EAnnotationType.SVGTextAnnotation,
116                options: {
117                    text: "can be placed",
118                    yAxisId: "y1",
119                    x1: 1,
120                    y1: 8,
121                    yCoordinateMode: ECoordinateMode.DataValue,
122                },
123            },
124            {
125                type: EAnnotationType.SVGTextAnnotation,
126                options: {
127                    text: "on the chart",
128                    yAxisId: "y1",
129                    x1: 2,
130                    y1: 9,
131                    yCoordinateMode: ECoordinateMode.DataValue,
132                },
133            },
134            {
135                type: EAnnotationType.SVGTextAnnotation,
136                options: {
137                    text: "Builder API Demo",
138                    x1: 0.5,
139                    y1: 0.5,
140                    opacity: 0.33,
141                    yCoordShift: -52,
142                    xCoordinateMode: ECoordinateMode.Relative,
143                    yCoordinateMode: ECoordinateMode.Relative,
144                    horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
145                    verticalAnchorPoint: EVerticalAnchorPoint.Center,
146                    fontSize: 36,
147                    fontWeight: "Bold",
148                },
149            },
150            {
151                type: EAnnotationType.SVGTextAnnotation,
152                options: {
153                    text: "Create SciChart charts with JSON Objects",
154                    x1: 0.5,
155                    y1: 0.5,
156                    yCoordShift: 0,
157                    opacity: 0.33,
158                    xCoordinateMode: ECoordinateMode.Relative,
159                    yCoordinateMode: ECoordinateMode.Relative,
160                    horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
161                    verticalAnchorPoint: EVerticalAnchorPoint.Center,
162                    fontSize: 24,
163                    fontWeight: "Bold",
164                },
165            },
166        ],
167        // Add interaction (zooming, panning, tooltips)
168        modifiers: [
169            {
170                type: EChart2DModifierType.Rollover,
171                options: {
172                    yAxisId: "y1",
173                    rolloverLineStroke: appTheme.VividTeal,
174                },
175            },
176            { type: EChart2DModifierType.MouseWheelZoom },
177            { type: EChart2DModifierType.ZoomExtents },
178        ],
179    });
180};
181

Full Chart using Builder API with Angular

Overview

This example demonstrates how to create a fully configured chart using the SciChart.js Builder API in an Angular environment. The chart is constructed via a JSON configuration that sets up axes, series, annotations, and interaction modifiers, providing a powerful and flexible way to produce high-performance visualizations.

Technical Implementation

The chart is initialized by defining its components in a structured JSON object. A categorical X Axis with custom text labels and two numeric Y Axes (one left-aligned and one right-aligned) are configured to manage different data scales. The Builder API is used to create complex series types, including a Spline Mountain Series with gradient fills and a Bubble Series with custom point markers. For a deep dive into how the Builder API functions, refer to the JavaScript Builder API Documentation. The asynchronous initialization of the chart also demonstrates best practices for handling complex rendering within Angular components.

Features and Capabilities

This example not only sets up a multi-axis chart but also includes advanced features such as custom annotations and interaction modifiers like rollover, mouse wheel zoom, and zoom extents. The annotations are configured using both data and relative coordinate modes, enabling precise placement of text and visual cues. Developers can explore the details of annotation configuration in the Tutorial 06 - Adding Annotations guide. Additionally, the example’s use of gradient fills and custom marker settings enhances the visual appeal of the chart.

Integration and Best Practices

Integrated within an Angular application, this example leverages the scichart-angular package to ensure seamless chart rendering and lifecycle management. By using asynchronous chart initialization, the implementation adheres to Angular’s component lifecycle best practices as outlined in the Component Lifecycle - Angular documentation. Furthermore, performance considerations are addressed to optimize rendering even in complex scenarios, with strategies inspired by the Performance Optimisation of JavaScript Applications & Charts guide.

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