<template>
|
<div class="h-[20rem] w-full" v-resize="chartContainerResize">
|
<div ref="chartRef"></div>
|
</div>
|
</template>
|
|
<script setup lang="ts">
|
import type * as echarts from 'echarts';
|
import _ from 'lodash';
|
import { ref } from 'vue';
|
import { SCATTER_SYMBOL_SIZE, chatComProps, getChatChartOption } from '../../../common';
|
import { useDrawChatChart } from '../../../hooks/useDrawChatChart';
|
const chartRef = ref<HTMLDivElement>(null);
|
const defaultDisplayType = 'line';
|
|
const props = defineProps(chatComProps);
|
|
const drawChart = () => {
|
const data = props.data;
|
const xType = 'time';
|
let timeIndex = data.cols.findIndex((item) => item.type === 'time');
|
if (timeIndex === -1) {
|
timeIndex = 0;
|
}
|
const timeCol = data.cols[timeIndex];
|
|
let valueIndex = data.cols.findIndex((item) => item.type === 'value');
|
if (valueIndex === -1) {
|
valueIndex = 2;
|
}
|
const valueCol = data.cols[valueIndex];
|
|
let nameCol = null;
|
let groupedValues = null;
|
if (data.chart === 'muli_line') {
|
let nameIndex = data.cols.findIndex((item) => item.type === 'name');
|
if (nameIndex === -1) {
|
nameIndex = 1;
|
}
|
nameCol = data.cols[nameIndex];
|
groupedValues = _.groupBy(data.values, (item) => item[nameIndex]);
|
} else if (data.chart === 'single_line') {
|
groupedValues = {
|
default: data.values,
|
};
|
} else {
|
// 默认都当muli_line
|
let nameIndex = data.cols.findIndex((item) => item.type === 'name');
|
if (nameIndex === -1) {
|
nameIndex = 1;
|
}
|
nameCol = data.cols[nameIndex];
|
groupedValues = _.groupBy(data.values, (item) => item[nameIndex]);
|
}
|
|
const seriesData = Object.keys(groupedValues).map((item) => {
|
const values = groupedValues[item];
|
return {
|
name: item === 'default' ? '' : item,
|
data: values.map((item) => [item[timeIndex], item[valueIndex]]),
|
type: defaultDisplayType,
|
symbol: 'none',
|
smooth: true,
|
};
|
});
|
const combineOption = _.defaultsDeep(getChatChartOption(), {
|
grid: {
|
bottom: 20,
|
},
|
toolbox: {
|
show: true,
|
feature: {
|
myBar: {
|
onclick: () => {
|
chartInstance.value.setOption({
|
series: seriesData.map((item) => ({
|
...item,
|
type: 'bar',
|
symbol: 'none',
|
})),
|
});
|
},
|
},
|
|
myScatter: {
|
onclick: () => {
|
chartInstance.value.setOption({
|
series: seriesData.map((item) => ({
|
...item,
|
type: 'scatter',
|
symbol: 'circle',
|
symbolSize: SCATTER_SYMBOL_SIZE,
|
})),
|
});
|
},
|
},
|
myLine: {
|
onclick: () => {
|
chartInstance.value.setOption({
|
series: seriesData.map((item) => ({
|
...item,
|
type: 'line',
|
symbol: 'none',
|
smooth: true,
|
})),
|
});
|
},
|
},
|
},
|
},
|
|
title: {
|
text: data?.title,
|
},
|
xAxis: {
|
name: timeCol?.title,
|
},
|
yAxis: {
|
name: valueCol?.title,
|
},
|
series: seriesData,
|
} as echarts.EChartsOption);
|
chartInstance.value.setOption(combineOption);
|
};
|
const { chartContainerResize, chartInstance } = useDrawChatChart({ chartRef, drawChart });
|
</script>
|
<style scoped lang="scss"></style>
|