LineChart.vue
2.54 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
<template>
<view class="charts-box">
<!-- :canvas2d="true" tooltipFormat="showYLable" :onmovetip="true" -->
<qiun-data-charts :type="chartType" :opts="opts" :chartData="chartData" :reshow="reshow" :tooltipShow="true" />
</view>
</template>
<script setup>
import { ref, reactive, watch } from 'vue';
// 1. 定义 Props 接收父组件数据
const props = defineProps({
// 传入的分类数据 (横坐标)
chartType: { // 新增
type: String,
default: 'line'
},
categories: {
type: Array,
default: () => [],
},
// 传入的系列数据 (纵坐标内容)
series: {
type: Array,
default: () => [],
},
// 专门用于解决弹窗不显示问题的属性
reshow: {
type: Boolean,
default: false,
},
});
const chartData = ref({});
// 2. 图表配置项
const opts = reactive({
dataLabel: false,
color: [
'#1890FF',
'#91CB74',
'#FAC858',
'#EE6666',
'#73C0DE',
'#3CA272',
'#FC8452',
'#9A60B4',
'#ea7ccc',
],
padding: [15, 10, 0, 15],
enableScroll: false,
legend: {
show: true,
position: 'top'
},
xAxis: {
disableGrid: true,
// itemCount: 7, // 一共显示7个标签:1、6、11、16、21、26、31
labelCount: 7,
// splitNumber: 6,
},
yAxis: {
gridType: 'dash',
axisLabel: { show: false },
dashLength: 2,
},
extra: {
line: {
type: 'straight',
width: 2,
activeType: 'hollow',
},
// 新增下面这段
column: {
width: 30, // 柱子宽度
radius: [4, 4, 0, 0] // 柱子圆角
},
tooltip: {
legendShow: true,
}
},
});
// 3. 核心逻辑:格式化数据
// const formatData = () => {
// if (props.categories.length > 0) {
// chartData.value = {
// categories: props.categories,
// series: props.series,
// };
// }
// };
const formatData = () => {
const cat = props.categories || [];
let ser = props.series || [];
console.log('cat=', cat, 'ser=', ser);
// 强制保证 series 每一项都有 data 数组
ser = ser.map(item => ({
...item,
data: item.data || [] // 最关键:没有data就给空数组
}));
if (cat.length > 0) {
chartData.value = {
categories: cat,
series: ser
};
}
};
// 4. 监听 Props 变化,当父组件传入新数据时自动重绘
watch(
() => [props.categories, props.series, props.chartType],
() => {
formatData();
},
{ immediate: true, deep: true },
);
</script>
<style lang="scss" scoped>
.charts-box {
width: 100%;
height: 100%;
}
</style>