649 words
3 minutes
Drawing Venn Diagrams with AntV/G2

I was recently assigned to a new project built by another team, and it uses a different charting library than the one we’d settled on. My team had been using echarts, while this project runs on antv/g2. Since I’d never worked with it before, I’m writing this down as I go.

Untitled

At first glance this didn’t look hard — a Venn diagram should do it. So I went straight to the official Venn examples and adapted the config step by step until I got this:

Untitled

Here’s the code:

chart
.path()
.data({
type: 'inline',
value: [
{ sets: ['A'], size: 15, label: 'Purchasers', value: 19992 },
{ sets: ['B'], size: 15, label: 'Total Members', value: 29992 },
{ sets: ['A', 'B'], size: 6, label: 'Member Purchasers', value: 9992 },
],
transform: [
{
type: 'venn',
},
],
})
.encode('d', 'path')
.encode('color', 'key')
.encode('shape', 'hollow')
.label({
position: 'inside',
text: (a) => `${a?.label}\n${formatNumOfOptions(a?.value, { digit: 0 })}`,
style: {
fontSize: 12,
fontWeight: 500,
fill: '#1890FF',
},
})
.style('lineWidth', 1)
.style('lineDash', [7, 4])
.style('fill', (d) => {
if (d.sets.length === 1) {
if (d.sets[0] === 'A') {
return '#3BA0FF';
}
return '#4DCB73';
}
return '#cdecf2';
})
.style('fillOpacity', 0.1)
.legend(false)
.tooltip(false);

Comparing with the mockup, two things are off: the stroke color at the intersection, and the label positions.

The intersection stroke is the easy fix — make each circle keep its own color there via style:

style('stroke', (d) => {
if (d.key === 'A') {
return '#1890FF';
}
if (d.key === 'B') {
return '#4DCB73';
}
return 'transparent';
})

The label positions take a bit more work: you need a custom label renderer. A look through the docs turned up label.render for exactly that.

Untitled

The render function returns custom HTML, and a CSS transform: translate nudges it into place. Here’s the implementation:

render: (_, datum) => {
if (datum.key === 'A') {
return `
<div style="font-weight: 500; color: #1890FF; transform: translate(-80px, -20px)">
<p style="font-size: 12px">${datum.label}</p>
<p style="font-size: 14px">${formatNumOfOptions(datum.value, { digit: 0 })}</p>
</div>
`;
}
if (datum.key === 'B') {
return `
<div style="font-weight: 500; color: #4DCB73; transform: translate(30px, -20px);">
<p style="font-size: 12px">${datum.label}</p>
<p style="font-size: 14px">${formatNumOfOptions(datum.value, { digit: 0 })}</p>
</div>
`;
}
return `
<div style="font-weight: 500; color: #00BBFF; transform: translate(-36px, -20px);">
<p style="font-size: 12px">${datum.label}</p>
<p style="font-size: 14px">${formatNumOfOptions(datum.value, { digit: 0 })}</p>
</div>
`;
}

At this point the labels are offset correctly, but you’ll notice a small blue square sitting at each label’s original anchor point, as shown below.

Untitled

That’s because the label is now styled via CSS — just delete the label.style.fill property and the square disappears. The final code:

const chart = new Chart({
container: 'user-assets',
height: 311,
});
chart
.path()
.data({
type: 'inline',
value: [
{ sets: ['A'], size: 15, label: 'Purchasers', value: 19992 },
{ sets: ['B'], size: 15, label: 'Total Members', value: 29992 },
{ sets: ['A', 'B'], size: 6, label: 'Member Purchasers', value: 9992 },
],
transform: [
{
type: 'venn',
},
],
})
.encode('d', 'path')
.encode('color', 'key')
.encode('shape', 'hollow')
.label({
position: 'inside',
text: (a) => `${a?.label}\n${formatNumOfOptions(a?.value, { digit: 0 })}`,
style: {
fontSize: 12,
},
render: (_, datum) => {
if (datum.key === 'A') {
return `
<div style="font-weight: 500; color: #1890FF; transform: translate(-80px, -20px)">
<p style="font-size: 12px">${datum.label}</p>
<p style="font-size: 14px">${formatNumOfOptions(datum.value, { digit: 0 })}</p>
</div>
`;
}
if (datum.key === 'B') {
return `
<div style="font-weight: 500; color: #4DCB73; transform: translate(30px, -20px);">
<p style="font-size: 12px">${datum.label}</p>
<p style="font-size: 14px">${formatNumOfOptions(datum.value, { digit: 0 })}</p>
</div>
`;
}
return `
<div style="font-weight: 500; color: #00BBFF; transform: translate(-36px, -20px);">
<p style="font-size: 12px">${datum.label}</p>
<p style="font-size: 14px">${formatNumOfOptions(datum.value, { digit: 0 })}</p>
</div>
`;
},
})
.style('lineWidth', 1)
.style('lineDash', [7, 4])
.style('fill', (d) => {
if (d.sets.length === 1) {
if (d.sets[0] === 'A') {
return '#3BA0FF';
}
return '#4DCB73';
}
return '#cdecf2';
})
// Keep the intersection strokes in each circle's own color
.style('stroke', (d) => {
if (d.key === 'A') {
return '#1890FF';
}
if (d.key === 'B') {
return '#4DCB73';
}
return 'transparent';
})
.style('fillOpacity', 0.1)
.legend(false)
.tooltip(false);
chart.render();
Drawing Venn Diagrams with AntV/G2
https://www.mihouo.com/en/posts/front/drawing-venn-diagrams-with-antv-g2/
Author
Jia
Published at
2024-04-11
License
CC BY-NC-SA 4.0