React Chart with Logarithmic Axis Example

Demonstrates how to create a React Chart with Logarithmic axis using SciChart.js, High Performance JavaScript Charts

src/drawExample.ts
import {
    ENumericFormat,
    EllipsePointMarker,
    FastLineRenderableSeries,
    LegendModifier,
    LogarithmicAxis,
    MouseWheelZoomModifier,
    NumericAxis,
    RubberBandXyZoomModifier,
    SciChartSurface,
    SweepAnimation,
    XyDataSeries,
    Thickness,
    ZoomExtentsModifier,
} from "scichart";
import { appTheme, createChartTheme } from "./appTheme";
import { ExampleDataProvider } from "./ExampleDataProvider";

const Y_AXIS_LINEAR_ID = "Y_AXIS_LINEAR_ID";
const X_AXIS_LINEAR_ID = "X_AXIS_LINEAR_ID";

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    // Create a SciChartSurface
    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
        theme: createChartTheme({
            majorGridLineBrush: appTheme.MutedSkyBlue + "55",
            minorGridLineBrush: appTheme.MutedSkyBlue + "22",
        }),
        title: "Logarithmic X & Y Axis",
        titleStyle: {
            fontSize: 20,
            fontWeight: "bold",
            placeWithinChart: true,
            color: appTheme.ForegroundColor + "C4",
            padding: new Thickness(10, 0, 4),
        },
    });

    // Create an X and Y Axis
    const xAxisLogarithmic = new LogarithmicAxis(wasmContext, {
        logBase: 10,
        labelFormat: ENumericFormat.Scientific,
        labelPrecision: 2,
        minorsPerMajor: 10,
        useNativeText: false,
    });
    sciChartSurface.xAxes.add(xAxisLogarithmic);

    // The LogarithmicAxis will apply logarithmic scaling and labelling to your data.
    // Simply replace a NumericAxis for a LogarithmicAxis on X or Y to apply this scaling
    // Note options logBase, labelFormat which lets you specify exponent on labels
    const yAxisLogarithmic = new LogarithmicAxis(wasmContext, {
        logBase: 10,
        labelFormat: ENumericFormat.Scientific,
        labelPrecision: 2,
        minorsPerMajor: 10,
        useNativeText: false,
    });
    sciChartSurface.yAxes.add(yAxisLogarithmic);

    const xAxisLinear = new NumericAxis(wasmContext, {
        labelFormat: ENumericFormat.Decimal,
        labelPrecision: 2,
        isVisible: false,
        id: X_AXIS_LINEAR_ID,
    });
    sciChartSurface.xAxes.add(xAxisLinear);

    const yAxisLinear = new NumericAxis(wasmContext, {
        labelFormat: ENumericFormat.Decimal,
        labelPrecision: 2,
        isVisible: false,
        id: Y_AXIS_LINEAR_ID,
    });
    sciChartSurface.yAxes.add(yAxisLinear);

    // Create some data
    const data0 = ExampleDataProvider.getExponentialCurve(2, 100);
    const data1 = ExampleDataProvider.getExponentialCurve(2.2, 100);
    const data2 = ExampleDataProvider.getExponentialCurve(2.4, 100);

    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, {
                xValues: data0.xValues,
                yValues: data0.yValues,
                dataSeriesName: "y = x<sup>2</sup>",
            }),
            stroke: appTheme.VividSkyBlue,
            strokeThickness: 3,
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 7,
                height: 7,
                fill: appTheme.VividSkyBlue,
                strokeThickness: 0,
            }),
            animation: new SweepAnimation({ duration: 800, delay: 0 }),
        })
    );

    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, {
                xValues: data1.xValues,
                yValues: data1.yValues,
                dataSeriesName: "y = x<sup>2.2</sup>",
            }),
            stroke: appTheme.VividPink,
            strokeThickness: 3,
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 7,
                height: 7,
                fill: appTheme.VividPink,
                strokeThickness: 0,
            }),
            animation: new SweepAnimation({ duration: 800, delay: 0 }),
        })
    );

    sciChartSurface.renderableSeries.add(
        new FastLineRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, {
                xValues: data2.xValues,
                yValues: data2.yValues,
                dataSeriesName: "y = x<sup>2.4</sup>",
            }),
            stroke: appTheme.VividOrange,
            strokeThickness: 3,
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 7,
                height: 7,
                fill: appTheme.VividOrange,
                strokeThickness: 0,
            }),
            animation: new SweepAnimation({ duration: 800, delay: 0 }),
        })
    );

    // Add some interactivity modifiers
    sciChartSurface.chartModifiers.add(
        new RubberBandXyZoomModifier(),
        new MouseWheelZoomModifier(),
        new ZoomExtentsModifier(),
        new LegendModifier({ showCheckboxes: false })
    );

    sciChartSurface.zoomExtents();
    return {
        sciChartSurface,
        wasmContext,
        yAxisLogarithmic,
        yAxisLinear,
        xAxisLinear,
        xAxisLogarithmic,
    };
};

Logarithmic Axis Example in React

Overview

This example demonstrates how to create a high performance chart using SciChart.js in a React application. It focuses on implementing logarithmic axes on both the X and Y axes and provides functionality for dynamically switching between logarithmic and linear representations using interactive controls.

Technical Implementation

The chart is implemented using the <SciChartReact/> component for React integration, with asynchronous initialization of the SciChartSurface ensuring a responsive and efficient setup. Interactive modifiers such as RubberBandZoomModifier, MouseWheelZoomModifier, and ZoomExtentsModifier are integrated to enable advanced chart interactions. Custom point markers and sweep animations are used to enhance visual feedback. For detailed information on integrating SciChart.js with React, refer to the React Charts with SciChart.js: Introducing "SciChart React" documentation.

The dynamic axis switching is managed by toggling the visibility and primary status of LogarithmicAxis and linear NumericAxis. This approach updates the FastLineRenderableSeries with the correct xAxisId and yAxisId properties based on the selected configuration. This method aligns with best practices described in the LogarithmicAxis Documentation - SciChart resource.

Features and Capabilities

The example offers dynamic update capabilities by allowing users to switch between three configurations: Logarithmic on both axes, Logarithmic X with Linear Y, and Linear on both axes. It leverages advanced features such as custom point markers for data visualization and sweep animations for smooth transitions. The inclusion of interactive modifiers ensures that users have control over zooming and panning, enhancing overall usability and performance.

Integration and Best Practices

The integration utilizes shared CSS controls's button group within the React environment to facilitate dynamic chart controls. This effective combination of React state management and SciChart.js configuration demonstrates scalable best practices for real-time data visualization. Developers can learn more about asynchronous initialization and component reuse in React from the Creating a SciChart React Component from the Ground Up guide. Additionally, the use of interactive behaviors is further detailed in the Tutorial 03 - Adding Zooming, Panning Behavior documentation, providing valuable insights for optimizing performance in dynamic charting applications.