JavaScript Polar Column Category Chart

Creates a JavaScript Polar Column Category Chart using SciChart.js, with a custom positive/negative threshold fill & stroke for each column.

src/drawExample.ts
import {
    PolarColumnRenderableSeries,
    PolarMouseWheelZoomModifier,
    PolarZoomExtentsModifier,
    PolarPanModifier,
    XyDataSeries,
    PolarNumericAxis,
    SciChartPolarSurface,
    EPolarAxisMode,
    NumberRange,
    EAxisAlignment,
    EPolarLabelMode,
    PolarCategoryAxis,
    DefaultPaletteProvider,
    parseColorToUIntArgb,
    EStrokePaletteMode,
    WaveAnimation,
    Thickness,
} from "scichart";
import { appTheme } from "./appTheme";

// Custom PaletteProvider for column series which colours datapoints above a threshold
class ColumnPaletteProvider extends DefaultPaletteProvider {
    private threshold: number;
    private positiveFillColor: number;
    private positiveStroke: number;

    private negativeFillColor: number;
    private negativeStroke: number;

    constructor(threshold: number) {
        super();
        this.strokePaletteMode = EStrokePaletteMode.SOLID;
        this.threshold = threshold;
        this.positiveStroke = parseColorToUIntArgb(appTheme.VividRed);
        this.positiveFillColor = parseColorToUIntArgb(appTheme.VividRed, 127);
        this.negativeStroke = parseColorToUIntArgb(appTheme.VividBlue);
        this.negativeFillColor = parseColorToUIntArgb(appTheme.VividBlue, 127); // 127/255 opacity
    }

    overrideStrokeArgb(xValue: number, yValue: number, index: number, opacity: number, metadata: any) {
        return yValue < this.threshold ? this.positiveStroke : this.negativeStroke;
    }

    overrideFillArgb(xValue: number, yValue: number, index: number, opacity: number, metadata: any) {
        return yValue < this.threshold ? this.positiveFillColor : this.negativeFillColor;
    }
}

const DATA_UK = {
    labels: [
        "Poultry",
        "Fruit",
        "Milk",
        "Cheese",
        "Pizza",
        "Meat",
        "Cereals",
        "Eggs",
        "Oats",
        "Lamb",
        "Butter",
        "Chocolate",
        "Sheep",
        "OliveOil",
    ],
    data: [-18.5, -12.5, -11.7, -9.2, -7.2, -6.8, -5.9, 7.8, 9.1, 10.2, 10.2, 11.7, 17.6, 22.1],
};

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    const { sciChartSurface, wasmContext } = await SciChartPolarSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
        title: "Cunsumer prices relative to past year in UK, 2024",
        titleStyle: {
            fontSize: 24,
        },
    });

    const radialYAxis = new PolarNumericAxis(wasmContext, {
        polarAxisMode: EPolarAxisMode.Radial,
        axisAlignment: EAxisAlignment.Right,
        visibleRange: new NumberRange(
            Math.min(...DATA_UK.data),
            Math.max(...DATA_UK.data) + 4 // Add some padding to fit data-label for topmost column
        ),
        drawMinorTickLines: false,
        drawMajorTickLines: false,
        useNativeText: true,
        drawMinorGridLines: false,
        zoomExtentsToInitialRange: true,
        labelPostfix: "%",
        labelPrecision: 0,
        // labelStyle: {
        //     color: "white",
        // },
        innerRadius: 0.15,
        startAngle: Math.PI / 2,
    });
    sciChartSurface.yAxes.add(radialYAxis);

    const polarXAxis = new PolarCategoryAxis(wasmContext, {
        polarAxisMode: EPolarAxisMode.Angular,
        axisAlignment: EAxisAlignment.Top,
        polarLabelMode: EPolarLabelMode.Parallel,
        visibleRange: new NumberRange(-1, DATA_UK.data.length),
        drawMajorGridLines: false,
        drawMinorGridLines: false,
        drawMinorTickLines: false, // ticks don't make sense on category axes
        useNativeText: true,
        zoomExtentsToInitialRange: true,
        flippedCoordinates: true,
        labelPrecision: 0,
        // labelStyle: {
        //     color: "white",
        // },
        totalAngle: Math.PI * 2,
        startAngle: Math.PI / 2,
        autoTicks: false,
        majorDelta: 1,
        labels: DATA_UK.labels,
    });
    sciChartSurface.xAxes.add(polarXAxis);

    const polarColumn = new PolarColumnRenderableSeries(wasmContext, {
        dataSeries: new XyDataSeries(wasmContext, {
            xValues: Array.from({ length: DATA_UK.data.length }, (_, i) => i),
            yValues: DATA_UK.data,
        }),
        dataLabels: {
            style: {
                fontSize: 14,
                padding: Thickness.fromNumber(0),
            },
            polarLabelMode: EPolarLabelMode.Parallel,
            // color: "white",
            precision: 0,
        },
        dataPointWidth: 0.6,
        strokeThickness: 2,
        paletteProvider: new ColumnPaletteProvider(0),
        animation: new WaveAnimation({ duration: 800, zeroLine: 0, fadeEffect: true }),
    });
    sciChartSurface.renderableSeries.add(polarColumn);

    sciChartSurface.chartModifiers.add(
        new PolarPanModifier(),
        new PolarZoomExtentsModifier(),
        new PolarMouseWheelZoomModifier()
    );

    return { sciChartSurface, wasmContext };
};

Polar Column Category Chart - JavaScript

Overview

This example demonstrates how to create a Polar Column Chart with category axis labels in JavaScript using SciChart.js. The visualization displays UK consumer price changes as radial columns with color-coded positive/negative values.

Technical Implementation

The chart uses a PolarCategoryAxis for angular positioning of food categories and a PolarNumericAxis for radial value display. Data is rendered via PolarColumnRenderableSeries with a custom ColumnPaletteProvider that colors columns differently above/below a threshold.

Features and Capabilities

Key features include:

  • Radial data labels with percentage formatting
  • Custom color thresholds using palette providers
  • Interactive modifiers like PolarZoomExtentsModifier and PolarPanModifier
  • Wave animation effects on initial render

Integration and Best Practices

The implementation follows SciChart's async initialization pattern for optimal performance. Proper cleanup is handled via surface disposal. For production use, consider data streaming techniques for dynamic updates.