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)

Fullscreen

Edit

 Edit

Docs

drawExample.ts

index.html

vanilla.ts

theme.ts

Copy to clipboard
Minimise
Fullscreen
1import { appTheme } from "../../../theme";
2import {
3    EPieType,
4    ELegendOrientation,
5    ELegendPlacement,
6    GradientParams,
7    Point,
8    PieSegment,
9    SciChartPieSurface,
10} from "scichart";
11
12export const drawExample = async (rootElement: string | HTMLDivElement) => {
13    // Create the pie chart
14    const sciChartPieSurface = await SciChartPieSurface.create(rootElement, {
15        theme: appTheme.SciChartJsTheme,
16        pieType: EPieType.Pie,
17        animate: true,
18        seriesSpacing: 15,
19        showLegend: true,
20        showLegendSeriesMarkers: true,
21        animateLegend: true,
22    });
23    // Optional placement of legend
24    sciChartPieSurface.legend.orientation = ELegendOrientation.Horizontal;
25    sciChartPieSurface.legend.placement = ELegendPlacement.BottomLeft;
26
27    // SciChart.js expects a list of PieSegment, however data is often formatted like this
28    // Dataset = 'percentage market share of phones, 2022'
29    const dataset = [
30        { name: "Apple", percent: 28.41 },
31        { name: "Samsung", percent: 28.21 },
32        { name: "Xiaomi", percent: 12.73 },
33        { name: "Huawei", percent: 5.27 },
34        { name: "Oppo", percent: 5.53 },
35        { name: "Vivo", percent: 4.31 },
36        { name: "Realme", percent: 3.16 },
37        { name: "Motorola", percent: 2.33 },
38        { name: "Unknown", percent: 2.19 },
39        { name: "LG", percent: 0.85 },
40        { name: "OnePlus", percent: 1.11 },
41        { name: "Tecno", percent: 1.09 },
42        { name: "Infinix", percent: 0.96 },
43        { name: "Google", percent: 0.77 },
44        { name: "Nokia", percent: 0.45 },
45    ];
46
47    // Colors are just hex strings, supporting #FFFFFF (RBG) or 8-digit with RGBA or CSS color strings e.g. rgba()
48    const colors = [
49        { color1: appTheme.VividOrange, color2: appTheme.MutedOrange },
50        { color1: appTheme.Indigo, color2: appTheme.VividBlue },
51        { color1: appTheme.MutedSkyBlue, color2: appTheme.MutedTeal },
52        { color1: appTheme.MutedTeal, color2: appTheme.PaleTeal },
53        { color1: appTheme.VividSkyBlue, color2: appTheme.MutedSkyBlue },
54        { color1: appTheme.MutedRed },
55        { color1: appTheme.MutedPink },
56        { color1: appTheme.VividPink },
57        { color1: appTheme.VividPurple },
58        { color1: appTheme.MutedOrange },
59        { color1: appTheme.VividOrange },
60        { color1: appTheme.PaleTeal },
61        { color1: appTheme.PaleBlue },
62        { color1: appTheme.PaleOrange },
63        { color1: appTheme.PalePink },
64    ];
65
66    // Optional Relative radius adjustment per segment
67    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];
68
69    const toPieSegment = (name: string, value: number, radiusAdjustment: number, color1: string, color2: string) => {
70        return new PieSegment({
71            value,
72            text: name,
73            labelStyle: { color: appTheme.ForegroundColor },
74            radiusAdjustment,
75            showLabel: value > 2,
76            colorLinearGradient: new GradientParams(new Point(0, 0), new Point(0, 1), [
77                { color: color1, offset: 0 },
78                { color: color2 || color1 + "77", offset: 1 },
79            ]),
80        });
81    };
82
83    // Transform the data to pie segment and add to scichart
84    const pieSegments = dataset.map((row, index) =>
85        toPieSegment(row.name, row.percent, radiusSize[index], colors[index].color1, colors[index].color2)
86    );
87
88    sciChartPieSurface.pieSegments.add(...pieSegments);
89
90    return { sciChartSurface: sciChartPieSurface };
91};
92

See Also: JavaScript Chart Types (28 Demos)

JavaScript Line Chart | JavaScript Chart Examples | SciChart | SciChart.js Demo

JavaScript Line Chart

Discover how to create a high performance JavaScript Line Chart with SciChart - the leading JavaScript library. Get your free demo now.

JavaScript Spline Line Chart | JavaScript Chart Library | SciChart.js Demo

JavaScript Spline Line Chart

Discover how to create a JavaScript Spline Line Chart with SciChart. Demo includes algorithm for smoother lines. Get your free trial now.

JavaScript Digital Line Chart | JavaScript Charts | View Now | SciChart.js Demo

JavaScript Digital Line Chart

Discover how to create a JavaScript Digital Line Chart with SciChart - your feature-rich JavaScript Chart Library. Get your free demo now.

JavaScript Band Chart | JavaScript Charts | View Examples | SciChart.js Demo

JavaScript Band Chart

Easily create a JavaScript Band Chart or High-Low Fill with SciChart - high performance JavaScript Chart Library. Get your free trial now.

JavaScript Spline Band Chart | JavaScript Charts | SciChart | SciChart.js Demo

JavaScript Spline Band Chart

SciChart's JavaScript Spline Band Chart makes it easy to draw thresholds or fills between two lines on a chart. Get your free demo today.

JavaScript Digital Band Chart | JavaScript Chart Library | SciChart.js Demo

JavaScript Digital Band Chart

Learn how to create a JavaScript Digital Band Chart or High-Low Fill Chart with SciChart's easy-to-follow demos. Get your free trial today.

JavaScript Bubble Chart | Online JavaScript Chart Examples | SciChart.js Demo

JavaScript Bubble Chart

Create a high performance JavaScript Bubble Chart with Sci-Chart. Demo shows how to draw point-markers at X,Y locations. Get your free demo now.

JavaScript Candlestick Chart | Chart Examples | SciChart.js | SciChart.js Demo

JavaScript Candlestick Chart

Discover how to create a JavaScript Candlestick Chart or Stock Chart using SciChart.js. For high Performance JavaScript Charts, get your free demo now.

JavaScript Column Chart | JavaScript Charts | SciChart.js | SciChart.js Demo

JavaScript Column Chart

JavaScript Column Chart demo by SciChart supports gradient fill and paletteproviders for more custom coloring options. Get your free demo now.

JavaScript Population Pyramid | SciChart.js Demo

JavaScript Population Pyramid

Population Pyramid of Europe and Africa

JavaScript Error Bars Chart |  Online Examples | SciChart.js | SciChart.js Demo

JavaScript Error Bars Chart

Create JavaScript Error Bars Chart using high performance SciChart.js. Display uncertainty or statistical confidence of a data-point. Get free demo now.

JavaScript Impulse Chart | JavaScript Charts | View Online | SciChart.js Demo

JavaScript Impulse Chart

Easily create JavaScript Impulse Chart or Stem Chart using SciChart.js - our own high performance JavaScript Chart Library. Get your free trial now.

JavaScript Text Chart | SciChart.js Demo

JavaScript Text Chart

Create JavaScript Text Chart with high performance SciChart.js.

JavaScript Fan Chart | JavaScript Chart Library | View Now | SciChart.js Demo

JavaScript Fan Chart

Discover how to create JavaScript Fan Chart with SciChart. Zoom in to see the detail you can go to using our JavaScript Charts. Get your free demo today.

JavaScript Heatmap Chart | JavaScript Chart Library Examples | SciChart.js Demo

JavaScript Heatmap Chart

Easily create a high performance JavaScript Heatmap Chart with SciChart. Get your free trial of our 5-star rated JavaScript Chart Component today.

JavaScript Non Uniform Heatmap Chart | JavaScript Chart Library Examples | SciChart.js Demo

JavaScript Non Uniform Heatmap Chart

Create JavaScript Non Uniform Chart using high performance SciChart.js. Display Heatmap with variable cell sizes. Get free demo now.

JavaScript Heatmap Chart With Contours Example | SciChart.js | SciChart.js Demo

JavaScript Heatmap Chart With Contours

Design a highly dynamic JavaScript Heatmap Chart With Contours with SciChart's feature-rich JavaScript Chart Library. Get your free demo today.

JavaScript Mountain Chart | View Examples Now | SciChart.js | SciChart.js Demo

JavaScript Mountain Chart

Create JavaScript Mountain Chart with SciChart.js. Zero line can be zero or a specific value. Fill color can be solid or gradient as well. Get a free demo now.

JavaScript Spline Mountain Chart | JavaScript Chart Library | SciChart.js Demo

JavaScript Spline Mountain Chart

JavaScript Spline Mountain Chart design made easy. Use SciChart.js' JavaScript Charts for high performance, feature-rich designs. Get free demo now.

JavaScript Digital Mountain Chart | JavaScript Chart Example | SciChart.js Demo

JavaScript Digital Mountain Chart

Create JavaScript Digital Mountain Chart with a stepped-line visual effect. Get your free trial of SciChart's 5-star rated JavaScript Chart Component now.

JavaScript Realtime Mountain Chart | View Online At SciChart | SciChart.js Demo

JavaScript Realtime Mountain Chart

JavaScript Realtime Mountain Chart made easy. Add animated, real-time updates with SciChart.js - high performance JavaScript Charts. Get free trial now.

JavaScript Scatter Chart | JavaScript Charts | SciChart.js | SciChart.js Demo

JavaScript Scatter Chart

Create JavaScript Scatter Chart with high performance SciChart.js. Easily render pre-defined point types. Supports custom shapes. Get your free trial now.

JavaScript Stacked Column Chart | Online JavaScript Charts | SciChart.js Demo

JavaScript Stacked Column Chart

Discover how to create a JavaScript Stacked Column Chart using our feature-rich JavaScript Chart Library, SciChart.js. Get your free demo today!

JavaScript Stacked Group Column Chart | View Examples Now | SciChart.js Demo

JavaScript Stacked Column Side by Side

Design JavaScript Stacked Group Column Chart side-by-side using our 5-star rated JavaScript Chart Framework, SciChart.js. Get your free demo now.

JavaScript Stacked Mountain Chart | JavaScript Chart Library | SciChart.js Demo

JavaScript Stacked Mountain Chart

Design a high performance JavaScript Stacked Mountain Chart with SciChart.js - your one-stop JavaScript chart library. Get free demo now to get started.

JavaScript Smooth Stacked Mountain Chart | JavaScript Chart Library | SciChart.js Demo

JavaScript Smooth Stacked Mountain Chart

Design a high performance JavaScript Stacked Mountain Chart with SciChart.js - your one-stop JavaScript chart library. Get free demo now to get started.

JavaScript Donut Chart | JavaScript Charts | SciChart.js | SciChart.js Demo

JavaScript Donut Chart

Create JavaScript Donut Chart with 5-star rated SciChart.js chart library. Supports legends, text labels, animated updates and more. Get free trial now.

JavaScript Quadrant Chart using Background Annotations | SciChart.js Demo

JavaScript Quadrant Chart using Background Annotations

Demonstrates how to color areas of the chart surface using background Annotations using SciChart.js Annotations API

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL.