Datapoint Metadata Tooltips on JavaScript Chart

Demonstrates how to add and use MetaData in a chart using SciChart.js, High Performance JavaScript Charts

src/drawExample.ts
import { appTheme } from "./appTheme";

import {
    DataLabelState,
    ECoordinateMode,
    EHorizontalAnchorPoint,
    EStrokePaletteMode,
    IPointMetadata,
    IPointMarkerPaletteProvider,
    IRenderableSeries,
    LineSeriesDataLabelProvider,
    NumericAxis,
    NumberRange,
    parseColorToUIntArgb,
    SciChartSurface,
    SplineLineRenderableSeries,
    TextAnnotation,
    TPointMarkerArgb,
    TWebAssemblyChart,
    Thickness,
    XyDataSeries,
    XySeriesInfo,
    EllipsePointMarker,
    RolloverModifier,
} from "scichart";

export const drawExample = async (rootElement: string | HTMLDivElement): Promise<TWebAssemblyChart> => {
    // Create a chart with X, Y axis
    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
    });
    sciChartSurface.xAxes.add(new NumericAxis(wasmContext));
    sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { growBy: new NumberRange(0.2, 0.2) }));

    // Given a dataset with X, Y but also additional values in the form of an object of any kind
    const dataValues = [
        { x: 0, y: 50, anObject: { label: "", pointColor: "#F48420", isSelected: false } },
        { x: 1, y: 35, anObject: { label: "Orange Point", pointColor: "#F48420", isSelected: false } },
        { x: 2, y: 68, anObject: { label: "Highest Point", pointColor: "#7BCAAB", isSelected: false } },
        { x: 3, y: 58, anObject: { label: "Selected Point", pointColor: "#F48420", isSelected: true } },
        { x: 4, y: 50, anObject: { label: "Orange Point", pointColor: "#F48420", isSelected: false } },
        { x: 5, y: 50, anObject: { label: "", pointColor: "#F48420", isSelected: false } },
        { x: 6, y: 40, anObject: { label: "Blue Point", pointColor: "#50C7E0", isSelected: false } },
        { x: 7, y: 53, anObject: { label: "Selected Point", pointColor: "#F48420", isSelected: true } },
        { x: 8, y: 55, anObject: { label: "", pointColor: "#F48420", isSelected: false } },
        { x: 9, y: 23, anObject: { label: "Blue Point", pointColor: "#50C7E0", isSelected: false } },
        { x: 10, y: 45, anObject: { label: "Selected Point", pointColor: "#F48420", isSelected: true } },
        { x: 11, y: 12, anObject: { label: "Lowest Point", pointColor: "#EC0F6C", isSelected: false } },
        { x: 12, y: 59, anObject: { label: "", pointColor: "#F48420", isSelected: false } },
        { x: 13, y: 60, anObject: { label: "", pointColor: "#F48420", isSelected: false } },
    ];

    // You can create a dataseries with these object values as metadata
    const xyDataSeriesWithMetadata = new XyDataSeries(wasmContext, {
        xValues: dataValues.map((row) => row.x),
        yValues: dataValues.map((row) => row.y),
        metadata: dataValues.map((row) => row.anObject), // put any javascript object here
    });

    // You can assign this dataseries to a RenderableSeries in SciChart
    const lineSeries = new SplineLineRenderableSeries(wasmContext, {
        dataSeries: xyDataSeriesWithMetadata,
        stroke: appTheme.VividSkyBlue,
        strokeThickness: 3,
        pointMarker: new EllipsePointMarker(wasmContext, {
            width: 15,
            height: 15,
            strokeThickness: 0,
        }),
    });

    // Now you can consume metadata in the following ways
    // - Colouring points
    // - Labelling points
    // - Custom data for tooltips
    // - Tagging datapoints with the objects for later use
    //

    // 1. via paletteprovider (colour points or segments based on metadata values)

    // @ts-ignore
    const getColorFromMetadata = (metadata) => {
        // @ts-ignore
        const pointColorArgb = parseColorToUIntArgb(metadata.pointColor);
        const selectedColorArgb = 0xffffffff;
        const fill = metadata.isSelected ? selectedColorArgb : pointColorArgb;
        return fill;
    };

    const pointPaletteProvider: IPointMarkerPaletteProvider = {
        strokePaletteMode: EStrokePaletteMode.SOLID,
        onAttached(parentSeries: IRenderableSeries): void {},
        onDetached(): void {},
        overridePointMarkerArgb(
            xValue: number,
            yValue: number,
            index: number,
            opacity?: number,
            metadata?: IPointMetadata
        ): TPointMarkerArgb {
            // Metadata values can be used in paletteprovider overrides
            if (metadata) {
                const fill = getColorFromMetadata(metadata);
                return { stroke: fill, fill };
            }
            return undefined; // means use default colour
        },
    };
    lineSeries.paletteProvider = pointPaletteProvider;

    // 2. Via DataLabel provider
    const dataLabelProvider = new LineSeriesDataLabelProvider({
        // @ts-ignore
        metaDataSelector: (metadata) => metadata.label, // This is how you route a label (string) from metadata to data-labels in scichart
        style: { fontFamily: "Arial", fontSize: 16, padding: new Thickness(5, 5, 5, 5) },
        color: appTheme.ForegroundColor,
    });
    lineSeries.dataLabelProvider = dataLabelProvider;
    // This is how you override colors of labels on a per-label basis, which can also come from metadata
    dataLabelProvider.getColor = (state: DataLabelState, label: string) => {
        const metadata = state.getMetaData();
        return getColorFromMetadata(metadata);
    };

    // 3. Via cursors and tooltips
    lineSeries.rolloverModifierProps.markerColor = appTheme.DarkIndigo;
    lineSeries.rolloverModifierProps.tooltipColor = appTheme.Indigo;
    lineSeries.rolloverModifierProps.tooltipDataTemplate = (seriesInfo: XySeriesInfo): string[] => {
        const valuesWithLabels: string[] = [];
        // Line Series
        const xySeriesInfo = seriesInfo as XySeriesInfo;

        valuesWithLabels.push("X Value: " + xySeriesInfo.formattedXValue);
        valuesWithLabels.push("Y Value: " + xySeriesInfo.formattedYValue);

        valuesWithLabels.push(" ");
        if (seriesInfo.pointMetadata) {
            // @ts-ignore
            let label = seriesInfo.pointMetadata.label;
            label = label === "" ? "..." : label;
            valuesWithLabels.push(`Metadata Label: "${label}"`);
            // @ts-ignore
            valuesWithLabels.push("Metadata Selected: " + seriesInfo.pointMetadata.isSelected);
        }
        return valuesWithLabels;
    };

    sciChartSurface.renderableSeries.add(lineSeries);

    // Add a RolloverModifier for tooltips
    sciChartSurface.chartModifiers.add(
        new RolloverModifier({
            showRolloverLine: false,
            showTooltip: true,
        })
    );

    // Add title annotation
    sciChartSurface.annotations.add(
        new TextAnnotation({
            text: "Line Chart with Metadata (Objects per data-point)",
            fontSize: 18,
            textColor: appTheme.ForegroundColor,
            x1: 0.5,
            y1: 0,
            opacity: 0.77,
            horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
            xCoordinateMode: ECoordinateMode.Relative,
            yCoordinateMode: ECoordinateMode.Relative,
        })
    );

    sciChartSurface.zoomExtents();
    return { sciChartSurface, wasmContext };
};

MetaData Labels, Tooltips and Rendering Example - JavaScript

Overview

This example, demonstrates how to create an interactive line chart using SciChart.js with JavaScript. It visualizes a dataset where each Xy data point is augmented with metadata that drives customizations such as point colors, dynamic labels, and interactive tooltips.

Technical Implementation

The chart is set up by asynchronously initializing a SciChartSurface with configured X and Y axes using the NumericAxis class and a defined growBy range via NumberRange. An XyDataSeries is created by mapping the provided X and Y values and attaching a metadata object (a custom JavaScript object) for each Xy data point. Customizations are applied through a custom PaletteProvider that adjusts the color of each point marker based on metadata. This is achieved using the helper function parseColorToUIntArgb for color conversion. Additionally, a custom DataLabelProvider extracts labels from metadata with a metaDataSelector, following concepts from Getting Labels from Metadata. A RolloverModifier is employed to provide interactive tooltips, enabling dynamic adjustments of marker and tooltip colors based on the underlying data. Text annotations are added using the TextAnnotation class, which supports relative coordinate modes for flexible placement.

Features and Capabilities

This example showcases metadata-driven customization where additional properties within each data point dictate not only the visual styling (such as point marker color via a custom palette provider) but also the data labels and tooltips. Such an approach allows each data point to be individually styled and can support features like highlighting or selection. Developers seeking to further understand the concepts can read about the DataSeries Metadata API here.

Integration and Best Practices

The implementation follows best practices for asynchronous initialization and resource cleanup in JavaScript, as shown by returning a destructor function that disposes of the SciChartSurface when no longer needed. This pattern is recommended in the Getting Started with SciChart JS guide. Although a React wrapper is provided in the project, the core implementation uses plain JavaScript, making it highly adaptable to various environments. By leveraging these techniques, developers can build highly interactive charts with a focus on performance and maintainability.