1025 words
5 minutes
Drawing a 'Sunburst' Chart with AntV/G2

Why the quotes around “sunburst” in the title? Because while the target looks like a sunburst chart, the data structure behind it is a long way from what a real sunburst expects.

In the official example, a sunburst’s data structure leaves parent values unset — they’re derived by summing all children. In other words: every parent has one or more children, and those children can never extend beyond their parent’s arc on the inner ring. What the product wanted was different: parent and child are unrelated except that the parent’s value must equal the sum of its children — and here each “parent” actually consists of two parts, so the children inevitably straddle the boundary between the two parent arcs. The prototype looked like this:

Product prototype

Radial Stacked Bar Chart#

So a sunburst wouldn’t fit. I raised this with the PM, who suggested trying a radial stacked bar chart. After some tinkering I got the shape right, but plenty of other things didn’t match the requirements.

Result with the radial stacked chart

The result with the radial stacked chart.

A radial stacked chart usually visualizes one set of categories across several states (periods). Our data, however, is different categories across different states — which caused all sorts of trouble.

The biggest issue was tooltip display.

The inner ring looked fine:

Inner ring tooltip

…but the outer ring’s labels were completely mismatched:

Outer ring tooltip

My guess: the radial stacked chart is designed around inner and outer showing the same categories, while mine were different — and the rendering logic appears to walk through every field in the fields array of chart.data.transform in order.

Which is exactly what breaks the tooltip mapping.

const data = [
{
State: '',
Tmall: 25,
Tmall_purchase: 15,
JD: 20,
JD_purchase: 10,
Douyin: 10,
Douyin_purchase: 5,
WeChat: 10,
WeChat_purchase: 5,
},
{
State: ' ',
Baileys: 5,
Baileys_purchase: 5,
JohnnieWalker: 5,
JohnnieWalker_purchase: 5,
Diageo: 10,
Diageo_purchase: 10,
},
];
// chart.data looks like this
{
value: data,
transform: [
{
type: 'fold',
fields: ['Tmall', 'Tmall_purchase', 'JD', 'JD_purchase', 'Douyin', 'Douyin_purchase', 'WeChat', 'WeChat_purchase', 'Baileys', 'Baileys_purchase', 'JohnnieWalker', 'JohnnieWalker_purchase', 'Diageo', 'Diageo_purchase'],
key: 'key',
value: 'totalMembers',
retains: ['State'],
},
],
}

The Final Solution#

Eventually I remembered drawing nested pies in ECharts before. AntV/G2 (v5) has no nested-pie example, but the docs show that rendering two views requires composition — and since we need stacked views, the right tool is space layer.

Following the official example, the nested pie came together quickly — but now a sizing conflict broke the outer ring’s tooltip. Each ring is actually drawn inside a square region sized by its diameter; with two nested rings, the later ring can sit on top of the earlier one for event handling, killing the first ring’s tooltip. Here’s what that looked like:

Broken outer tooltip

I couldn’t find any documented attribute to change that stacking rule, so I flipped it around: shrink the inner ring’s drawing area until it no longer covers the outer ring, and the outer tooltip works again. Concretely, I used attr to change each ring’s width/height, then coordinate.outerRadius and coordinate.innerRadius to control its visual size. The final code:

const data1 = [
{ item: 'Tmall', bgColor: '#e8b038', count: 20, percent: 0.2 },
{ item: 'Tmall-purchase', bgColor: '#e8b038', type: 'purchase', count: 10, percent: 0.1 },
{ item: 'JD', bgColor: '#528df3', count: 10, percent: 0.1 },
{ item: 'JD-purchase', bgColor: '#528df3', type: 'purchase', count: 10, percent: 0.1 },
{ item: 'Douyin', bgColor: '#ff3d72', count: 20, percent: 0.2 },
{ item: 'Douyin-purchase', bgColor: '#ff3d72', type: 'purchase', count: 10, percent: 0.1 },
{ item: 'WeChat', bgColor: '#00a54e', count: 15, percent: 0.15 },
{ item: 'WeChat-purchase', bgColor: '#00a54e', type: 'purchase', count: 5, percent: 0.05 },
];
const data2 = [
{ item: 'Diageo', bgColor: '#9ecbfb', count: 10, percent: 0.1 },
{ item: 'Diageo-purchase', type: 'purchase', bgColor: '#9ecbfb', count: 5, percent: 0.05 },
{ item: 'JohnnieWalker', bgColor: '#add7fc', count: 7, percent: 0.07 },
{ item: 'JohnnieWalker-purchase', type: 'purchase', bgColor: '#add7fc', count: 3, percent: 0.03 },
{ item: 'Baileys', bgColor: '#bfe2fd', count: 3, percent: 0.03 },
{ item: 'Baileys-purchase', type: 'purchase', bgColor: '#bfe2fd', count: 2, percent: 0.02 },
{ item: 'not-show', bgColor: 'transparent', count: 70, percent: 0.7 },
];
// Create the chart instance
const chart = new Chart({
container: 'channel-members',
height: 311,
});
const layer = chart.spaceLayer();
layer
.interval()
.data(data2)
.coordinate({ type: 'theta', outerRadius: 1.1, innerRadius: 0.82 })
.transform({ type: 'stackY' })
.attr('x', 640 / 2 - 150)
.attr('y', 311 / 2 - 150)
.attr('width', 300)
.attr('height', 300)
.encode('y', 'percent')
.style('fill', (d: any) => {
const result = {
image: lines({
backgroundColor: d.bgColor,
backgroundOpacity: 0.6,
stroke: d.bgColor,
lineWidth: 1,
spacing: 0,
}),
repetition: 'repeat',
transform: 'rotate(30deg)',
}
if (d.type === 'purchase') {
result.image = lines({
backgroundColor: 'transparent',
backgroundOpacity: 0.6,
stroke: d.bgColor,
lineWidth: 1,
spacing: 10,
});
}
return result;
})
.interaction('tooltip', { disableNative: true })
.tooltip((data) => {
if (data.item === '') {
return '';
}
return {
name: data.item,
value: `${data.percent * 100}%`,
}
});
layer
.interval()
.data(data1)
.coordinate({ type: 'theta', outerRadius: 3.2, innerRadius: 1.6 })
.transform({ type: 'stackY' })
.attr('x', 640 / 2 - 50)
.attr('y', 311 / 2 - 50)
.attr('width', 100)
.attr('height', 100)
.encode('y', 'percent')
.style('fill', (d: any) => {
const result = {
image: lines({
backgroundColor: d.bgColor,
backgroundOpacity: 0.6,
stroke: d.bgColor,
lineWidth: 1,
spacing: 0,
}),
repetition: 'repeat',
transform: 'rotate(30deg)',
}
if (d.type === 'purchase') {
result.image = lines({
backgroundColor: 'transparent',
backgroundOpacity: 0.6,
stroke: d.bgColor,
lineWidth: 1,
spacing: 10,
});
}
return result;
})
.tooltip((data) => ({
name: data.item,
value: `${data.percent * 100}%`,
}));
chart.on(`element:${ChartEvent.POINTER_OVER}`, ({ data }) => {
if (data.data.item === 'not-show') {
return;
}
chart.emit('tooltip:show', { data })
});
chart.on(`element:${ChartEvent.POINTER_MOVE}`, ({ data }) => {
if (data.data.item === 'not-show') {
return;
}
chart.emit('tooltip:show', { data })
});
chart.on(`plot:${ChartEvent.POINTER_OVER}`, () => chart.emit('tooltip:hide'));
chart.render();

Since only Tmall has sub-stores, the outer ring is incomplete. I use data.item === 'not-show' to hide the leftover slice, implemented by filling it with a transparent color via style.fill. Without it, the remaining items would stretch across the whole ring, which isn’t what we want. The catch: hovering that blank outer region still fires the tooltip. My fix was a custom interaction that checks whether data.item === 'not-show' is true and only emits the tooltip when it’s not.

The final result:

Final result 1

Final result 2

Final result 3

Drawing a 'Sunburst' Chart with AntV/G2
https://www.mihouo.com/en/posts/front/drawing-sunburst-charts-with-antv-g2/
Author
Jia
Published at
2024-04-15
License
CC BY-NC-SA 4.0