Here we demonstrate how to create a JavaScript Fan Chart using SciChart.js. Zoom in and out to see the detail you can go to using our JavaScript Charts
drawExample.ts
index.html
vanilla.ts
RandomWalkGenerator.ts
theme.ts
1import { appTheme } from "../../../theme";
2import { RandomWalkGenerator } from "../../../ExampleData/RandomWalkGenerator";
3
4import {
5 MouseWheelZoomModifier,
6 ZoomExtentsModifier,
7 ZoomPanModifier,
8 XyyDataSeries,
9 NumericAxis,
10 SciChartSurface,
11 XyDataSeries,
12 ENumericFormat,
13 WaveAnimation,
14 SplineLineRenderableSeries,
15 TextAnnotation,
16 EVerticalAnchorPoint,
17 SplineBandRenderableSeries,
18} from "scichart";
19// tslint:disable:max-line-length
20
21const animation = new WaveAnimation({ duration: 700, fadeEffect: true });
22
23export type TVarPoint = {
24 date: number;
25 actual: number;
26 varMax: number;
27 var4: number;
28 var3: number;
29 var2: number;
30 var1: number;
31 varMin: number;
32};
33
34export function getVarianceData(): TVarPoint[] {
35 const varianceData: TVarPoint[] = [];
36 const startDate = 1546300800; // 1st Jan 2019
37 const dateStep = 1546387200 - startDate; // one day;
38
39 const length: number = 10;
40 const yValues: number[] = new RandomWalkGenerator().Seed(923478).getRandomWalkSeries(length).yValues;
41 for (let i = 0; i < length; i++) {
42 const date = startDate + dateStep * i;
43
44 let varMax: number = NaN;
45 let var4: number = NaN;
46 let var3: number = NaN;
47 let var2: number = NaN;
48 let var1: number = NaN;
49 let varMin: number = NaN;
50
51 if (i > 4) {
52 varMax = yValues[i] + (i - 5) * 0.3;
53 var4 = yValues[i] + (i - 5) * 0.2;
54 var3 = yValues[i] + (i - 5) * 0.1;
55 var2 = yValues[i] - (i - 5) * 0.1;
56 var1 = yValues[i] - (i - 5) * 0.2;
57 varMin = yValues[i] - (i - 5) * 0.3;
58 }
59
60 varianceData.push({ date, actual: yValues[i], varMax, var4, var3, var2, var1, varMin });
61 }
62
63 return varianceData;
64}
65
66export const drawExample = async (rootElement: string | HTMLDivElement) => {
67 // Create a SciChartSurface
68 const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
69 theme: appTheme.SciChartJsTheme,
70 });
71
72 // Add an XAxis, YAxis
73 sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { labelFormat: ENumericFormat.Date_DDMMYYYY }));
74 sciChartSurface.yAxes.add(new NumericAxis(wasmContext));
75
76 // Generates some data for the example as an array of TVarPoint: {
77 // date: number;
78 // actual: number;
79 // varMax: number;
80 // var4: number;
81 // var3: number;
82 // var2: number;
83 // var1: number;
84 // varMin: number;
85 // }
86 const varianceData = getVarianceData();
87
88 // To render the fan chart, we use a Line Chart with XyDataSeries
89 // and three Band charts with XyyDataSeries
90 const actualDataSeries = new XyDataSeries(wasmContext);
91 const variance3DataSeries = new XyyDataSeries(wasmContext);
92 const variance2DataSeries = new XyyDataSeries(wasmContext);
93 const variance1DataSeries = new XyyDataSeries(wasmContext);
94
95 actualDataSeries.appendRange(
96 varianceData.map((v) => v.date),
97 varianceData.map((v) => v.actual)
98 );
99 variance3DataSeries.appendRange(
100 varianceData.map((v) => v.date),
101 varianceData.map((v) => v.varMin),
102 varianceData.map((v) => v.varMax)
103 );
104 variance2DataSeries.appendRange(
105 varianceData.map((v) => v.date),
106 varianceData.map((v) => v.var1),
107 varianceData.map((v) => v.var4)
108 );
109 variance1DataSeries.appendRange(
110 varianceData.map((v) => v.date),
111 varianceData.map((v) => v.var2),
112 varianceData.map((v) => v.var3)
113 );
114
115 // Add a line series with the Xy data (the actual data)
116 // Note use FastLineRenderableSeries for non-spline version
117 sciChartSurface.renderableSeries.add(
118 new SplineLineRenderableSeries(wasmContext, {
119 strokeThickness: 2,
120 dataSeries: actualDataSeries,
121 stroke: appTheme.VividPink,
122 animation,
123 })
124 );
125
126 // Add band series with progressively higher opacity for the fan variance data
127 // Note use FastBandRenderableSeries for non-spline version
128 sciChartSurface.renderableSeries.add(
129 new SplineBandRenderableSeries(wasmContext, {
130 dataSeries: variance3DataSeries,
131 opacity: 0.15,
132 fill: appTheme.VividPink,
133 strokeY1: "#00000000",
134 animation,
135 })
136 );
137 sciChartSurface.renderableSeries.add(
138 new SplineBandRenderableSeries(wasmContext, {
139 dataSeries: variance2DataSeries,
140 opacity: 0.33,
141 fill: appTheme.VividPink,
142 strokeY1: "#00000000",
143 animation,
144 })
145 );
146 sciChartSurface.renderableSeries.add(
147 new SplineBandRenderableSeries(wasmContext, {
148 dataSeries: variance1DataSeries,
149 opacity: 0.5,
150 fill: appTheme.VividPink,
151 strokeY1: "#00000000",
152 animation,
153 })
154 );
155
156 // Optional: Add some interactivity modifiers
157 sciChartSurface.chartModifiers.add(
158 new ZoomExtentsModifier(),
159 new ZoomPanModifier({ enableZoom: true }),
160 new MouseWheelZoomModifier(),
161 new ZoomExtentsModifier()
162 );
163
164 // Optional: Add some annotations (text) to show detail
165 sciChartSurface.annotations.add(
166 new TextAnnotation({
167 x1: varianceData[0].date,
168 y1: varianceData[0].actual,
169 verticalAnchorPoint: EVerticalAnchorPoint.Bottom,
170 yCoordShift: -50,
171 text: "Actual data",
172 opacity: 0.45,
173 textColor: appTheme.ForegroundColor,
174 })
175 );
176
177 sciChartSurface.annotations.add(
178 new TextAnnotation({
179 x1: varianceData[5].date,
180 y1: varianceData[5].actual,
181 text: "Forecast Variance",
182 verticalAnchorPoint: EVerticalAnchorPoint.Top,
183 yCoordShift: 50,
184 opacity: 0.45,
185 textColor: appTheme.ForegroundColor,
186 })
187 );
188
189 sciChartSurface.zoomExtents();
190 return { wasmContext, sciChartSurface };
191};
192This example demonstrates how to create a sophisticated fan chart using SciChart.js with JavaScript. The chart visualizes actual data alongside forecast variance by combining a spline line series with multiple SplineBandRenderableSeries, providing an intuitive visualization of trend and variance data over time.
The chart is initialized asynchronously using SciChartSurface.create(), ensuring that the WebGL-based rendering engine and the underlying WebAssembly context are set up efficiently. This asynchronous approach is detailed in the Getting Started with SciChart JS guide. The fan chart effect is achieved by rendering an actual data line via SplineLineRenderableSeries and overlaying it with progressively opaque band series using SplineBandRenderableSeries, as described in The Fan Charts Type documentation.
The implementation incorporates interactive modifiers such as ZoomPanModifier and MouseWheelZoomModifier, enabling smooth zooming and panning interactions. Additionally, it applies a WaveAnimation for smooth transition effects, while the use of XyyDataSeries enables rendering of the band charts that illustrate forecast variance. For a deeper understanding of the data series used, developers can refer to the XyyDataSeries API documentation.
Best practices such as efficient resource management are observed by encapsulating the chart creation in an asynchronous function and ensuring proper cleanup using sciChartSurface.delete(). Performance optimization is a key consideration, with high-performance WebAssembly rendering capabilities that are further explained in the Performance Tips & Tricks and Memory Best Practices guides. These practices help maintain responsiveness even when handling complex chart rendering and dynamic data updates.

Discover how to create a high performance JavaScript Line Chart with SciChart - the leading JavaScript library. Get your free demo now.

Discover how to create a JavaScript Spline Line Chart with SciChart. Demo includes algorithm for smoother lines. Get your free trial now.

Discover how to create a JavaScript Digital Line Chart with SciChart - your feature-rich JavaScript Chart Library. Get your free demo now.

Easily create a JavaScript Band Chart or High-Low Fill with SciChart - high performance JavaScript Chart Library. Get your free trial now.

SciChart's JavaScript Spline Band Chart makes it easy to draw thresholds or fills between two lines on a chart. Get your free demo today.

Learn how to create a JavaScript Digital Band Chart or High-Low Fill Chart with SciChart's easy-to-follow demos. Get your free trial today.

Create a high performance JavaScript Bubble Chart with Sci-Chart. Demo shows how to draw point-markers at X,Y locations. Get your free demo now.

Discover how to create a JavaScript Candlestick Chart or Stock Chart using SciChart.js. For high Performance JavaScript Charts, get your free demo now.

JavaScript Column Chart demo by SciChart supports gradient fill and paletteproviders for more custom coloring options. Get your free demo now.

Population Pyramid of Europe and Africa

Create JavaScript Error Bars Chart using high performance SciChart.js. Display uncertainty or statistical confidence of a data-point. Get free demo now.

Easily create JavaScript Impulse Chart or Stem Chart using SciChart.js - our own high performance JavaScript Chart Library. Get your free trial now.

Create JavaScript Text Chart with high performance SciChart.js.

Easily create a high performance JavaScript Heatmap Chart with SciChart. Get your free trial of our 5-star rated JavaScript Chart Component today.

Create JavaScript Non Uniform Chart using high performance SciChart.js. Display Heatmap with variable cell sizes. Get free demo now.

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

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

Create JavaScript Mountain Chart with SciChart.js. Zero line can be zero or a specific value. Fill color can be solid or gradient as well. Get a free demo now.

JavaScript Spline Mountain Chart design made easy. Use SciChart.js' JavaScript Charts for high performance, feature-rich designs. Get free demo now.

Create JavaScript Digital Mountain Chart with a stepped-line visual effect. Get your free trial of SciChart's 5-star rated JavaScript Chart Component now.

JavaScript Realtime Mountain Chart made easy. Add animated, real-time updates with SciChart.js - high performance JavaScript Charts. Get free trial now.

Create JavaScript Scatter Chart with high performance SciChart.js. Easily render pre-defined point types. Supports custom shapes. Get your free trial now.

Discover how to create a JavaScript Stacked Column Chart using our feature-rich JavaScript Chart Library, SciChart.js. Get your free demo today!

Design JavaScript Stacked Group Column Chart side-by-side using our 5-star rated JavaScript Chart Framework, SciChart.js. Get your free demo now.

Design a high performance JavaScript Stacked Mountain Chart with SciChart.js - your one-stop JavaScript chart library. Get free demo now to get started.

Design a high performance JavaScript Stacked Mountain Chart with SciChart.js - your one-stop JavaScript chart library. Get free demo now to get started.

Easily create and customise a high performance JavaScript Pie Chart with 5-star rated SciChart.js. Get your free trial now to access the whole library.

Create JavaScript Donut Chart with 5-star rated SciChart.js chart library. Supports legends, text labels, animated updates and more. Get free trial now.

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

Demonstrates how to color areas of the chart surface using background Annotations using SciChart.js Annotations API

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

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.

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.

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

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

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

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

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.

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