If you want to learn about heatmaps. this demo shows you how to create a JavaScript Heatmap Chart using SciChart.js, our 5-star rated JavaScript Chart Component.
drawExample.ts
index.html
vanilla.ts
theme.ts
1import {
2 HeatmapColorMap,
3 HeatmapLegend,
4 MouseWheelZoomModifier,
5 NumericAxis,
6 SciChartSurface,
7 UniformHeatmapDataSeries,
8 UniformHeatmapRenderableSeries,
9 zeroArray2D,
10 ZoomExtentsModifier,
11 ZoomPanModifier,
12} from "scichart";
13import { appTheme } from "../../../theme";
14
15const MAX_SERIES = 100;
16const WIDTH = 300;
17const HEIGHT = 200;
18
19// Draws a Heatmap chart in real-time
20export const drawExample = async (rootElement: string | HTMLDivElement) => {
21 // Create a SciChartSurface
22 const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
23 theme: appTheme.SciChartJsTheme,
24 });
25
26 // Add XAxis and YAxis
27 sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { isVisible: false }));
28 sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { isVisible: false }));
29
30 // Create a Heatmap Data-series. Pass heatValues as a number[][] to the UniformHeatmapDataSeries
31 const initialZValues: number[][] = generateExampleData(WIDTH, HEIGHT, 200, 20, MAX_SERIES);
32 const heatmapDataSeries = new UniformHeatmapDataSeries(wasmContext, {
33 xStart: 0,
34 xStep: 1,
35 yStart: 0,
36 yStep: 1,
37 zValues: initialZValues,
38 });
39
40 // Create a Heatmap RenderableSeries with the color map. ColorMap.minimum/maximum defines the values in
41 // HeatmapDataSeries which correspond to gradient stops at 0..1
42 const heatmapSeries = new UniformHeatmapRenderableSeries(wasmContext, {
43 dataSeries: heatmapDataSeries,
44 useLinearTextureFiltering: false,
45 colorMap: new HeatmapColorMap({
46 minimum: 0,
47 maximum: 200,
48 gradientStops: [
49 { offset: 1, color: appTheme.VividPink },
50 { offset: 0.9, color: appTheme.VividOrange },
51 { offset: 0.7, color: appTheme.MutedRed },
52 { offset: 0.5, color: appTheme.VividGreen },
53 { offset: 0.3, color: appTheme.VividSkyBlue },
54 { offset: 0.2, color: appTheme.Indigo },
55 { offset: 0, color: appTheme.DarkIndigo },
56 ],
57 }),
58 });
59
60 // Add heatmap to the chart
61 sciChartSurface.renderableSeries.add(heatmapSeries);
62
63 // Add interaction
64 sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
65 sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
66 sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());
67
68 // Functions for running the example in real-time
69 let timerId: NodeJS.Timeout;
70 let updateIndex: number = 0;
71
72 const updateChart = () => {
73 // Cycle through pre-generated data on timer tick
74 const newZValues = generateExampleData(WIDTH, HEIGHT, 200, updateIndex++, MAX_SERIES);
75 // Update the heatmap z-values
76 heatmapDataSeries.setZValues(newZValues);
77 if (updateIndex >= MAX_SERIES) {
78 updateIndex = 0;
79 }
80 timerId = setTimeout(updateChart, 16);
81 };
82
83 const startUpdate = () => {
84 if (!timerId) {
85 updateChart();
86 }
87 };
88
89 const stopUpdate = () => {
90 clearTimeout(timerId);
91 timerId = undefined;
92 };
93
94 const subscribeToRenderStats = (callback: (stats: { xSize: number; ySize: number; fps: number }) => void) => {
95 // Handle drawing/updating FPS
96 let lastRendered = Date.now();
97 sciChartSurface.rendered.subscribe(() => {
98 const currentTime = Date.now();
99 const timeDiffSeconds = (currentTime - lastRendered) / 1000;
100 lastRendered = currentTime;
101 const fps = 1 / timeDiffSeconds;
102 callback({
103 xSize: heatmapDataSeries.arrayWidth,
104 ySize: heatmapDataSeries.arrayHeight,
105 fps,
106 });
107 });
108 };
109
110 return { sciChartSurface, subscribeToRenderStats, controls: { startUpdate, stopUpdate } };
111};
112
113// Draws a Heatmap legend over the <div id={divHeatmapLegend}></div>
114export const drawHeatmapLegend = async (rootElement: string | HTMLDivElement) => {
115 const { heatmapLegend, wasmContext } = await HeatmapLegend.create(rootElement, {
116 theme: {
117 ...appTheme.SciChartJsTheme,
118 sciChartBackground: appTheme.DarkIndigo + "BB",
119 loadingAnimationBackground: appTheme.DarkIndigo + "BB",
120 },
121 yAxisOptions: {
122 isInnerAxis: true,
123 labelStyle: {
124 fontSize: 12,
125 color: appTheme.ForegroundColor,
126 },
127 axisBorder: {
128 borderRight: 1,
129 color: appTheme.ForegroundColor + "77",
130 },
131 majorTickLineStyle: {
132 color: appTheme.ForegroundColor,
133 tickSize: 6,
134 strokeThickness: 1,
135 },
136 minorTickLineStyle: {
137 color: appTheme.ForegroundColor,
138 tickSize: 3,
139 strokeThickness: 1,
140 },
141 },
142 colorMap: {
143 minimum: 0,
144 maximum: 200,
145 gradientStops: [
146 { offset: 1, color: appTheme.VividPink },
147 { offset: 0.9, color: appTheme.VividOrange },
148 { offset: 0.7, color: appTheme.MutedRed },
149 { offset: 0.5, color: appTheme.VividGreen },
150 { offset: 0.3, color: appTheme.VividSkyBlue },
151 { offset: 0.2, color: appTheme.Indigo },
152 { offset: 0, color: appTheme.DarkIndigo },
153 ],
154 },
155 });
156
157 return { sciChartSurface: heatmapLegend.innerSciChartSurface.sciChartSurface };
158};
159
160// This function generates data for the heatmap series example
161// because data-generation is not trivial, we generate once before the example starts
162// so you can see the speed & power of SciChart.js
163function generateExampleData(
164 width: number,
165 height: number,
166 cpMax: number,
167 index: number,
168 maxIndex: number
169): number[][] {
170 // Returns a 2-dimensional javascript array [height (y)] [width (x)] size
171 const zValues = zeroArray2D([height, width]);
172
173 // math.round but to X digits
174 function roundTo(number: number, digits: number) {
175 return number;
176 // return parseFloat(number.toFixed(digits));
177 }
178
179 const angle = roundTo(Math.PI * 2 * index, 3) / maxIndex;
180
181 // When appending data to a 2D Array for the heatmap, the order of appending (X,Y) does not matter
182 // but when accessing the zValues[][] array, we set data [y] then [x]
183 for (let y = 0; y < height; y++) {
184 for (let x = 0; x < width; x++) {
185 const v =
186 (1 + roundTo(Math.sin(x * 0.04 + angle), 3)) * 50 +
187 (1 + roundTo(Math.sin(y * 0.1 + angle), 3)) * 50 * (1 + roundTo(Math.sin(angle * 2), 3));
188 const cx = width / 2;
189 const cy = height / 2;
190 const r = Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
191 const exp = Math.max(0, 1 - r * 0.008);
192 const zValue = v * exp + Math.random() * 10;
193 zValues[y][x] = zValue > cpMax ? cpMax : zValue;
194 }
195 }
196 return zValues;
197}
198This example demonstrates a high-performance Heatmap Chart implemented using JavaScript with SciChart.js. The implementation focuses on real-time data streaming and dynamic visual updates, leveraging WebGL-powered rendering for optimal performance.
The chart is built by creating a SciChartSurface instance via the SciChart.js API, and hidden numeric axes are added to simplify the presentation. The example generates a two-dimensional array of data which is fed to a UniformHeatmapDataSeries. A UniformHeatmapRenderableSeries is then configured with a detailed gradient via HeatmapColorMap to accurately represent the data. Real-time updates are managed using JavaScript’s setTimeout to continuously update the series, as explained in the Adding Realtime Updates documentation.
Key features include real-time chart updates with dynamic data streaming, advanced color mapping techniques, and integrated performance monitoring. The color mapping is finely tuned using gradient stops which you can learn more about in the Uniform Heatmap Chart documentation and Uniform-Heatmap-Colormaps resources. In addition, interactive modifiers such as ZoomPanModifier and MouseWheelZoomModifier enhance the user experience.
Built on JavaScript, this example adheres to best practices by directly interacting with the SciChart.js API without relying on additional frameworks. Developers can monitor performance by subscribing to render events, a technique detailed in the Performance Tips & Tricks guide. Furthermore, interactive features are implemented using modifiers like the MouseWheelZoomModifier to ensure a smooth user experience. This approach provides a clear and maintainable structure for integrating SciChart.js within any JavaScript project.

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.

Discover how to create JavaScript Fan Chart with SciChart. Zoom in to see the detail you can go to using our JavaScript Charts. Get your free demo 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.