Dashed Line Styling

Demonstrates how create JavaScript Charts with dashed lines using SciChart.js, High Performance JavaScript Charts

src/drawExample.ts
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;
};

Dashed Line Styling - JavaScript

Overview

This example demonstrates the use of SciChart.js to create high performance dashed line charts using JavaScript. The focus is on rendering series with advanced dashed and dotted line styles along with gradient fills, providing a clear illustration of custom styling capabilities.

Technical Implementation

The chart is initialized asynchronously using SciChartSurface.create() in a dedicated drawExample function. Numeric axes are configured with custom label formatting and precision. Several renderable series are added including a mountain (area) series with a gradient fill implemented using GradientParams as described in The Mountain (Area) Series Type and multiple line series with varying strokeDashArray values to achieve dashed and dotted effects (refer to Series Styling - Dash Line Patterns for more details). Interactive modifiers such as ZoomPanModifier, MouseWheelZoomModifier, and ZoomExtentsModifier further enhance user interaction, while the use of the WebAssembly context (wasmContext) aids performance as noted in Performance Tips & Tricks.

Features and Capabilities

The example highlights advanced styling techniques with a mountain series featuring a strokeDashArray: [10,5] dash pattern along with gradient fills, and additional line series using strokeDashArray: [5,5] and strokeDashArray: [3,3] patterns for dotted effects. A band series adds further visual context, and interactive modifiers provide real-time dynamic updates.

Integration and Best Practices

This JavaScript implementation exemplifies best practices for asynchronous chart initialization and resource management. Charts are created by invoking a single asynchronous method and are properly disposed of using sciChartSurface.delete(), in line with guidelines found in the Getting Started with SciChart JS and Memory Best Practices documentation.