JavaScript Waterfall Chart | Bridge Chart

Creates a JavaScript Waterfall Chart using SciChart.js's new FastRectangleRenderableSeries with the following features: a custom Treemap-like DataLabelProvider for rectangle labels and custom Fill PaletteProvider

Also Known As: Bridge Chart and Cascade Chart.

Fullscreen

Edit

 Edit

Docs

drawExample.ts

index.html

vanilla.ts

theme.ts

Copy to clipboard
Minimise
Fullscreen
1import {
2    NumericAxis,
3    ZoomPanModifier,
4    ZoomExtentsModifier,
5    MouseWheelZoomModifier,
6    SciChartSurface,
7    ENumericFormat,
8    FastRectangleRenderableSeries,
9    XyxyDataSeries,
10    EColumnYMode,
11    EColumnMode,
12    EDataPointWidthMode,
13    NumberRange,
14    EHorizontalTextPosition,
15    EVerticalTextPosition,
16    TextLabelProvider,
17    RectangleSeriesDataLabelProvider,
18    formatNumber,
19    IRectangleSeriesDataLabelProviderOptions,
20    EDataLabelSkipMode,
21    EMultiLineAlignment,
22    IPointMetadata,
23    IFillPaletteProvider,
24    EFillPaletteMode,
25    parseColorToUIntArgb,
26    ELabelAlignment,
27    Thickness,
28} from "scichart";
29import { appTheme } from "../../../theme";
30
31const data = [
32    { date: "Jan.", profit: 387264 },
33    { date: "Feb.", profit: 772096 },
34    { date: "Mar.", profit: 638075 },
35    { date: "Apr.", profit: -211386 },
36    { date: "May", profit: -138135 },
37    { date: "Jun.", profit: -267238 },
38    { date: "Jul.", profit: 431406 },
39    { date: "Aug.", profit: 363018 },
40    { date: "Sep.", profit: -224638 },
41    { date: "Oct.", profit: -299867 },
42    { date: "Nov.", profit: 607365 },
43    { date: "Dec.", profit: 1106986 },
44];
45
46const waterfall = function (numbers: { date: string; profit: number }[]) {
47    let last = 0,
48        accu = 0;
49    const waterfall = numbers.map((d, i) => {
50        last = accu;
51        accu += d.profit;
52        return {
53            date: d.date,
54            nextDay: i < numbers.length - 1 ? numbers[i + 1].date : "Total",
55            prior: last,
56            accu: accu,
57            profit: d.profit,
58        };
59    });
60
61    waterfall.push({
62        date: "Total",
63        nextDay: null,
64        prior: 0,
65        accu: accu,
66        profit: 0,
67    });
68
69    return waterfall;
70};
71
72const generatedData = waterfall(data);
73
74const labelProvider = new TextLabelProvider({
75    labels: [...generatedData.map((d) => d.date)],
76    maxLength: 15,
77});
78
79export const drawExample = async (rootElement: string | HTMLDivElement) => {
80    // Create a SciChartSurface
81    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
82        theme: appTheme.SciChartJsTheme,
83    });
84
85    const growBy = new NumberRange(0.05, 0.05);
86
87    // Create an XAxis with a TextLabelProvider
88    const xAxis = new NumericAxis(wasmContext, {
89        // labelprovider maps Xaxis values (in this case index) to labels (in this case manufacturer name)
90        labelProvider,
91        labelStyle: {
92            alignment: ELabelAlignment.Center,
93            padding: new Thickness(2, 1, 2, 1),
94            fontSize: 11,
95        },
96        maxAutoTicks: 15,
97        growBy: new NumberRange(0.05, 0.05), // add some horizontal padding
98    });
99
100    sciChartSurface.xAxes.add(xAxis);
101
102    // Create a YAxis
103    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy, labelFormat: ENumericFormat.Engineering }));
104
105    class CustomFillProvider implements IFillPaletteProvider {
106        public readonly fillPaletteMode = EFillPaletteMode.SOLID;
107
108        public onAttached(): void {}
109        public onDetached(): void {}
110
111        public overrideFillArgb(
112            _xValue: number,
113            _yValue: number,
114            _index: number,
115            _opacity?: number,
116            metadata?: IPointMetadata
117        ): number {
118            const profit = (metadata as unknown as { profit: number })?.profit;
119
120            if (profit > 0) {
121                return parseColorToUIntArgb(appTheme.VividTeal, 255);
122            }
123
124            if (profit < 0) {
125                return parseColorToUIntArgb(appTheme.VividPink, 255);
126            }
127
128            return parseColorToUIntArgb(appTheme.VividBlue, 128);
129        }
130    }
131
132    class TreemapDataLabelProvider extends RectangleSeriesDataLabelProvider {
133        constructor(options?: IRectangleSeriesDataLabelProviderOptions) {
134            super(options);
135        }
136
137        // Override "getText" method to provide dynamic text based on rectangle size
138        getText(state: any): string {
139            const metadata = state.getMetaData() as any;
140
141            return (
142                `${formatNumber(metadata.accu, ENumericFormat.Engineering, 2)}$` +
143                (metadata.profit === 0
144                    ? ""
145                    : `\n${metadata.profit > 0 ? "+" : ""}${formatNumber(
146                        metadata.profit,
147                        ENumericFormat.Engineering,
148                        this.precision
149                    )}$`)
150            );
151        }
152    }
153
154    // Prepare data and create rectangle series
155
156    const rectangleSeries = new FastRectangleRenderableSeries(wasmContext, {
157        dataSeries: new XyxyDataSeries(wasmContext, {
158            xValues: generatedData.map((d, i) => i),
159            yValues: generatedData.map((d) => d.prior),
160            x1Values: generatedData.map((d, i) => i),
161            y1Values: generatedData.map((d) => d.accu),
162            metadata: generatedData as any[],
163        }),
164        columnXMode: EColumnMode.Mid,
165        columnYMode: EColumnYMode.TopBottom,
166        dataPointWidthMode: EDataPointWidthMode.Range,
167        stroke: "black",
168        strokeThickness: 1,
169        opacity: 0.5,
170        fill: appTheme.DarkIndigo,
171        dataLabelProvider: new TreemapDataLabelProvider({
172            skipMode: EDataLabelSkipMode.ShowAll,
173            color: "white",
174            style: {
175                fontSize: 11,
176                multiLineAlignment: EMultiLineAlignment.Center,
177                lineSpacing: 5,
178            },
179            horizontalTextPosition: EHorizontalTextPosition.Center,
180            verticalTextPosition: EVerticalTextPosition.Center,
181            metaDataSelector: (md: unknown) => {
182                return (md as { profit: string }).profit;
183            },
184        }),
185        paletteProvider: new CustomFillProvider(),
186    });
187    sciChartSurface.renderableSeries.add(rectangleSeries);
188
189    // Add interactivity modifiers
190    sciChartSurface.chartModifiers.add(
191        new ZoomPanModifier({ enableZoom: true }),
192        new ZoomExtentsModifier(),
193        new MouseWheelZoomModifier()
194    );
195
196    return { sciChartSurface, wasmContext };
197};
198

Waterfall Chart - JavaScript

Overview

This example demonstrates how to create a Waterfall Chart using SciChart.js in vanilla JavaScript. The chart visualizes cumulative profit data with colored rectangles indicating positive (teal) and negative (pink) values, using the FastRectangleRenderableSeries for high-performance rendering.

Technical Implementation

The chart uses an XyxyDataSeries to define rectangle positions (prior/accu values) and a custom IFillPaletteProvider to dynamically color rectangles based on profit values. The TextLabelProvider maps indices to month labels, while a custom RectangleSeriesDataLabelProvider displays formatted cumulative values.

Features and Capabilities

Key features include dynamic coloring based on data values, formatted engineering notation labels, and interactive zoom/pan with ZoomPanModifier. The implementation showcases how to transform raw data into waterfall format using a custom data processing function.

Integration and Best Practices

The example follows best practices for WASM initialization with SciChartSurface.create() and includes proper cleanup. For performance, it uses a single renderable series with palette providers instead of multiple series.

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

NEW!
JavaScript Order of Rendering | Javascript Charts | SciChart.js

JavaScript Order of Rendering Example

The JavaScript Order of Rendering example gives you full control of the draw order of series and annotations for charts. Try SciChart's advanced customizations.

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.