Skip to content

Commit f7665d8

Browse files
committed
feat: pie chat to display data with percent value
1 parent 6b99a02 commit f7665d8

File tree

1 file changed

+23
-2
lines changed
  • frontend/src/views/chat/component/charts

1 file changed

+23
-2
lines changed

frontend/src/views/chat/component/charts/Pie.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BaseG2Chart } from '@/views/chat/component/BaseG2Chart.ts'
22
import type { ChartAxis, ChartData } from '@/views/chat/component/BaseChart.ts'
33
import type { G2Spec } from '@antv/g2'
4+
import { endsWith, replace } from 'lodash-es'
45

56
export class Pie extends BaseG2Chart {
67
constructor(id: string) {
@@ -16,12 +17,31 @@ export class Pie extends BaseG2Chart {
1617
return
1718
}
1819

20+
// 特殊处理 %
21+
const _data = []
22+
let isPercent = false
23+
if (data.length > 0) {
24+
const v = data[0][y[0].value] + ''
25+
if (endsWith(v, '%')) {
26+
isPercent = true
27+
}
28+
}
29+
for (let i = 0; i < data.length; i++) {
30+
const v = data[i]
31+
const _v = { ...v }
32+
if (isPercent) {
33+
const formatValue = replace(v[y[0].value], '%', '')
34+
_v[y[0].value] = Number(formatValue)
35+
}
36+
_data.push(_v)
37+
}
38+
1939
const options: G2Spec = {
2040
...this.chart.options(),
2141
type: 'interval',
2242
coordinate: { type: 'theta', outerRadius: 0.8 },
2343
transform: [{ type: 'stackY' }],
24-
data: data,
44+
data: _data,
2545
encode: {
2646
y: y[0].value,
2747
color: series[0].value,
@@ -32,7 +52,8 @@ export class Pie extends BaseG2Chart {
3252
labels: [
3353
{
3454
position: 'outside',
35-
text: (data: any) => `${data[series[0].value]}: ${data[y[0].value]}`,
55+
text: (data: any) =>
56+
`${data[series[0].value]}: ${data[y[0].value]}${isPercent ? '%' : ''}`,
3657
},
3758
],
3859
tooltip: (data) => {

0 commit comments

Comments
 (0)