Basic Pie Chart in MUI X Charts (JavaScript)

The same plot in 14 other libraries — JavaScript: Chart.js, D3.js, Apache ECharts, Highcharts; Python: Altair, Bokeh, lets-plot, Matplotlib, Plotly, plotnine, Pygal, Seaborn; R: ggplot2; Julia: Makie.jl. Compare all 15 side by side: Basic Pie Chart in Python, R, Julia and JavaScript.

A pie chart showing proportions of categorical data as slices of a circle. Each slice represents a category's contribution to the whole, with the angle proportional to its value. Essential for visualizing parts-to-whole relationships in a simple, intuitive format.

Basic Pie Chart rendered with MUI X Charts

Renders

JavaScript source (MUI X Charts)

// anyplot.ai
// pie-basic: Basic Pie Chart
// Library: muix 9.16.0 | JavaScript 22.23.3
// Quality: 87/100 | Updated: 2026-10-23
//# anyplot-orientation: square
import { getContrastRatio } from "@mui/material/styles";
import { PieChart, PieArcLabel } from "@mui/x-charts/PieChart";

const t = window.ANYPLOT_TOKENS;

// Constants
const EXPLODE_OFFSET = 30; // px the largest slice is pulled out along its bisector
const TITLE = "pie-basic · javascript · muix · anyplot.ai";
const MARGIN = { top: 100, right: 60, bottom: 40, left: 60 };
const LEGEND_BAND = 60;

// Columns: set these to your data's column names
const SOURCE = "Traffic source"; // role: category
const SHARE = "Share of visits (%)"; // role: value

// Data: replace this block with your own records, one object per row
// Website traffic sources for a mid-size e-commerce site (share of visits, sums to 100)
const data = [
  { [SOURCE]: "Organic Search", [SHARE]: 38 },
  { [SOURCE]: "Direct", [SHARE]: 22 },
  { [SOURCE]: "Social Media", [SHARE]: 16 },
  { [SOURCE]: "Referral", [SHARE]: 12 },
  { [SOURCE]: "Paid Search", [SHARE]: 8 },
  { [SOURCE]: "Email", [SHARE]: 4 },
];

// Plot: reads only data and the constants above
const total = data.reduce((sum, d) => sum + d[SHARE], 0);
// Categorical pool without slot 5, the matte red reserved for bad/loss/error roles
const categorical = t.palette.filter((_, i) => i !== 4);
const slices = data.map((d, i) => ({
  id: String(i),
  label: d[SOURCE],
  value: d[SHARE],
  color: categorical[i % categorical.length],
}));
const explodedIndex = slices.reduce((best, s, i) => (s.value > slices[best].value ? i : best), 0);
const percent = (value) => `${Math.round((value / total) * 100)}%`;

// Arc-label ink: whichever of dark ink / white has the higher WCAG contrast
// against the slice's own fill, so every label stays legible on any Imprint color.
const DARK_INK = "#1A1A17";
const labelInk = (fill) =>
  getContrastRatio(fill, DARK_INK) > getContrastRatio(fill, "#FFFFFF") ? DARK_INK : "#FFFFFF";
const ContrastArcLabel = (props) => <PieArcLabel {...props} style={{ fill: labelInk(props.color) }} />;

export default function Chart() {
  const { width, height } = window.ANYPLOT_SIZE;
  const chartHeight = height - LEGEND_BAND;
  const drawingWidth = width - MARGIN.left - MARGIN.right;
  const drawingHeight = chartHeight - MARGIN.top - MARGIN.bottom;
  const outerRadius = Math.round((Math.min(drawingWidth, drawingHeight) / 2) * 0.86);

  // Cumulative angle bounds (deg) of the exploded slice, mirroring MUI X's pie
  // layout (0 = 12 o'clock, clockwise, data order).
  const before = slices.slice(0, explodedIndex).reduce((sum, s) => sum + s.value, 0);
  const startAngle = (before / total) * 360;
  const endAngle = ((before + slices[explodedIndex].value) / total) * 360;
  const mid = ((startAngle + endAngle) / 2) * (Math.PI / 180);

  // The main series keeps every slice (and drives the legend in data order); its
  // copy of the exploded slice is hidden, and an unlabelled one-item overlay
  // series redraws that slice offset outward along its bisector.
  return (
    <div style={{ position: "relative", width, height }}>
      <div
        style={{
          position: "absolute",
          top: 28,
          left: 0,
          right: 0,
          textAlign: "center",
          fontSize: 26,
          fontWeight: 600,
          color: t.ink,
          fontFamily: "'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif",
        }}
      >
        {TITLE}
      </div>
      <PieChart
        width={width}
        height={chartHeight}
        skipAnimation
        margin={MARGIN}
        slots={{ pieArcLabel: ContrastArcLabel }}
        series={[
          {
            id: "main",
            data: slices,
            cornerRadius: 2,
            outerRadius,
            arcLabel: (item) => (item.id === slices[explodedIndex].id ? "" : percent(item.value)),
            arcLabelMinAngle: 6,
          },
          {
            id: "exploded",
            data: [{ ...slices[explodedIndex], id: "exploded", label: undefined }],
            cornerRadius: 2,
            outerRadius,
            startAngle,
            endAngle,
            cx: drawingWidth / 2 + Math.sin(mid) * EXPLODE_OFFSET,
            cy: drawingHeight / 2 - Math.cos(mid) * EXPLODE_OFFSET,
            arcLabel: (item) => percent(item.value),
          },
        ]}
        slotProps={{
          legend: {
            direction: "horizontal",
            position: { vertical: "bottom", horizontal: "center" },
            sx: { fontSize: 18, color: t.ink, gap: 3 },
          },
        }}
        sx={{
          "& .MuiPieChart-arc": { stroke: t.pageBg, strokeWidth: 3 },
          [`& [data-series="main"] .MuiPieChart-arc[data-index="${explodedIndex}"]`]: {
            opacity: 0,
            pointerEvents: "none",
          },
          "& .MuiPieChart-arcLabel": { fontSize: 22, fontWeight: 700 },
        }}
      />
    </div>
  );
}

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/pie-basic/muix/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.

{
  "spec_id": "pie-basic",
  "language": "javascript",
  "library": "muix",
  "page": "https://anyplot.ai/pie-basic/javascript/muix",
  "hub": "https://anyplot.ai/pie-basic",
  "code_json": "https://api.anyplot.ai/specs/pie-basic/muix/code",
  "spec_json": "https://api.anyplot.ai/specs/pie-basic",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/pie-basic/javascript/muix/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/pie-basic/javascript/muix/plot-dark.png",
  "interactive_light_html": "https://storage.googleapis.com/anyplot-images/plots/pie-basic/javascript/muix/plot-light.html",
  "interactive_dark_html": "https://storage.googleapis.com/anyplot-images/plots/pie-basic/javascript/muix/plot-dark.html",
  "quality_score": 87.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Basic Pie Chart on anyplot.ai.

Other implementations