JavaScript Stacked Column Side by Side

We have created a simple example that shows you how to create a JavaScript 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 };
};

Stacked Column Side By Side on JavaScript for SciChart.js Example

Overview

This example demonstrates how to create a sophisticated stacked and grouped column chart using JavaScript with SciChart.js. In this example, multiple data series (for example, representing tomato, pepper and cucumber data) are rendered side by side by assigning different stackedGroupId values. The chart is initialized asynchronously using WebAssembly-powered performance via the SciChartSurface.

Technical Implementation

The chart creation begins with an asynchronous initialization using async/await, as shown in the implementation which mirrors the techniques described in the asynchronous initialization documentation. The NumericAxis for both the X and Y dimensions is configured with custom label precision and formatting settings; developers can refer to the Numeric Axis documentation for more details on this. Data series are created with the StackedColumnRenderableSeries and then assembled into a StackedColumnCollection to achieve the grouped (side-by-side) stacked effect. More information on these series types is available in the Stacked Column Series Type documentation. In addition, data labels are styled with custom fonts, padding, and precision settings following best practices outlined in the Data Label Positioning documentation.

Features and Capabilities

This implementation includes several advanced features:

  • Animated Series Transitions: A WaveAnimation is applied to the series to create smooth startup animations. This enhances the visual appeal and is discussed in the Series Style Animations documentation.

  • Interactive Chart Modifiers: The chart supports interactive functionalities such as zooming and panning through the integration of ZoomExtentsModifier, ZoomPanModifier, and MouseWheelZoomModifier. Developers can learn more about these interactive modifiers in the ZoomExtentsModifier documentation. Additionally, the inclusion of a LegendModifier enhances the user experience by providing interactive legends, as explained in the Legend Modifier documentation.

Integration and Best Practices

The example is implemented using JavaScript without reliance on frameworks or custom hooks, ensuring a lightweight integration process. Resource cleanup is handled through a destructor pattern that deletes the SciChartSurface when it is no longer needed, aligning with the guidelines provided in the Memory Best Practices documentation. Furthermore, performance optimization is achieved by leveraging the wasmContext for efficient WebAssembly-powered computations, ensuring the chart remains responsive even with complex data. Custom theming is also supported via the appTheme, and developers interested in visual customization can refer to the Chart Styling - Creating a Custom Theme documentation.

This example represents an excellent demonstration of building highly interactive and customizable charts with SciChart.js in a JavaScript environment, adhering to best practices in asynchronous programming, data visualization configuration, and resource management.