diff --git a/frontend/src/views/chat/component/charts/Bar.ts b/frontend/src/views/chat/component/charts/Bar.ts index eb6f6f7b..8cb577e5 100644 --- a/frontend/src/views/chat/component/charts/Bar.ts +++ b/frontend/src/views/chat/component/charts/Bar.ts @@ -1,6 +1,7 @@ import { BaseG2Chart } from '@/views/chat/component/BaseG2Chart.ts' import type { ChartAxis, ChartData } from '@/views/chat/component/BaseChart.ts' import type { G2Spec } from '@antv/g2' +import { checkIsPercent } from '@/views/chat/component/charts/utils.ts' export class Bar extends BaseG2Chart { constructor(id: string) { @@ -18,10 +19,12 @@ export class Bar extends BaseG2Chart { return } + const _data = checkIsPercent(y[0], data) + const options: G2Spec = { ...this.chart.options(), type: 'interval', - data: data, + data: _data.data, coordinate: { transform: [{ type: 'transpose' }] }, encode: { x: x[0].value, @@ -45,14 +48,17 @@ export class Bar extends BaseG2Chart { }, tooltip: (data) => { if (series.length > 0) { - return { name: data[series[0].value], value: data[y[0].value] } + return { + name: data[series[0].value], + value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}`, + } } else { - return { name: y[0].name, value: data[y[0].value] } + return { name: y[0].name, value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}` } } }, labels: [ { - text: y[0].value, + text: (data: any) => `${data[y[0].value]}${_data.isPercent ? '%' : ''}`, transform: [ { type: 'overlapDodgeY' }, { type: 'contrastReverse' }, diff --git a/frontend/src/views/chat/component/charts/Column.ts b/frontend/src/views/chat/component/charts/Column.ts index 0ac99a5c..37d3a639 100644 --- a/frontend/src/views/chat/component/charts/Column.ts +++ b/frontend/src/views/chat/component/charts/Column.ts @@ -1,6 +1,7 @@ import { BaseG2Chart } from '@/views/chat/component/BaseG2Chart.ts' import type { ChartAxis, ChartData } from '@/views/chat/component/BaseChart.ts' import type { G2Spec } from '@antv/g2' +import { checkIsPercent } from '@/views/chat/component/charts/utils.ts' export class Column extends BaseG2Chart { constructor(id: string) { @@ -18,10 +19,12 @@ export class Column extends BaseG2Chart { return } + const _data = checkIsPercent(y[0], data) + const options: G2Spec = { ...this.chart.options(), type: 'interval', - data: data, + data: _data.data, encode: { x: x[0].value, y: y[0].value, @@ -44,14 +47,17 @@ export class Column extends BaseG2Chart { }, tooltip: (data) => { if (series.length > 0) { - return { name: data[series[0].value], value: data[y[0].value] } + return { + name: data[series[0].value], + value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}`, + } } else { - return { name: y[0].name, value: data[y[0].value] } + return { name: y[0].name, value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}` } } }, labels: [ { - text: y[0].value, + text: (data: any) => `${data[y[0].value]}${_data.isPercent ? '%' : ''}`, position: 'top', dy: -25, transform: [ diff --git a/frontend/src/views/chat/component/charts/Line.ts b/frontend/src/views/chat/component/charts/Line.ts index 3aade722..6b75fc17 100644 --- a/frontend/src/views/chat/component/charts/Line.ts +++ b/frontend/src/views/chat/component/charts/Line.ts @@ -1,6 +1,7 @@ import { BaseG2Chart } from '@/views/chat/component/BaseG2Chart.ts' import type { ChartAxis, ChartData } from '@/views/chat/component/BaseChart.ts' import type { G2Spec } from '@antv/g2' +import { checkIsPercent } from '@/views/chat/component/charts/utils.ts' export class Line extends BaseG2Chart { constructor(id: string) { @@ -18,10 +19,12 @@ export class Line extends BaseG2Chart { return } + const _data = checkIsPercent(y[0], data) + const options: G2Spec = { ...this.chart.options(), type: 'view', - data: data, + data: _data.data, encode: { x: x[0].value, y: y[0].value, @@ -44,7 +47,7 @@ export class Line extends BaseG2Chart { type: 'line', labels: [ { - text: y[0].value, + text: (data: any) => `${data[y[0].value]}${_data.isPercent ? '%' : ''}`, style: { dx: -10, dy: -12, @@ -58,9 +61,12 @@ export class Line extends BaseG2Chart { ], tooltip: (data) => { if (series.length > 0) { - return { name: data[series[0].value], value: data[y[0].value] } + return { + name: data[series[0].value], + value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}`, + } } else { - return { name: y[0].name, value: data[y[0].value] } + return { name: y[0].name, value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}` } } }, }, diff --git a/frontend/src/views/chat/component/charts/Pie.ts b/frontend/src/views/chat/component/charts/Pie.ts index be9bd727..5d241e2b 100644 --- a/frontend/src/views/chat/component/charts/Pie.ts +++ b/frontend/src/views/chat/component/charts/Pie.ts @@ -1,7 +1,7 @@ import { BaseG2Chart } from '@/views/chat/component/BaseG2Chart.ts' import type { ChartAxis, ChartData } from '@/views/chat/component/BaseChart.ts' import type { G2Spec } from '@antv/g2' -import { endsWith, filter, replace } from 'lodash-es' +import { checkIsPercent } from '@/views/chat/component/charts/utils.ts' export class Pie extends BaseG2Chart { constructor(id: string) { @@ -18,39 +18,14 @@ export class Pie extends BaseG2Chart { } // % - const _data = [] - let isPercent = false - const notEmptyData = filter( - data, - (d) => - d && - d[y[0].value] !== null && - d[y[0].value] !== undefined && - d[y[0].value] !== 0 && - d[y[0].value] !== '0' - ) - if (notEmptyData.length > 0) { - const v = notEmptyData[0][y[0].value] + '' - if (endsWith(v.trim(), '%')) { - isPercent = true - } - } - for (let i = 0; i < data.length; i++) { - const v = data[i] - const _v = { ...v } - if (isPercent) { - const formatValue = replace(v[y[0].value], '%', '') - _v[y[0].value] = Number(formatValue) - } - _data.push(_v) - } + const _data = checkIsPercent(y[0], data) const options: G2Spec = { ...this.chart.options(), type: 'interval', coordinate: { type: 'theta', outerRadius: 0.8 }, transform: [{ type: 'stackY' }], - data: _data, + data: _data.data, encode: { y: y[0].value, color: series[0].value, @@ -62,11 +37,11 @@ export class Pie extends BaseG2Chart { { position: 'outside', text: (data: any) => - `${data[series[0].value]}: ${data[y[0].value]}${isPercent ? '%' : ''}`, + `${data[series[0].value]}: ${data[y[0].value]}${_data.isPercent ? '%' : ''}`, }, ], tooltip: (data) => { - return { name: y[0].name, value: `${data[y[0].value]}${isPercent ? '%' : ''}` } + return { name: y[0].name, value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}` } }, } diff --git a/frontend/src/views/chat/component/charts/utils.ts b/frontend/src/views/chat/component/charts/utils.ts new file mode 100644 index 00000000..2e1fa1f2 --- /dev/null +++ b/frontend/src/views/chat/component/charts/utils.ts @@ -0,0 +1,41 @@ +import type { ChartAxis, ChartData } from '@/views/chat/component/BaseChart.ts' +import { endsWith, filter, replace } from 'lodash-es' + +interface CheckedData { + isPercent: boolean + data: Array +} + +export function checkIsPercent(valueAxis: ChartAxis, data: Array): CheckedData { + const result: CheckedData = { + isPercent: false, + data: [], + } + + const notEmptyData = filter( + data, + (d) => + d && + d[valueAxis.value] !== null && + d[valueAxis.value] !== undefined && + d[valueAxis.value] !== 0 && + d[valueAxis.value] !== '0' + ) + if (notEmptyData.length > 0) { + const v = notEmptyData[0][valueAxis.value] + '' + if (endsWith(v.trim(), '%')) { + result.isPercent = true + } + } + for (let i = 0; i < data.length; i++) { + const v = data[i] + const _v = { ...v } as ChartData + if (result.isPercent) { + const formatValue = replace(v[valueAxis.value], '%', '') + _v[valueAxis.value] = Number(formatValue) + } + result.data.push(_v) + } + + return result +}