JavaScript Donut Chart

This demo demonstrates how create a JavaScript Donut Chart with our powerful JavaScript library, SciChart.js.

Market share of Internet Browsers (2022)

src/drawExample.ts
import { appTheme } from "./appTheme";
import {
    EPieType,
    ESizingMode,
    SciChartPieSurface,
    PieSegment,
    GradientParams,
    Point,
    ELegendOrientation,
    ELegendPlacement,
} from "scichart";

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    // Create the pie chart
    const sciChartPieSurface = await SciChartPieSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
        pieType: EPieType.Donut,
        holeRadius: 0.6,
        holeRadiusSizingMode: ESizingMode.Relative,
        animate: true,
        seriesSpacing: 10,
        showLegend: true,
        showLegendSeriesMarkers: true,
        animateLegend: true,
        cornerRadius: 6
    });
    // Optional placement of legend
    sciChartPieSurface.legend.orientation = ELegendOrientation.Vertical;
    sciChartPieSurface.legend.placement = ELegendPlacement.TopLeft;

    // SciChart.js expects a list of PieSegment, however data is often formatted like this
    // Dataset = 'percentage market share of phones, 2022'
    const dataset = [
        { name: "Chrome", percent: 65.27 },
        { name: "Safari", percent: 19.03 },
        { name: "Edge", percent: 4.4 },
        { name: "Firefox", percent: 3.07 },
        { name: "Samsung", percent: 2.7 },
        { name: "Opera", percent: 2.49 },
        { name: "Android", percent: 0.78 },
        { name: "IE", percent: 0.32 },
    ];

    // Colors are just hex strings, supporting #FFFFFF (RBG) or 8-digit with RGBA or CSS color strings e.g. rgba()
    const colors = [
        { color1: appTheme.VividOrange, color2: appTheme.MutedOrange },
        { color1: appTheme.Indigo, color2: appTheme.VividBlue },
        { color1: appTheme.MutedSkyBlue, color2: appTheme.MutedTeal },
        { color1: appTheme.MutedTeal, color2: appTheme.PaleTeal },
        { color1: appTheme.VividSkyBlue, color2: appTheme.MutedSkyBlue },
        { color1: appTheme.MutedRed },
        { color1: appTheme.MutedPink },
        { color1: appTheme.VividPink },
    ];

    // Optional Relative radius adjustment per segment
    const radiusSize = [0.8, 0.85, 0.9, 0.9, 0.9, 0.95, 0.95, 0.95];

    const toPieSegment = (name: string, value: number, radiusAdjustment: number, color1: string, color2: string) => {
        return new PieSegment({
            value,
            text: name,
            labelStyle: { color: appTheme.ForegroundColor },
            radiusAdjustment,
            showLabel: value > 2,
            colorLinearGradient: new GradientParams(new Point(0, 0), new Point(0, 1), [
                { color: color1, offset: 0 },
                { color: color2 || color1 + "77", offset: 1 },
            ]),
        });
    };

    // Transform the data to pie segment and add to scichart
    const pieSegments = dataset.map((row, index) =>
        toPieSegment(row.name, row.percent, radiusSize[index], colors[index].color1, colors[index].color2)
    );

    sciChartPieSurface.pieSegments.add(...pieSegments);

    return { sciChartSurface: sciChartPieSurface };
};

Donut Chart - JavaScript

Overview

This example illustrates the creation of an animated donut chart using SciChart.js with JavaScript. It demonstrates how to visualize market share data using dynamic, gradient-filled segments and configurable legends. The chart is constructed asynchronously to ensure smooth rendering and maximal performance in web applications.

Technical Implementation

The chart is built by creating a new instance of SciChartPieSurface configured as with pieType = EPieType.Donut, setting holeRadius and holeRadiusSizingMode and animated transitions. Each segment is generated from a dataset by transforming data objects into PieSegment instances with custom linear gradients. The asynchronous initialization in the function drawExample ensures the chart is set up efficiently. For detailed guidance on initializing SciChart.js charts asynchronously, please refer to the Getting Started with SciChart JS documentation.

Features and Capabilities

Key features of this implementation include animated updates, advanced gradient color configuration for each pie segment, dynamic legend orientation and placement, and relative radius adjustments for enhanced visual differentiation of data segments. These capabilities are essential for building interactive visualizations, as detailed in the JavaScript Donut Chart Documentation.

Integration and Best Practices

This example follows best practices by using efficient asynchronous initialization and properly managing resources through a returned destructor function, enabling safe cleanup of the chart instance when no longer needed. Such optimizations are in line with recommendations from the Memory Best Practices guide. Additionally, the use of gradient configurations and dynamic legends simplifies further customization and integration of SciChart.js into standard JavaScript projects. For more technical insights on animated configurations and gradient customization, developers can also explore practical examples in the JavaScript Donut Chart Documentation.