Skip to content

Commit 245dab2

Browse files
JasonZhao-123456ulleo
authored andcommitted
Feat: support data labels on charts
1 parent beebe6c commit 245dab2

File tree

7 files changed

+111
-46
lines changed

7 files changed

+111
-46
lines changed

frontend/src/views/chat/chat-block/ChartBlock.vue

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import icon_into_item_outlined from '@/assets/svg/icon_into-item_outlined.svg'
1919
import icon_window_max_outlined from '@/assets/svg/icon_window-max_outlined.svg'
2020
import icon_window_mini_outlined from '@/assets/svg/icon_window-mini_outlined.svg'
2121
import icon_copy_outlined from '@/assets/svg/icon_copy_outlined.svg'
22+
import ICON_STYLE from '@/assets/svg/icon_style-set_outlined.svg'
2223
import { useI18n } from 'vue-i18n'
2324
import SQLComponent from '@/views/chat/component/SQLComponent.vue'
2425
import { useAssistantStore } from '@/stores/assistant'
@@ -211,6 +212,8 @@ function showSql() {
211212
sqlShow.value = true
212213
}
213214
215+
const showLabel = ref(false)
216+
214217
function addToDashboard() {
215218
const recordeInfo = {
216219
id: '1-1',
@@ -393,6 +396,27 @@ watch(
393396
</el-tooltip>
394397
</div>
395398

399+
<div class="chart-select-container">
400+
<el-tooltip
401+
v-if="currentChartType !== 'table'"
402+
effect="dark"
403+
:offset="8"
404+
:content="showLabel ? t('chat.hide_label') : t('chat.show_label')"
405+
placement="top"
406+
>
407+
<el-button
408+
class="tool-btn"
409+
:class="{ 'chart-active': showLabel }"
410+
text
411+
@click="showLabel = !showLabel"
412+
>
413+
<el-icon size="16">
414+
<ICON_STYLE />
415+
</el-icon>
416+
</el-button>
417+
</el-tooltip>
418+
</div>
419+
396420
<div v-if="message?.record?.sql">
397421
<el-tooltip effect="dark" :offset="8" :content="t('chat.show_sql')" placement="top">
398422
<el-button class="tool-btn" text @click="showSql">
@@ -498,6 +522,7 @@ watch(
498522
:message="message"
499523
:data="data"
500524
:loading-data="loadingData"
525+
:show-label="showLabel"
501526
/>
502527
</div>
503528
<div v-if="dataObject.limit" class="over-limit-hint">

frontend/src/views/chat/component/BaseChart.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export abstract class BaseChart {
1717
_name: string = 'base-chart'
1818
axis: Array<ChartAxis> = []
1919
data: Array<ChartData> = []
20+
showLabel: boolean = false
2021

2122
constructor(id: string, name: string) {
2223
this.id = id

frontend/src/views/chat/component/ChartComponent.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const params = withDefaults(
1414
y?: Array<ChartAxis>
1515
series?: Array<ChartAxis>
1616
multiQuotaName?: string | undefined
17+
showLabel?: boolean
1718
}>(),
1819
{
1920
data: () => [],
@@ -22,6 +23,7 @@ const params = withDefaults(
2223
y: () => [],
2324
series: () => [],
2425
multiQuotaName: undefined,
26+
showLabel: false,
2527
}
2628
)
2729
@@ -61,14 +63,21 @@ const axis = computed(() => {
6163
6264
let chartInstance: BaseChart | undefined
6365
64-
function renderChart() {
6566
chartInstance = getChartInstance(params.type, chartId.value)
6667
if (chartInstance) {
68+
chartInstance.showLabel = params.showLabel
6769
chartInstance.init(axis.value, params.data)
6870
chartInstance.render()
6971
}
7072
}
7173
74+
watch(
75+
() => params.showLabel,
76+
() => {
77+
renderChart()
78+
}
79+
)
80+
7281
function destroyChart() {
7382
if (chartInstance) {
7483
chartInstance.destroy()

frontend/src/views/chat/component/DisplayChartBlock.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const props = defineProps<{
1111
message: ChatMessage
1212
data: Array<{ [key: string]: any }>
1313
loadingData?: boolean
14+
showLabel?: boolean
1415
}>()
1516
1617
const { t } = useI18n()
@@ -117,6 +118,7 @@ defineExpose({
117118
:series="series"
118119
:data="data"
119120
:multi-quota-name="multiQuotaName"
121+
:show-label="showLabel"
120122
/>
121123
<el-empty v-else :description="loadingData ? t('chat.loading_data') : t('chat.no_data')" />
122124
</div>

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export class Bar extends BaseG2Chart {
119119
elementHighlight: { background: true, region: true },
120120
tooltip: { series: series.length > 0, shared: true },
121121
},
122-
tooltip: (data) => {
122+
tooltip: (data: any) => {
123123
if (series.length > 0) {
124124
return {
125125
name: data[series[0].value],
@@ -129,6 +129,30 @@ export class Bar extends BaseG2Chart {
129129
return { name: y[0].name, value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}` }
130130
}
131131
},
132+
labels: this.showLabel
133+
? [
134+
{
135+
text: (data: any) => {
136+
const value = data[y[0].value]
137+
if (value === undefined || value === null) {
138+
return ''
139+
}
140+
return `${value}${_data.isPercent ? '%' : ''}`
141+
},
142+
position: (data: any) => {
143+
if (data[y[0].value] < 0) {
144+
return 'left'
145+
}
146+
return 'right'
147+
},
148+
transform: [
149+
{ type: 'contrastReverse' },
150+
{ type: 'exceedAdjust' },
151+
{ type: 'overlapHide' },
152+
],
153+
},
154+
]
155+
: [],
132156
} as G2Spec
133157

134158
if (series.length > 0) {

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class Column extends BaseG2Chart {
110110
elementHighlight: { background: true, region: true },
111111
tooltip: { series: series.length > 0, shared: true },
112112
},
113-
tooltip: (data) => {
113+
tooltip: (data: any) => {
114114
if (series.length > 0) {
115115
return {
116116
name: data[series[0].value],
@@ -120,28 +120,30 @@ export class Column extends BaseG2Chart {
120120
return { name: y[0].name, value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}` }
121121
}
122122
},
123-
// labels: [
124-
// {
125-
// text: (data: any) => {
126-
// const value = data[y[0].value]
127-
// if (value === undefined || value === null) {
128-
// return ''
129-
// }
130-
// return `${value}${_data.isPercent ? '%' : ''}`
131-
// },
132-
// position: (data: any) => {
133-
// if (data[y[0].value] < 0) {
134-
// return 'bottom'
135-
// }
136-
// return 'top'
137-
// },
138-
// transform: [
139-
// { type: 'contrastReverse' },
140-
// { type: 'exceedAdjust' },
141-
// { type: 'overlapHide' },
142-
// ],
143-
// },
144-
// ],
123+
labels: this.showLabel
124+
? [
125+
{
126+
text: (data: any) => {
127+
const value = data[y[0].value]
128+
if (value === undefined || value === null) {
129+
return ''
130+
}
131+
return `${value}${_data.isPercent ? '%' : ''}`
132+
},
133+
position: (data: any) => {
134+
if (data[y[0].value] < 0) {
135+
return 'bottom'
136+
}
137+
return 'top'
138+
},
139+
transform: [
140+
{ type: 'contrastReverse' },
141+
{ type: 'exceedAdjust' },
142+
{ type: 'overlapHide' },
143+
],
144+
},
145+
]
146+
: [],
145147
} as G2Spec
146148

147149
if (series.length > 0) {

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,29 @@ export class Line extends BaseG2Chart {
8686
encode: {
8787
shape: 'smooth',
8888
},
89-
// labels: [
90-
// {
91-
// text: (data: any) => {
92-
// const value = data[y[0].value]
93-
// if (value === undefined || value === null) {
94-
// return ''
95-
// }
96-
// return `${value}${_data.isPercent ? '%' : ''}`
97-
// },
98-
// style: {
99-
// dx: -10,
100-
// dy: -12,
101-
// },
102-
// transform: [
103-
// { type: 'contrastReverse' },
104-
// { type: 'exceedAdjust' },
105-
// { type: 'overlapHide' },
106-
// ],
107-
// },
108-
// ],
109-
tooltip: (data) => {
89+
labels: this.showLabel
90+
? [
91+
{
92+
text: (data: any) => {
93+
const value = data[y[0].value]
94+
if (value === undefined || value === null) {
95+
return ''
96+
}
97+
return `${value}${_data.isPercent ? '%' : ''}`
98+
},
99+
style: {
100+
dx: -10,
101+
dy: -12,
102+
},
103+
transform: [
104+
{ type: 'contrastReverse' },
105+
{ type: 'exceedAdjust' },
106+
{ type: 'overlapHide' },
107+
],
108+
},
109+
]
110+
: [],
111+
tooltip: (data: any) => {
110112
if (series.length > 0) {
111113
return {
112114
name: data[series[0].value],

0 commit comments

Comments
 (0)