JavaScript Order of Rendering Example

Demonstrates the new Ordered Rendering feature in SciChart.js Javascript charts which allows for full control of the draw order of series and annotations

Also Known As: Z-Index, Z-Ordering, Draw Order, Layering

Fullscreen

Edit

 Edit

Docs

drawExample.ts

index.html

vanilla.ts

theme.ts

Copy to clipboard
Minimise
Fullscreen
1import {
2    MouseWheelZoomModifier,
3    ZoomExtentsModifier,
4    ZoomPanModifier,
5    XyyDataSeries,
6    NumericAxis,
7    FastBandRenderableSeries,
8    SciChartSurface,
9    NumberRange,
10    SweepAnimation,
11    NativeTextAnnotation,
12    EWrapTo,
13    GenericAnimation,
14    EDefaultRenderLayer,
15    DoubleAnimator,
16    BoxAnnotation,
17    ECoordinateMode,
18} from "scichart";
19
20import { appTheme } from "../../../theme";
21
22export const drawExample = async (rootElement: string | HTMLDivElement) => {
23    // Create a SciChartSurface
24    const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
25        theme: appTheme.SciChartJsTheme,
26    });
27
28    // Add an XAxis, YAxis
29    sciChartSurface.xAxes.add(new NumericAxis(wasmContext));
30    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.1, 0.1) }));
31
32    // Create some data for the example. We need X, Y and Y1 values
33    const xValues = [];
34    const yValues = [];
35    const y1Values = [];
36    const POINTS = 1000;
37    const STEP = (3 * Math.PI) / POINTS;
38    for (let i = 0; i <= 1000; i++) {
39        const k = 1 - i / 2000;
40        xValues.push(i / 100);
41        yValues.push(Math.sin(i * STEP) * k * 0.7);
42        y1Values.push(Math.cos(i * STEP) * k);
43    }
44
45    // Create the band series and add to the chart
46    // The bandseries requires a special dataseries type called XyyDataSeries with X,Y and Y1 values
47
48    const band1 = new FastBandRenderableSeries(wasmContext, {
49        dataSeries: new XyyDataSeries(wasmContext, { xValues, yValues, y1Values }),
50        strokeThickness: 3,
51        fill: appTheme.MutedOrange,
52        fillY1: appTheme.MutedBlue,
53        opacity: 0.7,
54        stroke: appTheme.MutedOrange,
55        strokeY1: appTheme.MutedBlue,
56        animation: new SweepAnimation({ duration: 800 }),
57        renderLayer: EDefaultRenderLayer.SeriesLayer, // default layer for series, not actually needed here
58    });
59
60    const band2 = new FastBandRenderableSeries(wasmContext, {
61        dataSeries: new XyyDataSeries(wasmContext, {
62            xValues,
63            yValues: yValues.map((y) => y - 0.5),
64            y1Values: y1Values.map((y) => y - 0.5),
65        }),
66        strokeThickness: 3,
67        fill: appTheme.MutedSkyBlue,
68        fillY1: appTheme.MutedPink,
69        opacity: 0.7,
70        stroke: appTheme.MutedSkyBlue,
71        strokeY1: appTheme.MutedPink,
72        animation: new SweepAnimation({ duration: 800 }),
73        renderLayer: EDefaultRenderLayer.SeriesLayer, // default layer for series, not actually needed here
74    });
75
76    const band3 = new FastBandRenderableSeries(wasmContext, {
77        dataSeries: new XyyDataSeries(wasmContext, {
78            xValues,
79            yValues: yValues.map((y) => y - 1),
80            y1Values: y1Values.map((y) => y - 1),
81        }),
82        strokeThickness: 3,
83        fill: appTheme.MutedTeal,
84        fillY1: appTheme.MutedPurple,
85        opacity: 0.7,
86        stroke: appTheme.MutedTeal,
87        strokeY1: appTheme.MutedPurple,
88        animation: new SweepAnimation({ duration: 800 }),
89        renderLayer: EDefaultRenderLayer.SeriesLayer, // default layer for series, not actually needed here
90    });
91
92    const label1 = new NativeTextAnnotation({
93        renderNextTo: { renderable: band1, offset: 0 },
94        text: "1.",
95        fontSize: 20,
96        x1: 0.05,
97        xCoordinateMode: ECoordinateMode.Relative,
98        y1: 0.65,
99        textColor: appTheme.VividBlue,
100        wrapTo: EWrapTo.Annotation,
101        renderLayer: EDefaultRenderLayer.SeriesLayer,
102        // drawImmediate: true,
103        renderOrder: 0,
104    });
105
106    const label2 = new NativeTextAnnotation({
107        renderNextTo: { renderable: band2, offset: 0 },
108        text: "2.",
109        fontSize: 20,
110        x1: 0.05,
111        xCoordinateMode: ECoordinateMode.Relative,
112        y1: 0.14,
113        textColor: appTheme.VividBlue,
114        wrapTo: EWrapTo.Annotation,
115        renderLayer: EDefaultRenderLayer.SeriesLayer,
116        // drawImmediate: true,
117        renderOrder: 0,
118    });
119
120    const label3 = new NativeTextAnnotation({
121        renderNextTo: { renderable: band3, offset: 0 },
122        text: "3.",
123        fontSize: 20,
124        x1: 0.05,
125        xCoordinateMode: ECoordinateMode.Relative,
126        y1: -0.37,
127        textColor: appTheme.VividBlue,
128        wrapTo: EWrapTo.Annotation,
129        renderLayer: EDefaultRenderLayer.SeriesLayer,
130        // drawImmediate: true,
131        renderOrder: 0,
132    });
133
134    const nativeText = new NativeTextAnnotation({
135        text: "These Annotations are rendered on the series layer so they can render between series",
136        fontSize: 18,
137        x1: 4,
138        x2: 7,
139        y1: 0.2,
140        textColor: appTheme.ForegroundColor,
141        wrapTo: EWrapTo.Annotation,
142        renderLayer: EDefaultRenderLayer.SeriesLayer,
143        // drawImmediate: true,
144        renderOrder: 0,
145    });
146
147    const box = new BoxAnnotation({
148        x1: 3.9,
149        x2: 7.3,
150        y1: 0.23,
151        y2: -0.5,
152        stroke: appTheme.ForegroundColor,
153        fill: appTheme.MutedBlue,
154        opacity: 0.8,
155        renderNextTo: { renderable: nativeText, offset: -0.1 },
156    });
157
158    const annotationAnimation = new GenericAnimation<number>({
159        from: 0.5,
160        to: 4,
161        onAnimate: (from: number, to: number, progress: number) => {
162            const annotationRO = DoubleAnimator.interpolate(from, to, progress);
163            nativeText.text = `Render Order ${annotationRO.toFixed(
164                1
165            )}\n\nThese Annotations are rendered on the series layer so they can render between series`;
166            //nativeText.x1 += annotationRO;
167            //nativeText.x2 += annotationRO;
168            //box.x1 = nativeText.x1;
169            //box.x2 = nativeText.x2;
170            nativeText.setRenderOrder(annotationRO);
171        },
172        duration: 4000,
173        delay: 0,
174        onCompleted: () => {
175            let temp = annotationAnimation.from;
176            annotationAnimation.from = annotationAnimation.to;
177            annotationAnimation.to = temp;
178            annotationAnimation.reset();
179        },
180    });
181
182    sciChartSurface.addAnimation(annotationAnimation);
183
184    sciChartSurface.annotations.add(nativeText, box, label1, label2, label3);
185
186    sciChartSurface.renderableSeries.add(band1, band2, band3);
187
188    // Optional: Add some interactivity modifiers
189    sciChartSurface.chartModifiers.add(
190        new ZoomExtentsModifier(),
191        new ZoomPanModifier({ enableZoom: true }),
192        new MouseWheelZoomModifier()
193    );
194
195    const changeOrder = (order: boolean) => {
196        if (order) {
197            // band1.setRenderLayer(EDefaultRenderLayer.AnnotationsBelowSeriesLayer);
198            // band2.setRenderLayer(EDefaultRenderLayer.Foreground);
199            band1.setRenderOrder(1);
200            band2.setRenderOrder(2);
201            band3.setRenderOrder(3);
202            label1.text = "1.";
203            label2.text = "2.";
204            label3.text = "3.";
205        } else {
206            band1.setRenderOrder(3);
207            band2.setRenderOrder(2);
208            band3.setRenderOrder(1);
209            label1.text = "3.";
210            label2.text = "2.";
211            label3.text = "1.";
212        }
213    };
214
215    sciChartSurface.zoomExtents();
216    return { wasmContext, sciChartSurface, changeOrder };
217};
218

Ordered Rendering Chart - JavaScript

Overview

This example demonstrates ordered rendering capabilities in SciChart.js, showing how to dynamically control the draw order of series and annotations. It features three band series with labels that can be reordered via a programmatic API.

Technical Implementation

The chart uses FastBandRenderableSeries with XyyDataSeries to create filled bands. Annotations are placed on the EDefaultRenderLayer.SeriesLayer to render between series. The GenericAnimation API animates renderOrder values from 0.5 to 4.0.

Features and Capabilities

Key features include dynamic reordering via setRenderOrder, relative positioning with ECoordinateMode.Relative, and annotation-series binding through renderNextTo. The example includes standard interactivity modifiers like ZoomPanModifier.

Integration and Best Practices

The vanilla JS implementation shows proper async initialization and cleanup. For production use, consider the Memory Management Guide when implementing similar ordered rendering scenarios.

javascript Chart Examples & Demos

See Also: Charts added in v4 (16 Demos)

JavaScript Histogram Chart | Javascript Charts | SciChart.js

JavaScript Histogram Chart

Create a JavaScript Histogram Chart with custom texture fills and patterns. Try the SciChart.js library for seamless integration today.

JavaScript Gantt Chart | Javascript Charts | SciChart.js Demo

JavaScript Gantt Chart Example

Build a JavaScript Gantt Chart with SciChart. View the demo for horizontal bars, rounded corners and data labels to show project timelines and task completion.

JavaScript Choropleth Map | Javascript Charts | SciChart.js Demo

JavaScript Choropleth Map Example

Create a JavaScript Choropleth map, a type of thematic map where areas are shaded or patterned in proportion to the value of a variable being represented.

JavaScript Multi-Layer Map | Javascript Charts | SciChart.js

JavaScript Multi-Layer Map Example

Create a JavaScript Multi-Layer Map Example, using FastTriangleRenderableSeries with GeoJSON data-points using a constrained delaunay triangulation algorithm.

JavaScript Animated Bar Chart | Javascript Charts | SciChart.js

JavaScript Animated Bar Chart Example

Bring annual comparison data to life with the JavaScript Animated Bar Chart example from SciChart. This demo showcases top 10 tennis players from 1990 to 2024.

JavaScript Vector Field Plot | Javascript Charts | SciChart.js

JavaScript Vector Field Plot

View the JavaScript Vector Field Plot example from SciChart, including dynamic vector generation, gradient-colored segments, and interactive zoom/pan. Try demo.

JavaScript Waterfall Chart | Bridge Chart | SciChart.js

JavaScript Waterfall Chart | Bridge Chart

Build a JavaScript Waterfall Chart with dynamic coloring, multi-line data labels and responsive design. Try SciChart.js for seamless integration today.

JavaScript Box Plot Chart | Javascript Charts | SciChart.js Demo

JavaScript Box Plot Chart

Try the JavaScript Box-Plot Chart examples with developer-friendly chart lifecycle management, dynamic sub-surface positioning, and custom styling.

JavaScript Triangle Series | Triangle Mesh Chart | SciChart

JavaScript Triangle Series | Triangle Mesh Chart

Create JavaScript Triangle Meshes with the Triangle Series from SciChart. This demo supports strip mode, list mode and the drawing of polygons. View the example.

JavaScript Treemap Chart | Javascript Charts | SciChart.js Demo

JavaScript Treemap Chart

Create a JavaScript Treemap Chart to define rectangle positions based on total value. Use SciChart FastRectangleRenderableSeries and d3-hierarchy.js layouts.

NEW!
JavaScript Map Chart with Heatmap overlay | SciChart.js

JavaScript Map Chart with Heatmap overlay

Design a highly dynamic JavaScript Map Chart with Heatmap overlay with SciChart's feature-rich JavaScript Chart Library. Get your free demo today.

Realtime Audio Analyzer Bars Demo | SciChart.js Demo

Realtime Audio Analyzer Bars Demo

Demonstrating the capability of SciChart.js to create a JavaScript Audio Analyzer Bars and visualize the Fourier-Transform of an audio waveform in realtime.

JavaScript Linear Gauges | Javascript Charts | SciChart.js Demo

JavaScript Linear Gauges Example

View the JavaScript Linear Gauge Chart example to combine rectangles & annotations. Create a linear gauge dashboard with animated indicators and custom scales.

Responsive HTML Annotations | Javascript Charts | SciChart.js

JavaScript Responsive HTML Annotations Example

Build Responsive JavaScript HTML Annotations with SciChart. Use the advanced CSS container queries for responsive text layout and custom design. View demo now.

HTML Annotations and Custom in-chart Controls | SciChart

HTML Annotations and Custom in-chart Controls Example

JavaScript HTML Chart Control example demonstrates advanced HTML annotation integration and how to render HTML components within charts. Try the SciChart demo.

JavaScript Polar Modifiers | Polar Interactivity Modifiers

JavaScript 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.

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