JavaScript Histogram Chart

Creates a JavaScript Histogram Chart using SciChart.js, by leveraging the FastRectangleRenderableSeries, and its customTextureOptions property to have a custom tiling texture fill.

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    EAxisAlignment,
9    FastRectangleRenderableSeries,
10    XyxyDataSeries,
11    EColumnYMode,
12    EColumnMode,
13    EDataPointWidthMode,
14    NumberRange,
15    EHorizontalTextPosition,
16    EVerticalTextPosition,
17    ICustomTextureOptions,
18} from "scichart";
19import { appTheme } from "../../../theme";
20
21// Population data by age range
22const populationData = {
23    xValues: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100],
24    yValues: {
25        male: [
26            4869936, 5186991, 5175063, 5286053, 5449038, 5752398, 6168124, 6375035, 6265554, 5900833, 6465830, 7108184,
27            6769524, 5676968, 4828153, 3734266, 2732054, 1633630, 587324, 128003, 12023,
28        ],
29        female: [
30            4641147, 4940521, 5010242, 5010526, 5160160, 5501673, 6022599, 6329356, 6299693, 5930345, 6509757, 7178487,
31            7011569, 6157651, 5547296, 4519433, 3704145, 2671974, 1276597, 399148, 60035,
32        ],
33    },
34};
35
36const BREAK_POINTS = [0, 20, 30, 45, 65, 70, 80];
37
38function prepareRectangleData() {
39    const totalData = populationData.xValues.map((xValue, index) => {
40        const total = populationData.yValues.male[index] + populationData.yValues.female[index];
41        return { xValue, yValue: total };
42    });
43
44    // Prepare data for each range
45    const xValues: number[] = [];
46    const yValues: number[] = [];
47    const x1Values: number[] = [];
48    const y1Values: number[] = [];
49
50    BREAK_POINTS.forEach((breakPoint, index) => {
51        let nextBreakPoint = BREAK_POINTS[index + 1];
52
53        if (nextBreakPoint === undefined) {
54            nextBreakPoint = 100; // Set the last range
55        }
56
57        const rangePopulation = totalData
58            .filter((data) => {
59                return data.xValue >= breakPoint && data.xValue < nextBreakPoint;
60            })
61            .reduce((sum, data) => sum + data.yValue, 0);
62
63        xValues.push(breakPoint);
64        yValues.push(rangePopulation);
65        x1Values.push(nextBreakPoint);
66        y1Values.push(0); // Set y1 to 0 for the bottom of the rectangle
67    });
68
69    return { xValues, yValues, x1Values, y1Values };
70}
71
72class StickFigureTextureOptions implements ICustomTextureOptions {
73    options: { stroke: string };
74    textureHeight: number = 48;
75    textureWidth: number = 48;
76    repeat?: boolean = true;
77
78    public constructor(options: { stroke: string; repeat: boolean; textureHeight: number; textureWidth: number }) {
79        this.options = options;
80        this.textureHeight = options.textureHeight;
81        this.textureWidth = options.textureWidth;
82    }
83
84    public createTexture(
85        context: CanvasRenderingContext2D,
86        options: { fill: string; opacity: number; stroke: string }
87    ) {
88        context.fillStyle = options.fill;
89        context.fillRect(0, 0, this.textureWidth, this.textureHeight);
90        context.strokeStyle = options.stroke;
91
92        // Set up transformation: move to center, rotate, move back for
93        context.translate(this.textureWidth / 2, this.textureHeight / 2);
94        context.translate(-this.textureWidth / 2, -this.textureHeight / 2);
95
96        // Proportional values
97        const centerX = this.textureWidth / 2;
98        const headRadius = Math.min(this.textureWidth, this.textureHeight) * 0.16; // 16% of smaller dimension
99        const headY = this.textureHeight * 0.25;
100        const bodyTopY = headY + headRadius;
101        const bodyBottomY = this.textureHeight * 0.63;
102        const armY = bodyTopY + this.textureHeight * 0.06;
103        const armSpan = this.textureWidth * 0.38; // arms reach out 19% each side
104        const legY = bodyBottomY;
105        const legSpan = this.textureWidth * 0.25; // legs out 12.5% each side
106        const legBottomY = this.textureHeight * 0.97;
107
108        // Head
109        context.beginPath();
110        context.arc(centerX, headY, headRadius, 0, Math.PI * 2);
111        context.stroke();
112
113        // Body
114        context.beginPath();
115        context.moveTo(centerX, bodyTopY);
116        context.lineTo(centerX, bodyBottomY);
117        context.stroke();
118
119        // Left Arm
120        context.beginPath();
121        context.moveTo(centerX, armY);
122        context.lineTo(centerX - armSpan, armY + this.textureHeight * 0.09);
123        context.stroke();
124
125        // Right Arm
126        context.beginPath();
127        context.moveTo(centerX, armY);
128        context.lineTo(centerX + armSpan, armY + this.textureHeight * 0.09);
129        context.stroke();
130
131        // Left Leg
132        context.beginPath();
133        context.moveTo(centerX, legY);
134        context.lineTo(centerX - legSpan, legBottomY);
135        context.stroke();
136
137        // Right Leg
138        context.beginPath();
139        context.moveTo(centerX, legY);
140        context.lineTo(centerX + legSpan, legBottomY);
141        context.stroke();
142    }
143}
144
145export const drawExample = async (rootElement: string | HTMLDivElement) => {
146    // Create a SciChartSurface
147    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
148        theme: appTheme.SciChartJsTheme,
149        title: "Europe Population Distribution by Age Range",
150        titleStyle: {
151            fontSize: 24,
152        },
153    });
154
155    // Add X-axis
156    const xAxis = new NumericAxis(wasmContext, {
157        axisTitle: "Age Range (Years)",
158        autoTicks: false,
159        majorDelta: 5,
160        drawMajorBands: false,
161        drawMinorGridLines: false,
162        drawMajorTickLines: false,
163        drawMinorTickLines: false,
164        axisTitleStyle: {
165            fontSize: 13,
166            fontFamily: "Arial",
167            color: "#ffffff",
168            fontStyle: "italic",
169        },
170        growBy: new NumberRange(0.02, 0.02),
171    });
172    xAxis.labelProvider.formatLabel = (value: number) => {
173        // Custom label formatter to improve readability
174        if (BREAK_POINTS.includes(value)) {
175            return value.toString();
176        }
177        if (value === 100) {
178            return "100+";
179        }
180        return null;
181    };
182    sciChartSurface.xAxes.add(xAxis);
183
184    // Add Y-axis
185    const yAxis = new NumericAxis(wasmContext, {
186        axisTitle: "Population (Millions)",
187        labelFormat: ENumericFormat.Engineering,
188        axisAlignment: EAxisAlignment.Left,
189        drawMajorBands: false,
190        drawMinorGridLines: false,
191        drawMajorGridLines: true,
192        drawMajorTickLines: false,
193        drawMinorTickLines: false,
194        axisTitleStyle: {
195            fontSize: 14,
196            fontFamily: "Arial",
197            color: "#ffffff",
198            fontStyle: "italic",
199        },
200        growBy: new NumberRange(0.01, 0.1),
201    });
202    sciChartSurface.yAxes.add(yAxis);
203
204    // Prepare data and create rectangle series
205    const { xValues, yValues, x1Values, y1Values } = prepareRectangleData();
206
207    const rectangleSeries = new FastRectangleRenderableSeries(wasmContext, {
208        dataSeries: new XyxyDataSeries(wasmContext, {
209            xValues,
210            yValues,
211            x1Values,
212            y1Values,
213        }),
214        columnXMode: EColumnMode.StartEnd,
215        columnYMode: EColumnYMode.TopBottom,
216        dataPointWidthMode: EDataPointWidthMode.Range,
217        stroke: appTheme.DarkIndigo,
218        opacity: 0.8,
219        fill: appTheme.MutedSkyBlue,
220        topCornerRadius: 8,
221        bottomCornerRadius: 0,
222        customTextureOptions: new StickFigureTextureOptions({
223            stroke: appTheme.MutedBlue,
224            repeat: true,
225            textureWidth: 40,
226            textureHeight: 40,
227        }),
228        dataLabels: {
229            color: appTheme.DarkIndigo,
230            style: {
231                fontSize: 16,
232            },
233            precision: 0,
234            numericFormat: ENumericFormat.Engineering,
235            verticalTextPosition: EVerticalTextPosition.Above,
236            horizontalTextPosition: EHorizontalTextPosition.Right,
237        },
238    });
239    sciChartSurface.renderableSeries.add(rectangleSeries);
240
241    // Adjust the size of the custom texture so it scales as you zoom
242    sciChartSurface.layoutMeasured.subscribe((data) => {
243        const width = xAxis.getCurrentCoordinateCalculator().getCoordWidth(5);
244        const height = yAxis.getCurrentCoordinateCalculator().getCoordWidth(10000000);
245        if (
246            width !== rectangleSeries.customTextureOptions.textureWidth ||
247            height !== rectangleSeries.customTextureOptions.textureHeight
248        ) {
249            rectangleSeries.customTextureOptions = new StickFigureTextureOptions({
250                stroke: appTheme.MutedBlue,
251                repeat: true,
252                textureWidth: width,
253                textureHeight: height,
254            });
255        }
256    });
257
258    // Add interactivity modifiers
259    sciChartSurface.chartModifiers.add(new ZoomPanModifier(), new ZoomExtentsModifier(), new MouseWheelZoomModifier());
260
261    return { sciChartSurface, wasmContext };
262};
263

JavaScript Histogram Chart

Overview

This example demonstrates how to create a Histogram Chart using SciChart.js in vanilla JavaScript. The chart visualizes Europe's population distribution by age range using FastRectangleRenderableSeries with custom textures and data labels.

Technical Implementation

The chart is initialized asynchronously using SciChartSurface.create(). It uses XyxyDataSeries to define rectangle positions with columnXMode: EColumnMode.StartEnd and columnYMode: EColumnYMode.TopBottom. A custom StickFigureTextureOptions class implements ICustomTextureOptions to create patterned fills.

Features and Capabilities

The example showcases population data aggregation into age ranges, engineering-format labels on the Y-axis, and interactive modifiers including ZoomPanModifier and MouseWheelZoomModifier. Custom corner radii and data labels enhance visual presentation.

Integration and Best Practices

The implementation follows JavaScript best practices with proper async/await initialization and includes a cleanup function for memory management. The data preparation demonstrates efficient aggregation techniques for histogram visualization.

javascript Chart Examples & Demos

See Also: JavaScript Chart Types (40 Demos)

JavaScript Line Chart | Javascript Charts | SciChart.js Demo

JavaScript Line Chart

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

JavaScript Spline Line Chart | Javascript Charts | SciChart.js

JavaScript Spline Line Chart

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

JavaScript Digital Line Chart | Javascript Charts | SciChart.js

JavaScript Digital Line Chart

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

JavaScript Band Chart | Javascript Charts | SciChart.js Demo

JavaScript Band Chart

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

JavaScript Spline Band Chart | Javascript Charts | SciChart.js

JavaScript Spline Band Chart

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.

JavaScript Digital Band Chart | Javascript Charts | SciChart.js

JavaScript Digital Band Chart

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.

JavaScript Bubble Chart | Online JavaScript Chart Examples

JavaScript Bubble Chart

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.

JavaScript Candlestick Chart | Online JavaScript Chart Examples

JavaScript Candlestick Chart

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 | Javascript Charts | SciChart.js Demo

JavaScript Column Chart

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

JavaScript Population Pyramid | Javascript Charts | SciChart.js

JavaScript Population Pyramid

Population Pyramid of Europe and Africa

JavaScript Error Bars Char | Javascript Charts | SciChart.js

JavaScript Error Bars Chart

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

JavaScript Impulse Chart | Javascript Charts | SciChart.js Demo

JavaScript Impulse Chart

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

JavaScript Text Chart | Javascript Charts | SciChart.js Demo

JavaScript Text Chart

Create JavaScript Text Chart with high performance SciChart.js.

JavaScript Fan Chart | Javascript Charts | SciChart.js Demo

JavaScript Fan Chart

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.

JavaScript Heatmap Chart | Javascript Charts | SciChart.js Demo

JavaScript Heatmap Chart

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

JavaScript Non Uniform Heatmap Chart | SciChart.js Demo

JavaScript Non Uniform Heatmap Chart

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

JavaScript Heatmap Chart With Contours | SciChart.js Demo

JavaScript Heatmap Chart With Contours Example

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

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.

JavaScript Mountain Chart | Javascript Charts | SciChart.js Demo

JavaScript Mountain Chart

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 | Javascript Charts | SciChart.js

JavaScript Spline Mountain Chart

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

JavaScript Digital Mountain Chart | SciChart.js Demo

JavaScript Digital Mountain Chart

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 | View Online At SciChart

JavaScript Realtime Mountain Chart

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

JavaScript Scatter Chart | Javascript Charts | SciChart.js Demo

JavaScript Scatter Chart

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

JavaScript Stacked Column Chart | Online JavaScript Charts

JavaScript Stacked Column Chart

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

JavaScript Stacked Group Column Chart | View Examples Now

JavaScript Stacked Column Side by Side

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

JavaScript Stacked Mountain Chart | SciChart.js Demo

JavaScript Stacked Mountain Chart

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

JavaScript Smooth Stacked Mountain Chart | SciChart.js

JavaScript Smooth Stacked Mountain Chart

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

JavaScript Pie Chart | Javascript Charts | SciChart.js Demo

JavaScript Pie Chart

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.

JavaScript Donut Chart | Javascript Charts | SciChart.js Demo

JavaScript Donut Chart

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

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.

JavaScript Quadrant Chart using Background Annotations

JavaScript Quadrant Chart using Background Annotations

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

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 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 Force Directed Graph | Javascript Charts | SciChart.js

JavaScript Force Directed Graph

JavaScript Force Directed Graph demo by SciChart.js. Visualize network graphs with physics simulation, interactive node dragging, and hover tooltips.

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