JavaScript Digital Line Chart

Demonstrates how to create a JavaScript Digital Line Chart using SciChart.js, our powerful JavaScript Charts

src/drawExample.ts
import {
    EllipsePointMarker,
    FastLineRenderableSeries,
    MouseWheelZoomModifier,
    NumberRange,
    NumericAxis,
    ScaleAnimation,
    SciChartSurface,
    XyDataSeries,
    ZoomExtentsModifier,
    ZoomPanModifier,
} from "scichart";
import { RandomWalkGenerator } from "./RandomWalkGenerator";
import { appTheme } from "./appTheme";

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

    // Create the X,Y Axis
    sciChartSurface.xAxes.add(new NumericAxis(wasmContext));
    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.05, 0.05) }));

    // Create some xValues, yValues arrays
    const { xValues, yValues } = new RandomWalkGenerator().Seed(1337).getRandomWalkSeries(25);

    // Create and add a line series to the chart
    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, { xValues, yValues }),
            stroke: appTheme.VividPink,
            strokeThickness: 3,
            // Flag isDigitalLine turns a normal line series into a digital line series
            isDigitalLine: true,
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 11,
                height: 11,
                fill: appTheme.VividPink,
                stroke: appTheme.PaleSkyBlue,
                strokeThickness: 2,
            }),
            animation: new ScaleAnimation({ zeroLine: -1, duration: 500, fadeEffect: true }),
        })
    );

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

    // Zoom to fit
    sciChartSurface.zoomExtents();

    return { sciChartSurface, wasmContext };
};

Digital Line Chart Example in JavaScript

Overview

This example demonstrates how to create a Digital Line Chart using SciChart.js in a JavaScript environment. It highlights the process of setting up a high-performance chart that renders a digital (step) line series with interactive capabilities and resource-efficient WebAssembly utilization.

Technical Implementation

The chart is initialized within an asynchronous function that creates a SciChartSurface along with its WebAssembly context as described in the Getting Started with SciChart JS guide. Numeric axes are configured using the NumericAxis class and enhanced with a growBy value via a NumberRange object to ensure proper padding. The data is generated using a random walk algorithm and plotted using an XyDataSeries. The digital line series is implemented by setting the isDigitalLine flag to true in a FastLineRenderableSeries, and a custom EllipsePointMarker is applied to clearly display data points. For more on digital line configuration, refer to the Digital (Step) Line Series documentation.

Features and Capabilities

This example incorporates several advanced features. It utilizes ScaleAnimation to animate the chart series for a dynamic visual effect and integrates interactive modifiers such as ZoomPanModifier, ZoomExtentsModifier, and the MouseWheelZoomModifier to allow users to zoom and pan seamlessly. The use of WebAssembly enables high-performance rendering, a key aspect in building responsive, real-time applications, as detailed in WebAssembly performance optimization.

Integration and Best Practices

The integration in a JavaScript setup is designed for ease of use. The initialization function directly creates the chart and returns a cleanup (destructor) function to ensure proper resource disposal, aligning well with the Memory Best Practices. This pattern not only streamlines the process of embedding SciChart.js into web projects but also optimizes performance by leveraging asynchronous initialization patterns as highlighted in the Tutorial on setting up a project with SciChart.js.