This SciChart demo demonstrates how to create a React Error Bars Chart using SciChart.js our High Performance JavaScript Chart component.
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 };
};
This example demonstrates how to create an Error Bars Chart in a React application using SciChart.js. The implementation uses the <SciChartReact/> component to initialize the chart via the drawExample function, offering a high performance, interactive chart experience with both vertical and horizontal error bars.
The chart is set up by creating a SciChartSurface with preconfigured numeric axes and then adding multiple renderable series. A mountain series with a gradient fill is added for visual context, while two FastErrorBarsRenderableSeries are used to demonstrate both horizontal and vertical error bars. Data for error bars is generated dynamically using absolute values via HlcDataSeries. The chart also integrates smooth animations with the EAnimationType Scale configuration, providing a visually appealing entry effect. Developers looking to further understand the React integration can refer to the Creating a SciChart React Component from the Ground Up documentation.
Key features of this example include the use of both horizontal and vertical error bars to represent uncertainty in data points. The chart leverages performance optimizations through the FastErrorBarsRenderableSeries, ensuring that rendering remains efficient even with dynamic data updates. Additionally, the example showcases advanced customization such as gradient fills and custom point markers with EllipsePointMarker, enhancing the visual appeal. For further details about error bar configuration, see The Error Bars Chart Type.
This example exemplifies best practices for integrating SciChart.js into a React application. The <SciChartReact/> component is used to initialize charts with the drawExample pattern, ensuring a clean separation between the React component and chart creation logic. Interactive chart modifiers such as ZoomPanModifier, ZoomExtentsModifier, and MouseWheelZoomModifier are incorporated to allow users to effortlessly explore the data. Developers interested in reactive chart integration and performance strategies should review React Charts with SciChart.js: Introducing "SciChart React" and the Performance Tips & Tricks documentation for advanced optimization techniques.








































