Demonstrates how create React Charts with dashed lines using SciChart.js, High Performance JavaScript Charts
import { appTheme } from "./appTheme";
import { ExampleDataProvider } from "./ExampleDataProvider";
import {
ENumericFormat,
FastBandRenderableSeries,
FastLineRenderableSeries,
FastMountainRenderableSeries,
GradientParams,
MouseWheelZoomModifier,
NumberRange,
NumericAxis,
Point,
SciChartSurface,
TSciChart,
XyDataSeries,
XyyDataSeries,
ZoomExtentsModifier,
ZoomPanModifier,
} from "scichart";
// tslint:disable:no-empty
// tslint:disable:max-classes-per-file
export const drawExample = async (rootElement: string | HTMLDivElement) => {
const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
theme: appTheme.SciChartJsTheme,
});
// Create XAxis
sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { labelFormat: ENumericFormat.Decimal, labelPrecision: 2 }));
// Create YAxis
sciChartSurface.yAxes.add(
new NumericAxis(wasmContext, {
growBy: new NumberRange(0.1, 0.1),
})
);
// Create a Mountain series with a dashed line
sciChartSurface.renderableSeries.add(
new FastMountainRenderableSeries(wasmContext, {
stroke: "SteelBlue",
fillLinearGradient: new GradientParams(new Point(0, 0), new Point(0, 1), [
{ color: appTheme.VividSkyBlue + "77", offset: 0 },
{ color: "Transparent", offset: 0.5 },
]),
strokeThickness: 3,
dataSeries: createLineData(wasmContext, 2),
// Strokedash array defines the stroke dash. [10,5] means draw for 10pts, gap for 5pts
strokeDashArray: [10, 5],
})
);
// Create a line series with a dotted line
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
stroke: appTheme.VividSkyBlue,
strokeThickness: 2,
dataSeries: createLineData(wasmContext, 1),
// Strokedash array defines the stroke dash. [5,5] means draw for 5pts, gap for 5pts
strokeDashArray: [5, 5],
})
);
// Create a line series a dotted line
sciChartSurface.renderableSeries.add(
new FastLineRenderableSeries(wasmContext, {
stroke: appTheme.VividSkyBlue,
strokeThickness: 2,
dataSeries: createLineData(wasmContext, 0),
// Strokedash array defines the stroke dash. [3,3] means draw for 3pt, gap for 3pt
strokeDashArray: [3, 3],
})
);
// Create a band series with dashed lines and add to the chart
sciChartSurface.renderableSeries.add(
new FastBandRenderableSeries(wasmContext, {
dataSeries: createBandData(wasmContext),
strokeThickness: 2,
fill: appTheme.VividSkyBlue + "33",
fillY1: appTheme.VividSkyBlue + "33",
stroke: appTheme.VividSkyBlue,
strokeY1: appTheme.VividSkyBlue,
// strokeDashArray: [10, 10],
// strokeY1DashArray: [3, 3]
})
);
// Add some interactivity modifiers
sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());
sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
sciChartSurface.zoomExtents();
return { sciChartSurface, wasmContext };
};
// Creates some dummy data and appends into an XyDataSeries for the example
const createLineData = (wasmContext: TSciChart, whichSeries: number) => {
const data = ExampleDataProvider.getFourierSeriesZoomed(1.0, 0.1, 5.0, 5.15);
const xyDataSeries = new XyDataSeries(wasmContext);
xyDataSeries.appendRange(
data.xValues,
data.yValues.map((y) => (whichSeries === 0 ? y : whichSeries === 1 ? y * 1.1 : y * 1.5))
);
return xyDataSeries;
};
const createBandData = (wasmContext: TSciChart) => {
const data0 = ExampleDataProvider.getFourierSeriesZoomed(0.6, 0.13, 5.0, 5.15);
const data1 = ExampleDataProvider.getFourierSeriesZoomed(0.5, 0.12, 5.0, 5.15);
const xyyDataSeries = new XyyDataSeries(wasmContext);
xyyDataSeries.appendRange(data0.xValues, data0.yValues, data1.yValues);
return xyyDataSeries;
};
This example demonstrates how to create high performance SciChart.js charts within a React application. It focuses on advanced styling techniques including dashed and dotted line patterns alongside gradient fills, all rendered using WebGL for optimal performance.
In this example, a React component utilizes the <SciChartReact/> component to asynchronously initialize a SciChartSurface through the initChart prop. The chart is built by creating numeric axes and multiple renderable series with distinct stroke dash arrays (for instance, a mountain series with a strokeDashArray: [10,5] dash pattern and line series with strokeDashArray: [5,5] and strokeDashArray: [3,3] dash arrays). Gradient fills are applied to the mountain series using gradient parameters. Detailed techniques for dashed line styling can be found in the SciChart.js Dash Line Styling Documentation.
The implementation supports a variety of series including a mountain series with gradient fills, two line series with varying dash styles, and a band series, demonstrating the extensive customization possibilities of SciChart.js. In addition, interactive modifiers such as ZoomPanModifier, MouseWheelZoomModifier, and ZoomExtentsModifier enhance the real-time responsiveness and user experience of the chart.
By leveraging React integration best practices, the example illustrates efficient asynchronous chart setup and resource management. Developers can refer to the Creating a SciChart React Component from the Ground Up tutorial for deeper insights into component lifecycle and optimization techniques. This approach ensures that the high performance capabilities of SciChart.js are fully utilized within React, providing a robust and maintainable solution for dynamic charting applications.










