webdevpuneet.com

Sunburst Chart — Hierarchical Ring Chart in SVG

Sunburst Chart · Charts · Plain HTML, CSS & JS · Live preview

CategoryCharts

Copy & paste

Source Code

<div class="sb-card">
  <h3>Storage by category</h3>
  <div class="sb-wrap">
    <svg class="sb-svg" id="sbSvg" viewBox="0 0 220 220" aria-label="Sunburst chart"></svg>
    <div class="sb-center" id="sbCenter"><strong id="sbVal">100%</strong><span id="sbCap">Total</span></div>
  </div>
  <div class="sb-legend" id="sbLegend"></div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Annular sector paths
Each ring slice is one SVG path with correct arc flags.
Two-level hierarchy
Children nest within their parent's angular span.
Proportional angles
Every node's span reflects its share of the total.
Parent-tinted children
Outer colors derive from the parent for visual grouping.
Hover focus
Center shows the label, path, and percentage.
Center readout
Crisp overlaid HTML, resets to total on leave.
Data-driven legend
Legend and layout come from the data array.
No library
Pure HTML/CSS/JS/SVG — no D3 or chart dependency.

About this UI Snippet

Sunburst Chart — Nested Ring Hierarchy with Hover Focus

Screenshot of the Sunburst Chart snippet rendered live

A sunburst chart shows a hierarchy as concentric rings — the inner ring is the top-level categories and each outer arc is a child nested within its parent's angular slice. It's the compact way to show part-of-whole relationships at two levels, like storage by category and file type. This snippet renders one in pure SVG with hover focus and a centre readout, in plain HTML, CSS, and vanilla JavaScript with no charting library.

Annular sector path math

Each segment is an annular sector (a slice of a ring) drawn with one SVG path: two arcs (outer and inner radius) joined by straight edges. A polar(r, a) helper converts a radius and angle to x/y around the centre (offset by −90° so angles start at the top), and a sector() function assembles the path with the correct large-arc-flag for slices over 180°. This is the same arc-drawing technique behind donut and pie charts, extended to nested rings.

Hierarchy and proportional angles

Data is a list of parents, each with children. The script sums each parent's children to get its value and the grand total, then assigns every node an angular span proportional to its value out of the total. Critically, children are laid out within their parent's span, so the outer ring nests correctly — a child's arc sits directly outside the parent slice it belongs to. Child colours are tints of the parent colour, so the family relationship reads visually.

Hover focus and the centre readout

Hovering any segment shows its label and its percentage of the total in the centre — for an outer arc it shows the full path ("Media › Photos"), so you always know where you are in the hierarchy. Leaving the chart resets the centre to the total. The centre is an overlaid HTML element, so the text is crisp and easy to style independently of the SVG.

Lightweight and legible

Thin strokes separate the segments, hover dims slightly for feedback, and a legend ties the inner-ring colours to their categories. Everything derives from the data array, so changing the hierarchy or values needs no code edits.

Dependency-free reference

The whole chart is the sector math plus a layout loop — no D3 or chart library — making it a clean, portable reference for the sunburst pattern when you need hierarchical proportions without the weight of a full visualization toolkit.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA two-level sunburst renders with a legend and center total.
  2. 2
    Use your dataEdit DATA — parents with children and values; parents auto-sum.
  3. 3
    Hover a segmentThe center shows its label and percentage of the total.
  4. 4
    Read the ringsInner ring is categories; outer arcs are nested children.
  5. 5
    RestyleChange colors, radii, or stroke; child tints derive from parents.
  6. 6
    ResizeAdjust the viewBox and container to scale the chart.

Real-world uses

Common Use Cases

Storage and disk usage
Break space down by top-level category and sub-folder in nested rings, with a centre readout showing the value of whichever segment is currently hovered.
Budget composition
Show spend by category and subcategory, with each child coloured as a tint of its parent so related groups stay visually together on the ring.
Traffic and source breakdowns
Nest channels under groups such as Paid, Organic and Social, and compare each channel's share of its parent next to a donut chart.
Portfolio allocation
Visualise holdings by sector and asset, where every node's angle reflects its exact share of the total and children sit inside their parent's slice.
Hierarchy and arc maths
Study how each annular sector is a single SVG path with correct arc flags, and how children are fitted inside their parent's angular span.

Got questions?

Frequently Asked Questions

Every segment is an annular sector — a slice of a ring — built as one SVG path: an outer arc, a line in to the inner radius, an inner arc back, and a closing line. A polar helper converts a radius and angle to coordinates around the centre (offset so angles begin at the top), and the path uses the large-arc-flag to handle slices over 180 degrees. It is the donut-arc technique applied to two radii.

Each parent gets an angular span proportional to its total. Children are then laid out sequentially within that exact span, each taking an angle proportional to its own value out of the grand total. Because the child arcs start where the parent slice starts and fill its span, the outer ring lines up directly outside the matching inner slice, which is what makes it read as a hierarchy.

Coloring children as lighter shades of their parent's color makes the family relationship obvious without a busy legend — you can see at a glance which outer arcs belong to which inner category. The snippet derives child shades by lightening the parent color by an increasing amount per child, so siblings are distinguishable but clearly related.

Yes. The sector function works for any inner/outer radius pair, so you can add a third ring by extending the layout loop to recurse into grandchildren within each child's span. For drill-down, animate radii and re-layout on click to zoom into a branch. The math stays the same; you are just adding rings and transitions.

Compute the segment list (label, path, color, value) from your data in a memo/computed and render them as SVG paths. Track the hovered node in state to drive the center readout. D3's partition/arc or a chart library can replace the math for complex cases, but the data shape and rendering approach are the same. Tailwind users swap the classes for utilities.