How to make an HTML pie chart

Three ways to draw a pie chart in HTML: a CSS conic gradient, an SVG with stroke offsets, or a chart library. The gradient is enough for most shared pages, and a link keeps the numbers current.

An HTML pie chart is most easily drawn with a CSS conic-gradient on a round element, which needs no JavaScript, no SVG and no library.

The trick is that the gradient stops are cumulative. A 30 percent slice followed by a 20 percent slice ends at 50 percent, not 20.

A finished pie chart page. Circle on the left, colour swatch legend on the right.
A finished pie chart page. Circle on the left, colour swatch legend on the right.

The CSS gradient version

Four slices, written as running totals.

.pie {
  width: 240px; aspect-ratio: 1; border-radius: 50%;
  background: conic-gradient(
    #4f7cff 0 42%,
    #34b27b 42% 68%,
    #e0a33e 68% 88%,
    #8a8f98 88% 100%
  );
}

That is the entire chart. It scales, it prints, it works offline, and there is nothing to load.

For a donut, lay a circle of the page background colour over the middle with a pseudo-element, or use a radial mask. Keep the hole under about 60 percent of the diameter or the slices get hard to compare.

Three approaches compared

CSS conic gradient Inline SVG Chart library
Extra files to load None None The library
Hover tooltips No With script Built in
Animated entry Limited Yes Built in
Labels placed on slices No Yes, with maths Built in
Works with scripts blocked Yes Yes No
Size on the page A few lines Moderate Large

For a chart inside a report that people read once, the gradient wins on every practical axis. For a dashboard where readers hover to inspect values, a library earns its weight.

Inline SVG sits in the middle. You control the geometry exactly, and the markup stays part of the page rather than being drawn after load.

The SVG version, briefly

One circle, one stroke per slice, offset around the circumference.

<svg viewBox="0 0 42 42">
  <circle cx="21" cy="21" r="15.9" fill="none"
    stroke="#4f7cff" stroke-width="10"
    stroke-dasharray="42 58" stroke-dashoffset="25" />
</svg>

The radius 15.9 makes the circumference almost exactly 100, so dash values read as percentages directly. Each further slice repeats the circle with its own dash array and a dash offset equal to the cumulative total before it.

Label outside the circle

Labels inside slices look tidy in a mockup and collide in real data. A five percent slice has nowhere to put a word.

Put the legend next to the chart as a list.

  • A square or circle swatch filled with the slice colour.
  • The category name in plain text.
  • The percentage, and the raw count in brackets if the reader will want it.

Order the legend the same way the slices run, clockwise from twelve. A legend in a different order than the chart is worse than no legend.

Make the values readable without the picture

A pie chart carries its data in angles, which is nothing to a screen reader and nothing in a text-only email client.

Give the chart container role="img" and an aria-label that states the headline split. Then include the same numbers as a small table beneath, or visually hidden if the design does not want it visible.

This is the same reason alt text matters for images. The picture is one representation of the data, not the data.

The legend list beside the chart, each row showing a swatch, a label and a percentage.
The legend list beside the chart, each row showing a swatch, a label and a percentage.

When a pie chart is the wrong chart

Pie charts answer one question: what share of the whole is each part. They are poor at almost everything else.

Switch to a bar chart when there are more than about six categories, when the categories do not sum to a meaningful whole, or when the reader needs to compare two similar values precisely.

Two pie charts side by side to show change over time is the worst common case. A pair of bars, or a single bar split into segments, reads immediately.

A chart in a report gets revised. That makes it a page, not an image.

  1. Open the file in the HTML file opener to confirm the gradient and fonts survive outside your folder.
  2. Paste the HTML into a NOS document. It renders as a page of its own.
  3. Share, then Share link, then Create link. Leave it unlisted unless the chart is meant to be found in search.
  4. Send the link. Revise the page rather than sending a second version.
The share dialog with the link created and Public on the web unticked.
The share dialog with the link created and Public on the web unticked.

When the quarter closes, click the numbers in the document and update the gradient stops. The address holds, so the link in last month's update is still the current chart.

Turning a chart into a link covers the same route for charts that came out of a spreadsheet or a chat.

If the chart came from an AI chat

Assistants write conic gradient and SVG pie charts readily, and the output is usually close to correct.

Two things to check before you share it. Do the slice percentages sum to 100, and are the colours distinguishable when printed in grey.

If the chart looks right but the page around it does not, missing styles in AI HTML is the usual cause. Prompting for an HTML chart covers how to ask for one that already has the legend and the accessible table.

A percentage being corrected by clicking the text in the document, with the chart above it.
A percentage being corrected by clicking the text in the document, with the chart above it.

Checklist before sending

  • Do the cumulative stops end at exactly 100 percent?
  • Is the legend in the same order as the slices?
  • Are the numbers available as text, not only as angles?
  • Does it still read in greyscale, for whoever prints it?
  • Is there a <title> so the tab and preview card are not blank?

Questions people ask

Can you make a pie chart with CSS alone?

Yes. A conic-gradient background on a round element produces a pie chart with no JavaScript and no SVG. You give each slice a colour and a cumulative percentage stop. It renders in every current browser and prints correctly.

Should I use a pie chart or a bar chart?

Use a pie chart for parts of one whole, with roughly six slices at most. Use a bar chart for comparing values against each other, or for anything with more categories. People compare bar lengths far more accurately than they compare slice angles.

How do I label pie slices without JavaScript?

Put the legend outside the circle as a list, with a coloured swatch next to each label and the percentage in the text. Labels inside slices need measuring and collide on small slices. An external legend also reads correctly on a phone.

How do I share a pie chart so the numbers stay current?

Paste the HTML into a NOS document and send the share link. When the figures change you edit the page and the same address shows the new chart. A screenshot or an exported image freezes the numbers on the day you made it.

Keep reading