效果图如下:
代码:
import { Chart } from '@antv/g2'
js代码
const primaryChartData1 = [
{
name: '未答人数',
count: 2,
},
{
name: '已答人数',
count: 1,
},
]
const randerChart1 = () => {
const tempNode = document.getElementById('chartData1')
const child = tempNode?.lastElementChild
if (child) {
tempNode.removeChild(child)
}
const data = primaryChartData1
const chart = new Chart({
container: 'chartData1',
width: 450,
height: 300
})
chart.data(data)
chart.coordinate('theta', {
radius: 0.7
})
chart.scale('count', {
formatter: (val) => {
val = val * 100 + '%'
return val
},
})
chart.tooltip({
showTitle: false,
showMarkers: false,
})
chart.axis(false)
chart.legend({
position: 'right',
offsetX: -10
})
const interval = chart
.interval()
.adjust('stack')
.position('count')
.color('name', ['#1890FF', '#FAB242'])
.label('name', (val) => {
return {
content: (obj) => {
return obj.name + ':' + obj.count
},
offset: 20,
}
})
.tooltip('name*count', (name, count) => {
return {
name: name,
value: count,
}
})
.style({
lineWidth: 1,
stroke: '#fff',
})
chart.render()
}
html代码
<div id="chartData1"></div>
|