High Precision Date Axis

Demonstrates the DateTimeNumericAxis with 64-bit precision, handling ranges from Nanoseconds to Billions of Years.

src/drawExample.ts
import {
    DateTimeNumericAxis,
    EllipsePointMarker,
    MouseWheelZoomModifier,
    RubberBandXyZoomModifier,
    ZoomExtentsModifier,
    ZoomPanModifier,
    XyDataSeries,
    NumericAxis,
    FastLineRenderableSeries,
    SciChartSurface,
    NumberRange,
    EAutoRange,
    EAxisAlignment,
    EExecuteOn,
    SmartDateLabelProvider,
    EHighPrecisionLabelMode,
    EDatePrecision,
    formatNumber,
    ENumericFormat,
} from "scichart";
import { TDatasetId, createDatasets } from "./createDatasets";
import { appTheme } from "./appTheme";

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

    const xAxis = new DateTimeNumericAxis(wasmContext, {
        axisAlignment: EAxisAlignment.Bottom,
        useNativeText: true,
        growBy: new NumberRange(0.05, 0.05),
        labelProvider: new SmartDateLabelProvider({
            highPrecisionLabelMode: EHighPrecisionLabelMode.Suffix,
            datePrecision: EDatePrecision.Nanoseconds,
            // the default dataset is using nanosecond precision,
            // this will be changed when switching datasets

            showWiderDateOnFirstLabel: true,
            showYearOnWiderDate: true,

            showSecondsOnPreciseDate: true,
            showSecondsOnWideDate: false,
        }),
    });

    // Special handling for huge dates that do not fit in standard JS Date object
    const sdlp = xAxis.labelProvider as SmartDateLabelProvider;
    const YEAR = 86400 * 365.25;
    const originalWideDateFormatter = sdlp.formatDateWide.bind(sdlp);
    const originalPreciseDateFormatter = sdlp.formatDatePrecise.bind(sdlp);

    sdlp.formatDateWide = (labelRange: string, valueInSeconds: number): string => {
        // even with huge values, use k, m, b suffixes for thousands, millions, billions or years
        if (valueInSeconds > YEAR * 9999) {
            return "Year " + formatNumber(valueInSeconds / YEAR, ENumericFormat.Engineering, 1); // e.g. "Year 500M" -> year 500 million
        }
        return originalWideDateFormatter(labelRange, valueInSeconds);
    };
    sdlp.formatDatePrecise = (labelRange: string, valueInSeconds: number, rawValue?: number): string => {
        if (valueInSeconds > YEAR * 9999) {
            const cutTime = valueInSeconds % 86400; // a bit of a hack, since we can't format dates hours from year 500 Million :)
            return originalPreciseDateFormatter(labelRange, cutTime, rawValue);
        }
        return originalPreciseDateFormatter(labelRange, valueInSeconds, rawValue);
    };
    sciChartSurface.xAxes.add(xAxis);

    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            axisAlignment: EAxisAlignment.Left,
            growBy: new NumberRange(0.1, 0.1),
            labelPrecision: 3,
            autoRange: EAutoRange.Always,
        })
    );

    const lineSeries = new FastLineRenderableSeries(wasmContext, {
        dataSeries: new XyDataSeries(wasmContext, {
            containsNaN: false,
            isSorted: true,
            // x/y values will be appended when switching datasets, for now just create empty data series
        }),
        strokeThickness: 2,
        pointMarker: new EllipsePointMarker(wasmContext, {
            fill: appTheme.ForegroundColor,
        }),
    });

    sciChartSurface.renderableSeries.add(lineSeries);

    // Add modifiers
    sciChartSurface.chartModifiers.add(
        new MouseWheelZoomModifier(),
        new ZoomPanModifier(),
        new ZoomExtentsModifier(),
        new RubberBandXyZoomModifier({ executeCondition: { button: EExecuteOn.MouseRightButton } })
    );
    sciChartSurface.zoomExtents();

    // Datasets handling
    const datasets = createDatasets();

    const useDataset = (id: TDatasetId) => {
        const next = datasets.find((d) => d.id === id)!;
        const dataSeries = lineSeries.dataSeries as XyDataSeries;
        dataSeries.clear();
        dataSeries.appendRange(next.xValues, next.yValues);

        const sdlp = xAxis.labelProvider as SmartDateLabelProvider;
        switch (id) {
            case "secondPrecision":
                lineSeries.stroke = appTheme.VividOrange;
                sdlp.datePrecision = EDatePrecision.Seconds;
                sdlp.showSecondsOnPreciseDate = true;
                break;
            case "millisecondPrecision":
                lineSeries.stroke = appTheme.VividPink;
                sdlp.datePrecision = EDatePrecision.Milliseconds;
                sdlp.showSecondsOnPreciseDate = false;
                break;
            case "microsecondPrecision":
                lineSeries.stroke = appTheme.VividPurple;
                sdlp.datePrecision = EDatePrecision.Microseconds;
                sdlp.showSecondsOnPreciseDate = true;
                break;
            case "nanosecondPrecision":
                lineSeries.stroke = appTheme.VividTeal;
                sdlp.datePrecision = EDatePrecision.Nanoseconds;
                sdlp.showSecondsOnPreciseDate = true;
                break;
        }
        sciChartSurface.zoomExtents();
    };
    useDataset("nanosecondPrecision"); // default to nanosecond precision

    return {
        wasmContext,
        sciChartSurface,
        controls: {
            zoomInPrecise: () => {
                const clusterIndex = 15; // on which cluster to zoom in
                const pointsPerCluster = 10;

                const dataSeries = lineSeries.dataSeries as XyDataSeries;
                const xValues = dataSeries.getNativeXValues();

                const startDataIndex = clusterIndex * pointsPerCluster;
                const endDataIndex = startDataIndex + (pointsPerCluster - 1);

                if (endDataIndex >= xValues.size()) return;

                const minX = xValues.get(startDataIndex);
                const maxX = xValues.get(endDataIndex);

                let padding = 3;
                xAxis.animateVisibleRange(
                    new NumberRange(minX - padding, maxX + padding),
                    3000,
                    (t) => 1 - Math.pow(1 - t, 10) // very exaggerated ease out :)
                );
            },
            zoomOut: () => {
                sciChartSurface.zoomExtents(2000);
            },
            useDataset,
        },
    };
};

High Precision Date Axis Example - React

Overview

This example demonstrates the extreme precision capabilities of the DateTimeNumericAxis in SciChart.js within a React application.

Standard WebGL rendering and many JavaScript charting libraries are limited to 32-bit floating-point coordinates. This limitation typically causes rendering artifacts (jitter) or precision loss when dealing with very large numbers (like astronomical dates) or very small increments (like nanoseconds). SciChart.js utilizes a proprietary 64-bit floating-point coordinate pipeline to overcome this, allowing for visualization of data across vast timescales without loss of detail.

The 4 Precision Ranges

This demo allows you to toggle between four distinct datasets to showcase this flexibility:

  • Nanosecond Precision: A range of 50 Days. Demonstrates extreme detail (1 unit = 1e-9 seconds). Suitable for high-frequency trading or scientific instrumentation.
  • Microsecond Precision: A range of 40 Years. (1 unit = 1e-6 seconds).
  • Millisecond Precision: A range of 70,000 Years. Standard Javascript Date precision, but pushed to historical limits.
  • Seconds Precision: A range of 1 Billion Years. Demonstrates handling values that far exceed the limits of the standard JavaScript Date object (which is limited to approx. 270,000 years).

Technical Implementation

The SmartDateLabelProvider is key to this functionality. By setting the datePrecision property, we instruct the chart how to interpret the raw Float64 data:

  • EDatePrecision.Nanoseconds: Divides input by 1,000,000,000 to get seconds.
  • EDatePrecision.Microseconds: Divides input by 1,000,000.

Furthermore, the highPrecisionLabelMode allows formatting sub-millisecond data using:

Suffix (e.g., '50ns'),

Fractional (e.g., '.000000050')

Scientific (e.g., '5.0e-8') notation, ensuring labels remain readable even at maximum zoom depth.