React Stacked Column Side by Side

We have created a simple example that shows you how to create a React Stacked Column Chart Side-by-side using our JavaScript Chart Framework.

src/drawExample.ts
import { appTheme } from "./appTheme";
import {
    ELegendOrientation,
    ELegendPlacement,
    ENumericFormat,
    LegendModifier,
    MouseWheelZoomModifier,
    NumericAxis,
    SciChartSurface,
    StackedColumnCollection,
    StackedColumnRenderableSeries,
    WaveAnimation,
    NumberRange,
    XyDataSeries,
    ZoomExtentsModifier,
    ZoomPanModifier,
    EVerticalTextPosition,
    EColumnDataLabelPosition,
    IStackedColumnSeriesDataLabelProviderOptions,
    Thickness,
} from "scichart";

const xValues = [1997, 1998, 1999, 2000, 2001, 2002, 2003];
const tomatoesData = [15, 17, 26, 22, 28, 21, 22];
const cucumberData = [14, 12, 27, 25, 23, 17, 17];
const pepperData = [17, 14, 27, 26, 22, 28, 16];

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

    // Create XAxis, YAxis
    sciChartSurface.xAxes.add(
        new NumericAxis(wasmContext, {
            labelFormat: ENumericFormat.Decimal,
            labelPrecision: 0,
            autoTicks: false,
            majorDelta: 1,
            minorDelta: 1,
            drawMajorGridLines: false,
            drawMinorGridLines: false,
            drawMajorBands: false,
            axisTitle: "Year",
            growBy: new NumberRange(0.02, 0.02),
        })
    );
    sciChartSurface.yAxes.add(
        new NumericAxis(wasmContext, {
            labelPrecision: 0,
            drawMinorGridLines: false,
            drawMinorTickLines: false,
            axisTitle: "Produce sold (Tonnes)",
            growBy: new NumberRange(0.02, 0.05),
        })
    );

    const dataLabels: IStackedColumnSeriesDataLabelProviderOptions = {
        style: {
            fontSize: 12,
            fontFamily: "Arial",
            padding: new Thickness(0, 0, 2, 0), // lift label above the top by 2 pixels
        },
        positionMode: EColumnDataLabelPosition.Outside,
        verticalTextPosition: EVerticalTextPosition.Center,
        precision: 0,
    };

    // Different stackedGroupIds place the columns side by side.
    const stackedColumnCollection = new StackedColumnCollection(wasmContext, { dataPointWidth: 0.5 });
    [
        { yValues: tomatoesData, dataSeriesName: "Tomato", fill: appTheme.VividPink },
        { yValues: pepperData, dataSeriesName: "Pepper", fill: appTheme.VividOrange },
        { yValues: cucumberData, dataSeriesName: "Cucumber", fill: appTheme.VividSkyBlue },
    ].forEach(({ yValues, dataSeriesName, fill }, index) => {
        stackedColumnCollection.add(
            new StackedColumnRenderableSeries(wasmContext, {
                dataSeries: new XyDataSeries(wasmContext, { xValues, yValues, dataSeriesName }),
                fill,
                stackedGroupId: `Group${index}`,
                dataLabels,
            })
        );
    });
    stackedColumnCollection.animation = new WaveAnimation({ duration: 1000, fadeEffect: true });

    // Add the Stacked Column collection to the chart
    sciChartSurface.renderableSeries.add(stackedColumnCollection);

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

    // Add a legend to the chart to show the series
    sciChartSurface.chartModifiers.add(
        new LegendModifier({
            placement: ELegendPlacement.TopLeft,
            orientation: ELegendOrientation.Vertical,
            showLegend: true,
            showCheckboxes: false,
            showSeriesMarkers: true,
        })
    );

    sciChartSurface.zoomExtents();

    return { wasmContext, sciChartSurface };
};

React Stacked Column Side by Side

Overview

This example demonstrates how to create a stacked column chart side-by-side using SciChart.js within a React framework. The chart visualizes data for produce items by grouping individual series (for tomatoes, peppers, and cucumbers) into separate side-by-side column or bar charts, providing a clear visual comparison of data over a series of years.

Technical Implementation

The chart is constructed using the SciChart.js API by first configuring the X and Y axes with numeric parameters and then creating individual StackedColumnRenderableSeries for each data series. Each series is assigned a unique stacking group identifier (using the property stackedGroupId) to ensure they appear side-by-side rather than being stacked on top of each other. The setup includes the use of interactive modifiers such as ZoomExtentsModifier, ZoomPanModifier, and MouseWheelZoomModifier to enhance usability, and a smooth appearance is achieved through the implementation of WaveAnimation. The React integration is streamlined by using the <SciChartReact/> component with the initChart property, as explained in the Tutorial 02 - Creating a Chart with scichart-react.

Features and Capabilities

This example showcases advanced chart grouping by organizing multiple data series into distinct stacking groups for a side-by-side presentation. Key features include dynamic chart animations, interactive data exploration through pan and zoom controls, and a customizable legend to clearly indicate each data series. The grouping logic and chart configuration align with the principles detailed in the JavaScript Stacked Column Chart Documentation.

Integration and Best Practices

Integrating SciChart.js with React is efficiently handled by the <SciChartReact/> component, which accepts an initialization function via the initChart prop to render the chart within a React component. This method supports the best practices outlined in React Charts with SciChart.js: Introducing “SciChart React”. In addition, applying interactivity modifiers such as ZoomPanModifier is essential for responsive chart behavior, as documented in the ZoomPanModifier documentation. Furthermore, developers looking to optimize performance can refer to the Performance Tips & Tricks for guidance on efficient rendering and data handling in large-scale React applications.