JavaScript Pie Chart

For an example that demonstrates how create a JavaScript Pie Chart, our demo code teaches you how to do this with SciChart's JavaScript Charting Library.

Market share of Mobile Phone Manufacturers (2022)

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

export const drawExample = async (rootElement: string | HTMLDivElement) => {
    // Create the pie chart
    const sciChartPieSurface = await SciChartPieSurface.create(rootElement, {
        theme: appTheme.SciChartJsTheme,
        pieType: EPieType.Pie,
        animate: true,
        seriesSpacing: 15,
        showLegend: true,
        showLegendSeriesMarkers: true,
        animateLegend: true,
        cornerRadius: 3
    });
    // Optional placement of legend
    sciChartPieSurface.legend.orientation = ELegendOrientation.Horizontal;
    sciChartPieSurface.legend.placement = ELegendPlacement.BottomLeft;

    // SciChart.js expects a list of PieSegment, however data is often formatted like this
    // Dataset = 'percentage market share of phones, 2022'
    const dataset = [
        { name: "Apple", percent: 28.41 },
        { name: "Samsung", percent: 28.21 },
        { name: "Xiaomi", percent: 12.73 },
        { name: "Huawei", percent: 5.27 },
        { name: "Oppo", percent: 5.53 },
        { name: "Vivo", percent: 4.31 },
        { name: "Realme", percent: 3.16 },
        { name: "Motorola", percent: 2.33 },
        { name: "Unknown", percent: 2.19 },
        { name: "LG", percent: 0.85 },
        { name: "OnePlus", percent: 1.11 },
        { name: "Tecno", percent: 1.09 },
        { name: "Infinix", percent: 0.96 },
        { name: "Google", percent: 0.77 },
        { name: "Nokia", percent: 0.45 },
    ];

    // 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 },
        { color1: appTheme.VividPurple },
        { color1: appTheme.MutedOrange },
        { color1: appTheme.VividOrange },
        { color1: appTheme.PaleTeal },
        { color1: appTheme.PaleBlue },
        { color1: appTheme.PaleOrange },
        { color1: appTheme.PalePink },
    ];

    // Optional Relative radius adjustment per segment
    const radiusSize = [0.8, 0.8, 0.8, 0.8, 0.85, 0.85, 0.85, 0.9, 0.9, 0.9, 0.95, 0.95, 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 };
};

JavaScript Pie Chart Example (JavaScript)

Overview

This example demonstrates how to create a high performance, animated pie chart using SciChart.js with JavaScript. The implementation transforms a raw dataset into individual pie segments with dynamic gradient fills and adjustable segment radii, making it ideal for visualizing data such as market share statistics.

Technical Implementation

The core of the example is the asynchronous initialization of the chart using the SciChartPieSurface, which is created by calling an async function that returns a promise. This pattern, detailed in tutorials like Including SciChart.js in an HTML Page using CDN, ensures that the WebGL context is fully ready before the chart is rendered. Additionally, the example leverages the GradientParams class to apply smooth gradient fills to each pie segment as shown in The Pie Chart Type Documentation. Data transformation is handled by mapping raw dataset values into PieSegment instances (see PieSegment API Documentation) with conditional label rendering based on segment values.

Features and Capabilities

This pie chart supports animated transitions for both the chart segments and the legend. The legend is configured for horizontal orientation and placed at the bottom left to optimize the display area. The implementation also demonstrates advanced visual customization through dynamic gradient fills and relative radius adjustments for each segment, providing an engaging and informative user experience. Furthermore, the chart is optimized for performance by leveraging WebGL rendering for smooth animations and efficient resource usage.

Integration and Best Practices

Designed for a JavaScript environment, the example follows modern asynchronous patterns and includes a cleanup mechanism that calls the delete() method on the SciChartPieSurface, ensuring proper disposal of WebGL resources. This approach aligns with recommended best practices for resource management as outlined in Deleting DataSeries Memory. Developers can further refine their implementations by studying these techniques and integrating similar patterns in their projects, ensuring robust and maintainable charting applications.