DataPremium

Animated Donut Chart

A four-slice ring whose boundaries glide on every value change; whatever falls short of 100% stays grey: it shows a whole being filled.

JSCSS@propertyconic-gradient

Updated

This effect is part of Effect.Labs — 846 vanilla effects, some free, some premium. Data has 28 effects, including 1 free. Explore the category →

3 usage examples

Build your own muesli: the grey shows the room left in the jar — Animated Donut Chart example 1

① Build your own muesli: the grey shows the room left in the jar

WhenOn a made-to-order muesli page, four ingredients each have their "−" and "+" buttons. The ring stands for the jar: every addition grows a slice and pushes the grey back; in the centre, the percentage already filled.
WhyA blend being composed is not a split of 100%: until the jar is full there is room left, and the grey says so. You see at once what you put in and what you can still add.
Settings--adn-size:168px, --adn-hole:60%, --adn-speed:.7s, cereal and dried-fruit hues, track #e6dccb. The buttons sit inside each .adn-slice and change its data-value in steps of 5; the ring is wrapped in a block that carries the centre text. In the scene, a click outside the buttons adds 10 to the next ingredient.
This month's data plan, use by use, and what is left — a mobile carrier's app — Animated Donut Chart example 2

② This month's data plan, use by use, and what is left — a mobile carrier's app

WhenThe "20 GB plan" card of a carrier app: video 38%, music 14%, maps 9%, everything else 11%. The grey quarter of the ring is what has not been used yet; in the centre, "5.6 GB left".
WhyThe subscriber wants two answers: where did my gigabytes go, and how many are left. A ring that stops before the full turn gives both in one drawing.
Settings--adn-size:150px, --adn-hole:58%, --adn-speed:1.1s, track #e7ebf7, legend on the right with percentages aligned to the edge. In the scene, slices are at 0 and receive their values on the first pointer move, like a server reply: the ring fills from 0.
Sticker album: what the pack just added to the collection — Animated Donut Chart example 3

③ Sticker album: what the pack just added to the collection

WhenIn a collecting game, the "My collection" panel shows the share of the album already found, by rarity. With each pack opened, the slices grow a little and the grey recedes; the first legendary sticker brings a fourth colour to life.
WhyThe reward reads on the ring, without animating the whole page to say "you made progress". The grey that remains makes you want to open the next pack.
Settings--adn-size:92px, --adn-hole:52%, --adn-speed:.9s, ring above the legend (flex-direction:column on .adn-wrap), track rgba(255,255,255,.12). Values are sticker counts scaled to the 60-sticker album (18 out of 60 = 30). In the scene, a click anywhere opens a pack.

How it works

.adn-ring is a disc painted by a conic-gradient of four colours followed by a grey. The four boundaries between colours are variables, --adn-s1 to --adn-s4, registered with @property as <percentage>: the browser therefore knows how to animate them, and a one-second transition makes them glide instead of jumping. A radial mask pierces the centre (--adn-hole).

Each <li class="adn-slice" data-value="…"> in the legend is one slice, four at most. Values are read as percentages of a whole of 100: 35, 25, 20 and 15 cover 95% of the ring, and the remaining 5% keeps the --adn-track colour. If the total exceeds 100, every slice is scaled down by the same ratio.

On appearance (35% of the ring visible, then 150 ms), the four boundaries start at 0 and the ring fills clockwise. After that, a MutationObserver watches the data-value attributes: changing one makes that slice grow or shrink and pushes the following ones.

The script writes each legend row's value as a percentage, in the page language, with one decimal at most ("11.7%"). That text changes at once; only the ring takes its time.

Accessibility

  • The legend carries the figures: a <ul> named by aria-label, each row read as "Rent 35%". The ring is aria-hidden.
  • The remainder is not stated: the grey part has no row in the legend. If it matters (free room, data left), write it in plain text nearby, such as "5.6 GB left".
  • Reduced motion: no glide; the ring is filled on first display and moves from one state to the next without a transition.
  • Silent changes: no aria-live region. When a button changes the slices, keep focus on it; the list stays within reach to be read again.
  • Controls: the effect provides none. The "+" and "−" buttons in the examples are yours to write: real <button> elements with an accessible name.

Browser compatibility

conic-gradient, @property (for the glide), mask-image and its -webkit- form, aspect-ratio, MutationObserver, IntersectionObserver. In Chromium 149, Firefox 151 and WebKit 26.5: boundaries at 0% off screen, first boundary between 19 and 21% half a second after load, landing on 35, 60, 80 and 95%; a total of 160 scaled down to 100; two rings that do not interfere.

Chrome 88+✓ Full (measured)
Firefox 128+✓ Full (measured); from 89 to 127, slices change without gliding
Safari 16.4+✓ WebKit engine measured, not checked in real Safari
Edge 88+✓ Full (Chromium engine)
Safari iOS 16.4+✓ Not checked on a real device
Chrome Android 88+✓ Full (Chromium engine)

Without @property the ring is correct but switches from one state to the next at once. Without JavaScript it stays entirely grey and the legend has no figures.

The code

HTML structure to paste into your page (the CSS + JS come with your purchase):

index.html — structure
<div class="fx-0242-root">
  <div class="adn-wrap">
    <div class="adn-ring" aria-hidden="true"></div>
    <ul class="adn-legend" aria-label="Monthly budget">
      <li class="adn-slice" data-value="35">Rent</li>
      <li class="adn-slice" data-value="25">Groceries</li>
      <li class="adn-slice" data-value="20">Energy</li>
      <li class="adn-slice" data-value="15">Outings</li>
    </ul>
  </div>
</div>

One-time payment, no subscription — the code is yours forever

Customize

Options passed to the API or data-* attributes:

Option / propertyDefaultEffect
data-value on each .adn-slice (HTML) 35, 25, 20, 15 Slice, as a percentage of the whole. Changing it makes the ring glide to the new state.
Number of .adn-slice (HTML) 4 From one to four. A fifth row would show in the legend with no slice and no figure.
--adn-c1 to --adn-c4 (CSS) #6366f1, #ec4899, #f59e0b, #10b981 Slice and dot colours, in list order.
--adn-track (CSS) white at 8% Colour of the remainder, and of the ring before it fills.
--adn-size (CSS) 140px Diameter; never exceeds the container width.
--adn-hole (CSS) 40% Hole radius, as a share of the ring radius: 60% gives a thin ring, 20% an almost full disc.
--adn-speed (CSS) 1s Glide duration, on appearance and on every change.
--adn-text (CSS) white at 70% Legend text colour.
--adn-bg, --adn-min-height (CSS) #0a0a0f, 300px Block background and minimum height.

FAQ

Both glide from one set of values to the next with the same technique. Morphing Pie Chart is a full disc of up to six slices that always scales your values to 100%: it shows a split. Animated Donut Chart is a four-slice ring that takes your values as they are: if they do not add up to 100, the rest stays grey. It shows what is taken and what is left.
Convert them before writing them: value / total * 100. Otherwise 3, 5 and 2 are read as 3%, 5% and 2%, and the ring stays almost empty. The total to use is that of the whole (the album's 60 stickers), not the sum of the slices, or the ring would always be complete.
Not without rewriting the code: the gradient, the @property variables and the script are built for four boundaries. Group the smallest into an "Other" slice, or pick Morphing Pie Chart, which takes six.