|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { ChatMessage } from '@/api/chat.ts' |
| 3 | +import DisplayChartBlock from '@/views/chat/component/DisplayChartBlock.vue' |
| 4 | +import { computed, ref } from 'vue' |
| 5 | +import { concat } from 'lodash-es' |
| 6 | +import type { ChartTypes } from '@/views/chat/component/BaseChart.ts' |
| 7 | +
|
| 8 | +const props = withDefaults( |
| 9 | + defineProps<{ |
| 10 | + message: ChatMessage |
| 11 | + isPredict?: boolean |
| 12 | + }>(), |
| 13 | + { |
| 14 | + isPredict: false, |
| 15 | + } |
| 16 | +) |
| 17 | +
|
| 18 | +const dataObject = computed<{ |
| 19 | + fields: Array<string> |
| 20 | + data: Array<{ [key: string]: any }> |
| 21 | +}>(() => { |
| 22 | + if (props.message?.record?.data) { |
| 23 | + if (typeof props.message?.record?.data === 'string') { |
| 24 | + return JSON.parse(props.message.record.data) |
| 25 | + } else { |
| 26 | + return props.message.record.data |
| 27 | + } |
| 28 | + } |
| 29 | + return {} |
| 30 | +}) |
| 31 | +
|
| 32 | +const data = computed(() => { |
| 33 | + if (props.isPredict) { |
| 34 | + let _list = [] |
| 35 | + if ( |
| 36 | + props.message?.record?.predict_data && |
| 37 | + typeof props.message?.record?.predict_data === 'string' |
| 38 | + ) { |
| 39 | + if ( |
| 40 | + props.message?.record?.predict_data.length > 0 && |
| 41 | + props.message?.record?.predict_data.trim().startsWith('[') && |
| 42 | + props.message?.record?.predict_data.trim().endsWith(']') |
| 43 | + ) { |
| 44 | + try { |
| 45 | + _list = JSON.parse(props.message?.record?.predict_data) |
| 46 | + } catch (e) { |
| 47 | + console.error(e) |
| 48 | + } |
| 49 | + } |
| 50 | + } else { |
| 51 | + if (props.message?.record?.predict_data.length > 0) { |
| 52 | + _list = props.message?.record?.predict_data |
| 53 | + } |
| 54 | + } |
| 55 | +
|
| 56 | + if (_list.length == 0) { |
| 57 | + return _list |
| 58 | + } |
| 59 | +
|
| 60 | + if (dataObject.value.data && dataObject.value.data.length > 0) { |
| 61 | + return concat(dataObject.value.data, _list) |
| 62 | + } |
| 63 | +
|
| 64 | + return _list |
| 65 | + } else { |
| 66 | + return dataObject.value.data |
| 67 | + } |
| 68 | +}) |
| 69 | +
|
| 70 | +const chartRef = ref() |
| 71 | +
|
| 72 | +const chartObject = computed<{ |
| 73 | + type: ChartTypes |
| 74 | + title: string |
| 75 | + axis: { |
| 76 | + x: { name: string; value: string } |
| 77 | + y: { name: string; value: string } |
| 78 | + series: { name: string; value: string } |
| 79 | + } |
| 80 | + columns: Array<{ name: string; value: string }> |
| 81 | +}>(() => { |
| 82 | + if (props.message?.record?.chart) { |
| 83 | + return JSON.parse(props.message.record.chart) |
| 84 | + } |
| 85 | + return {} |
| 86 | +}) |
| 87 | +
|
| 88 | +const currentChartType = ref<ChartTypes | undefined>(undefined) |
| 89 | +
|
| 90 | +const chartType = computed<ChartTypes>({ |
| 91 | + get() { |
| 92 | + if (currentChartType.value) { |
| 93 | + return currentChartType.value |
| 94 | + } |
| 95 | + return chartObject.value.type ?? 'table' |
| 96 | + }, |
| 97 | + set(v) { |
| 98 | + currentChartType.value = v |
| 99 | + }, |
| 100 | +}) |
| 101 | +</script> |
| 102 | + |
| 103 | +<template> |
| 104 | + <div |
| 105 | + v-if=" |
| 106 | + (!isPredict && (message?.record?.sql || message?.record?.chart)) || |
| 107 | + (isPredict && message?.record?.chart && data.length > 0) |
| 108 | + " |
| 109 | + class="chart-component-container" |
| 110 | + > |
| 111 | + <div class="header-bar">todo</div> |
| 112 | + <div v-if="message?.record?.chart" class="chart-block"> |
| 113 | + <DisplayChartBlock |
| 114 | + :id="message.record.id" |
| 115 | + ref="chartRef" |
| 116 | + :chart-type="chartType" |
| 117 | + :message="message" |
| 118 | + :data="data" |
| 119 | + /> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | +</template> |
| 123 | + |
| 124 | +<style scoped lang="less"> |
| 125 | +.chart-component-container { |
| 126 | + width: 100%; |
| 127 | + padding: 16px; |
| 128 | + display: flex; |
| 129 | + flex-direction: column; |
| 130 | +
|
| 131 | + gap: 16px; |
| 132 | +
|
| 133 | + border: 1px solid rgba(222, 224, 227, 1); |
| 134 | + border-radius: 12px; |
| 135 | +
|
| 136 | + .header-bar { |
| 137 | + height: 32px; |
| 138 | + } |
| 139 | +
|
| 140 | + .chart-block { |
| 141 | + height: 352px; |
| 142 | + width: 100%; |
| 143 | + } |
| 144 | +} |
| 145 | +</style> |
0 commit comments