Files
SQLBot/frontend/src/views/chat/component/charts/Column.ts
T

163 lines
4.1 KiB
TypeScript

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,
formatNumber,
getAxesWithFilter,
processMultiQuotaData,
} from '@/views/chat/component/charts/utils.ts'
export class Column extends BaseG2Chart {
constructor(id: string) {
super(id, 'column')
}
init(axis: Array<ChartAxis>, data: Array<ChartData>) {
super.init(axis, data)
const axes = getAxesWithFilter(this.axis)
if (axes.x.length == 0 || axes.y.length == 0) {
console.debug({ instance: this })
return
}
let config = {
data: data,
y: axes.y,
series: axes.series,
}
if (axes.multiQuota.length > 0) {
config = processMultiQuotaData(
axes.x,
config.y,
axes.multiQuota,
axes.multiQuotaName,
config.data
)
}
const x = axes.x
const y = config.y
const series = config.series
const _data = checkIsPercent(y, config.data)
console.debug({ 'render-info': { x: x, y: y, series: series, data: _data }, instance: this })
const options: G2Spec = {
...this.chart.options(),
type: 'interval',
data: _data.data,
encode: {
x: x[0].value,
y: y[0].value,
color: series.length > 0 ? series[0].value : undefined,
},
style: {
radiusTopLeft: (d: ChartData) => {
if (d[y[0].value] && d[y[0].value] > 0) {
return 4
}
return 0
},
radiusTopRight: (d: ChartData) => {
if (d[y[0].value] && d[y[0].value] > 0) {
return 4
}
return 0
},
radiusBottomLeft: (d: ChartData) => {
if (d[y[0].value] && d[y[0].value] < 0) {
return 4
}
return 0
},
radiusBottomRight: (d: ChartData) => {
if (d[y[0].value] && d[y[0].value] < 0) {
return 4
}
return 0
},
},
axis: {
x: {
title: false, // x[0].name,
labelFontSize: 12,
labelAutoHide: {
type: 'hide',
keepHeader: true,
keepTail: true,
},
labelAutoRotate: false,
labelAutoWrap: true,
labelAutoEllipsis: true,
},
y: {
title: false, // y[0].name,
labelFormatter: (value: any) => {
return String(formatNumber(value))
},
},
},
scale: {
x: {
nice: true,
},
y: {
nice: true,
type: 'linear',
},
},
interaction: {
elementHighlight: { background: true, region: true },
tooltip: { series: series.length > 0, shared: true },
},
tooltip: (data: any) => {
if (series.length > 0) {
return {
name: data[series[0].value],
value: `${formatNumber(data[y[0].value])}${_data.isPercent ? '%' : ''}`,
}
} else {
return {
name: y[0].name,
value: `${formatNumber(data[y[0].value])}${_data.isPercent ? '%' : ''}`,
}
}
},
labels: this.showLabel
? [
{
text: (data: any) => {
const value = data[y[0].value]
if (value === undefined || value === null) {
return ''
}
return `${formatNumber(value)}${_data.isPercent ? '%' : ''}`
},
position: (data: any) => {
if (data[y[0].value] < 0) {
return 'bottom'
}
return 'top'
},
transform: [
{ type: 'contrastReverse' },
{ type: 'exceedAdjust' },
{ type: 'overlapHide' },
],
},
]
: [],
} as G2Spec
if (series.length > 0) {
options.transform = [{ type: 'stackY' }]
}
this.chart.options(options)
}
}