React Bubble Chart

Demonstrates how to create a React Bubble Chart This is a JavaScript Chart type which draws point-markers (Ellipse, Square, Triangle, Circle) at X,Y locations

src/drawExample.ts
import {
    XyDataSeries,
    XyzDataSeries,
    NumericAxis,
    FastBubbleRenderableSeries,
    SciChartSurface,
    NumberRange,
    EllipsePointMarker,
    EFillPaletteMode,
    EStrokePaletteMode,
    IPointMarkerPaletteProvider,
    TPointMarkerArgb,
    IRenderableSeries,
    IPointMetadata,
    MouseWheelZoomModifier,
    ZoomExtentsModifier,
    ZoomPanModifier,
    parseColorToUIntArgb,
    SplineLineRenderableSeries,
    SweepAnimation,
} from "scichart";

import { appTheme } from "./appTheme";

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    // Create a SciChartSurface 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.05, 0.05) }));

    // Create some data
    const xValues = [];
    const yValues = [];
    const zValues = [];
    let prevYValue = 0;
    for (let i = 0; i < 20; i++) {
        const curYValue = Math.sin(i) * 10 + 5;
        const size = Math.sin(i) * 60 + 3;

        xValues.push(i);
        yValues.push(prevYValue + curYValue);
        zValues.push(size);

        prevYValue += curYValue;
    }

    // Create and add a line series to the chart
    sciChartSurface.renderableSeries.add(
        new SplineLineRenderableSeries(wasmContext, {
            dataSeries: new XyDataSeries(wasmContext, { xValues, yValues }),
            stroke: appTheme.PaleSkyBlue,
            strokeThickness: 3,
            animation: new SweepAnimation({ duration: 500 }),
        })
    );

    // Create and add a Bubble series to the chart
    // The Bubble series requires a special dataseries type called XyzDataSeries with X,Y and Z (size) values
    sciChartSurface.renderableSeries.add(
        new FastBubbleRenderableSeries(wasmContext, {
            dataSeries: new XyzDataSeries(wasmContext, { xValues, yValues, zValues }),
            // Pointmarker defines the marker shown per-bubble point. This will be scaled according to z-value
            pointMarker: new EllipsePointMarker(wasmContext, {
                width: 64,
                height: 64,
                strokeThickness: 0,
                fill: appTheme.VividSkyBlue + "77",
            }),
            // Optional: Allows per-point colouring of bubble stroke
            paletteProvider: new BubblePaletteProvider(appTheme.VividOrange),
            animation: new SweepAnimation({ delay: 200, duration: 500, fadeEffect: true }),
        })
    );

    // Add some zooming and panning behaviour
    sciChartSurface.chartModifiers.add(new ZoomPanModifier({ enableZoom: true }));
    sciChartSurface.chartModifiers.add(new ZoomExtentsModifier());
    sciChartSurface.chartModifiers.add(new MouseWheelZoomModifier());

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

/**
 * Optional: An example PaletteProvider which implements IPointMarkerPaletteProvider
 * This can be attached to Scatter or Bubble series to change the stroke or fill
 * of the series point-markers conditionally
 */
class BubblePaletteProvider implements IPointMarkerPaletteProvider {
    public readonly fillPaletteMode = EFillPaletteMode.SOLID;
    public readonly strokePaletteMode: EStrokePaletteMode = EStrokePaletteMode.SOLID;
    private readonly fillArgb: number;

    constructor(fillHexString: string) {
        this.fillArgb = parseColorToUIntArgb(fillHexString);
    }

    public onAttached(parentSeries: IRenderableSeries): void {}

    public onDetached(): void {}

    public overridePointMarkerArgb(
        xValue: number,
        yValue: number,
        index: number,
        opacity?: number,
        metadata?: IPointMetadata
    ): TPointMarkerArgb {
        return xValue >= 8 && xValue <= 12 ? { fill: this.fillArgb, stroke: this.fillArgb } : undefined;
    }
}

React Bubble Chart

Overview

This React Charts example demonstrates how to create a high performance React Bubble Chart using SciChart.js.

Technical Implementation

The chart is initialized asynchronously through an initChart function that is passed to the SciChartReact component. In the implementation, a SciChartSurface is created with numeric X and Y axes. Data is structured using an XY data series for the spline line and an XYZ data series for the bubble chart, where the Z value defines the bubble size. Animations are applied using SweepAnimation, and a custom palette provider adjusts bubble colors conditionally. For further guidance, refer to Tutorial 02 - Creating a Chart with scichart-react.

Features and Capabilities

The example showcases advanced features including conditionally styled bubble markers via a custom palette provider, smooth animated transitions, and interactive chart modifiers such as zooming, panning, and mouse wheel zoom. The bubble series leverages the XYZ data series to represent data intensity, a concept detailed in the Bubble Series Type documentation.

Integration and Best Practices

Embracing React best practices, the example integrates SciChart.js through the <SciChartReact/> component, ensuring a modular and maintainable approach. Asynchronous WebGL initialization optimizes performance while providing a responsive user interface. Developers are encouraged to explore further details on React integration and performance optimization techniques in the SciChart React tutorials.