index.vue
3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<template>
<Chart :options="chartOption" autoresize></Chart>
</template>
<script setup lang="ts">
import useChartOption from '@/hooks/chart-option';
import { nextTick, onMounted, ref } from 'vue';
const props = defineProps({
chartData: {
type: Object,
default: {
yAxis: [],
xAxis: [],
legend: [],
xAxisUnit: []
}
}
})
const renderChart = ref(false)
const xAxis =ref<string[]>([]);
//Y轴单位
const yAxis: any = ref<any>([]);
//Y轴数据
const series: any = ref<any>([]);
const { chartOption } = useChartOption(() => {
return {
tooltip: { trigger: "axis" },
legend: {
data: props.chartData.legend,
icon: "roundRect",
itemWidth: 8,
itemHeight: 8,
left: 40,
bottom: 0,
},
grid: {
top: 10,
},
xAxis: [
{
type: "category",
data: xAxis.value,
// 阴影指示器
axisPointer: {
type: "shadow",
},
axisTick: {
// 轴刻度
show: false,
},
},
],
yAxis: yAxis.value,
dataZoom: [
{
type: "inside",
},
{
backgroundColor: "#7BCFA3",
height: 10,
moveHandleSize: 0,
},
],
series: series.value,
};
})
const _getMaxValue = (arr: any) => {
const max = Math.max(...arr);
return Math.ceil(max / 9.5) * 10;
}
const _getMinValue = (arr: any) => {
const min = Math.min(...arr);
return Math.floor(min / 12) * 10;
}
onMounted(() => {
xAxis.value = props.chartData.xAxis;
if (props.chartData.yAxisUnit && props.chartData.yAxisUnit.length > 0) {
const values: any = [];
if (props.chartData.yAxis && props.chartData.yAxis.length > 0) {
props.chartData.yAxis.forEach((item: any, index: number) => {
values.push({
minValue: _getMinValue(item.dataList),
maxValue: _getMaxValue(item.dataList),
});
});
}
props.chartData.yAxisUnit.forEach((item: any, index: number) => {
yAxis.value.push({
type: "value",
name: item,
show: true,
// min: values[index].minValue,
// max: values[index].maxValue,
// interval: (values[index].maxValue - values[index].minValue) / 5,
axisLabel: {
formatter: "{value}",
},
boundaryGap: [0.2, 0.2]
});
});
}
if (props.chartData.yAxis && props.chartData.yAxis.length > 0) {
props.chartData.yAxis.forEach((item: any, index: number) => {
series.value.push({
name: item.name,
type: props.chartData.chartType[index],
yAxisIndex: index,
symbol: "circle",
smooth: true,
showSymbol: false,
color: index % 2 == 0 ? "#7BCFA3" : "#F3B07E",
tooltip: {
valueFormatter: function (value: any) {
return value + props.chartData.yAxisUnit[index];
},
},
data: item.dataList,
});
});
}
console.log("series",series.value);
})
// wait container expand
nextTick(() => {
renderChart.value = true
})
</script>