LineChart.vue
1.73 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
<template>
<view class="charts-box">
<qiun-data-charts
type="line"
:opts="opts"
:chartData="chartData"
:reshow="reshow"
:canvas2d="true"
/>
</view>
</template>
<script setup>
import { ref, reactive, watch } from 'vue';
// 1. 定义 Props 接收父组件数据
const props = defineProps({
// 传入的分类数据 (横坐标)
categories: {
type: Array,
default: () => [],
},
// 传入的系列数据 (纵坐标内容)
series: {
type: Array,
default: () => [],
},
// 专门用于解决弹窗不显示问题的属性
reshow: {
type: Boolean,
default: false,
},
});
const chartData = ref({});
// 2. 图表配置项
const opts = reactive({
color: [
'#1890FF',
'#91CB74',
'#FAC858',
'#EE6666',
'#73C0DE',
'#3CA272',
'#FC8452',
'#9A60B4',
'#ea7ccc',
],
padding: [15, 10, 0, 15],
enableScroll: false,
legend: {},
xAxis: {
disableGrid: true,
},
yAxis: {
gridType: 'dash',
dashLength: 2,
},
extra: {
line: {
type: 'straight',
width: 2,
activeType: 'hollow',
},
},
});
// 3. 核心逻辑:格式化数据
const formatData = () => {
if (props.categories.length > 0) {
chartData.value = {
categories: props.categories,
series: props.series,
};
}
};
// 4. 监听 Props 变化,当父组件传入新数据时自动重绘
watch(
() => [props.categories, props.series],
() => {
formatData();
},
{ immediate: true, deep: true },
);
</script>
<style lang="scss" scoped>
.charts-box {
width: 100%;
height: 100%;
}
</style>