JavaScript Error Bars Chart

This SciChart demo demonstrates how to create a JavaScript Error Bars Chart using SciChart.js our High Performance JavaScript Chart component.

src/drawExample.ts
import { appTheme } from "./appTheme";
import {
    NumericAxis,
    XyDataSeries,
    SciChartSurface,
    NumberRange,
    ZoomPanModifier,
    ZoomExtentsModifier,
    MouseWheelZoomModifier,
    HlcDataSeries,
    EllipsePointMarker,
    FastErrorBarsRenderableSeries,
    EErrorMode,
    EErrorDirection,
    EDataPointWidthMode,
    SplineMountainRenderableSeries,
    GradientParams,
    Point,
    ScaleAnimation,
} from "scichart";

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
    });

    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.1, 0.1) }));
    sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.1, 0.1) }));

    // Xy values for the data
    const xValues = [0, 1, 2, 2.5, 4.5, 5, 6, 7, 8];
    const yValues = [2.5, 3.5, 3.7, 4.0, 5.0, 5.5, 5.0, 4.0, 3.0];

    const randomError = () => Math.random() * 0.2 + 0.2;
    // Low high error (absolute values)
    const lowValues = yValues.map((y) => y - randomError());
    const highValues = yValues.map((y) => y + randomError());

    // Left/right error (absolute values)
    const leftValues = xValues.map((x) => x - randomError());
    const rightValues = xValues.map((x) => x + randomError());

    // add optional mountain series. We use Spline type, for higher performance use FastLine or FastMountainRenderableSeries
    const lineSeries = new SplineMountainRenderableSeries(wasmContext, {
        dataSeries: new XyDataSeries(wasmContext, { xValues, yValues }),
        strokeThickness: 5,
        stroke: appTheme.VividSkyBlue,
        fillLinearGradient: new GradientParams(new Point(0, 0), new Point(0, 1), [
            { offset: 0, color: appTheme.VividSkyBlue + "77" },
            { offset: 1, color: "Transparent" },
        ]),
        animation: new ScaleAnimation({ zeroLine: -1, duration: 500 }),
    });
    sciChartSurface.renderableSeries.add(lineSeries);

    // Define Horizontal Error Bars Series, Error bars require HLC data with absolute values for error whiskers
    const errorBarsHorizontalSeries = new FastErrorBarsRenderableSeries(wasmContext, {
        dataSeries: new HlcDataSeries(wasmContext, {
            xValues,
            yValues,
            highValues: leftValues,
            lowValues: rightValues,
        }),
        errorMode: EErrorMode.Both,
        errorDirection: EErrorDirection.Horizontal,
        dataPointWidthMode: EDataPointWidthMode.Relative,
        dataPointWidth: 0.3,
        strokeThickness: 4,
        stroke: appTheme.VividSkyBlue + "77",
        animation: new ScaleAnimation({ zeroLine: 0, duration: 500 }),
    });
    sciChartSurface.renderableSeries.add(errorBarsHorizontalSeries);

    // Define Vertical Error Bars Series, Error bars require HLC data with absolute values for error whiskers
    const errorBarsSeries = new FastErrorBarsRenderableSeries(wasmContext, {
        dataSeries: new HlcDataSeries(wasmContext, { xValues, yValues, highValues, lowValues }),
        errorMode: EErrorMode.Both,
        errorDirection: EErrorDirection.Vertical,
        dataPointWidthMode: EDataPointWidthMode.Relative,
        dataPointWidth: 0.3,
        strokeThickness: 4,
        stroke: appTheme.VividSkyBlue,
        animation: new ScaleAnimation({ zeroLine: 0, duration: 500 }),
        // Add optional pointmarker (or use separate XyScatterRenderableSeries)
        pointMarker: new EllipsePointMarker(wasmContext, {
            width: 9,
            height: 9,
            strokeThickness: 0,
            fill: appTheme.VividOrange,
        }),
    });
    sciChartSurface.renderableSeries.add(errorBarsSeries);

    // add some interactivity
    sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
    sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());

    sciChartSurface.zoomExtents();

    return { sciChartSurface };
};

JavaScript Error Bars Chart

Overview

This example demonstrates how to create a high performance JavaScript Error Bars Chart using SciChart.js in JavaScript. It visualizes data uncertainty with both vertical and horizontal error bars, providing developers with a clear method to represent statistical confidence and variability in data points.

Technical Implementation

The chart is initialized by creating a SciChartSurface with a WebAssembly context using the SciChartSurface.create method. Axes are set up using the NumericAxis class with customized growBy settings to ensure proper spacing. Error bars are rendered using the FastErrorBarsRenderableSeries, which uses an HlcDataSeries to provide absolute error values. This approach follows best practices as outlined in The Error Bars Chart documentation. Additionally, a SplineMountainRenderableSeries with a gradient fill and scale animations (detailed in the Spline (Smoothed) Mountain Series Type) is implemented to add visual context and improve aesthetic appeal.

Features and Capabilities

Key features of this implementation include dual error bars for both horizontal and vertical directions, smooth scale animations, and high performance rendering through WebGL. The example leverages interactive chart modifiers such as zooming and panning to offer users a dynamic and responsive experience. The integration of custom point markers further enhances the visualization, making it easier to identify specific data points.

Integration and Best Practices

Built using JavaScript, this example emphasizes clean initialization and resource management. The chart is created within a dedicated function that returns a cleanup method to correctly dispose of the SciChartSurface, aligning with the guidelines found in Memory Best Practices. This approach ensures that applications maintain optimal performance and prevent memory leaks. Developers are encouraged to explore the linked documentation for further details on initializing WebAssembly contexts, configuring numeric axes, and optimizing interactive chart performance.