LineChart.vue
3.37 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
<script setup lang="ts">
import { ref, Ref, onMounted, computed, watch } from 'vue';
import { useECharts } from '/@/hooks/web/useECharts';
import { useAppStore } from '/@/store/modules/app';
import * as echarts from 'echarts';
const props = defineProps({
seriesData: {
type: Array,
default: () => [],
},
});
const appStore = useAppStore();
const skinName = computed(() => {
return appStore.getDarkMode === 'light' ? '#ffffff' : '#151515';
});
const chartRef = ref<HTMLDivElement | null>(null);
const { setOptions, resize } = useECharts(chartRef as Ref<HTMLDivElement>);
const getOptions: any = () => {
return {
backgroundColor: skinName.value,
// 柱状图的颜色
color: ['#ffdb5c', '#9fe6b8', '#67e0e3', '#32c5e9', '#37a2da'],
tooltip: {
show: true,
},
grid: {
left: 50,
right: 50,
top: 10, //拉伸距离顶部高度
bottom: 0, //拉伸距离底部高度
containLabel: true,
},
//X轴
xAxis: {
type: 'value',
axisLine: {
show: false,
},
axisTick: {
show: false,
},
//不显示X轴刻度线和数字
splitLine: { show: false },
axisLabel: { show: false },
},
yAxis: {
type: 'category',
data: props.seriesData.map((item: any) => item.label),
//升序
inverse: true,
splitLine: { show: false },
axisLine: {
show: false,
},
axisTick: {
show: false,
},
//key和图间距
offset: 10,
//动画部分
animationDuration: 300,
animationDurationUpdate: 300,
//key文字大小
nameTextStyle: {
fontSize: 5,
},
},
series: [
{
//柱状图自动排序,排序自动让Y轴名字跟着数据动
realtimeSort: true,
type: 'bar',
data: props.seriesData.map((item: any) => item.value),
barWidth: 15,
barGap: 10,
smooth: true,
valueAnimation: true,
//Y轴数字显示部分
label: {
normal: {
show: true,
position: 'right',
valueAnimation: true,
offset: [5, -2],
textStyle: {
color: '#333',
fontSize: 13,
},
},
},
itemStyle: {
emphasis: {
barBorderRadius: 7,
},
//颜色样式部分
normal: {
barBorderRadius: 7,
color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
{ offset: 0, color: '#3977E6' },
{ offset: 1, color: '#37BBF8' },
]),
},
},
},
],
};
};
watch(
() => appStore.getDarkMode,
(target) => {
const backgroundColor = target === 'light' ? '#ffffff' : '#151515';
setOptions &&
setOptions({
...getOptions(),
backgroundColor,
});
}
);
onMounted(() => {
setOptions(getOptions());
//自适应
window.addEventListener('resize', () => resize());
});
</script>
<template>
<div class="w-180 h-70">
<div ref="chartRef" class="w-full h-full"></div>
</div>
</template>
<style scoped></style>