HorizontalBarChart.vue
2.62 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
<template>
<div class="horizontal-bar">
<p class="title">{{ title }}</p>
<div ref="horizontalBarChart" style="width: 100%; height: 20vh;"></div>
</div>
</template>
<script>
import * as echarts from 'echarts';
import resize from './../mixin/resize'
export default {
name: 'HorizontalBarChart',
mixins: [resize],
props: {
title: {
type: String,
default: '',
},
data: {
type: Array,
default: () => [
{name: '<20岁', value: 4},
{name: '20–30岁', value: 10},
{name: '30–40岁', value: 25},
{name: '40–50岁', value: 30},
{name: '50–60岁', value: 20},
{name: '60–70岁', value: 10},
{name: '≥70岁', value: 1},
],
},
colors: {
type: Array,
default: () => [],
},
},
mounted() {
this.initChart(this.$refs.horizontalBarChart);
this.drawChart();
},
methods: {
drawChart() {
const chart = echarts.init(this.$refs.horizontalBarChart);
const option = {
tooltip: {
// trigger: 'axis',
// axisPointer: {
// type: 'shadow',
// },
show: false
},
grid: {
top: '5%',
left: '6%',
right: '8%',
bottom: '3%',
containLabel: true,
},
xAxis: {
type: 'value',
axisTick: {show: false},
splitLine: {show: false}, // 去掉背景的竖线
axisLabel: { show: false }
},
yAxis: {
type: 'category',
data: this.data.map(item => item.name),
axisTick: {show: false}, // 去掉y轴刻度
axisLine: {show: false},
axisLabel: {
textStyle:{
padding:[0,20,0,0]
}
}
},
series: [
{
// name: '年龄分布',
type: 'bar',
data: this.data.map(item => item.value),
itemStyle: {
color: new echarts.graphic.LinearGradient(
0, 0, 1, 0, // 渐变方向从左到右
this.colors
),
barBorderRadius: 4
},
label: {
show: true,
position: 'right',
valueAnimation: true,
formatter: (params) => `${params.value}%` // 在柱状图右边展示x轴的数据和名称
}
},
],
};
chart.setOption(option);
},
},
};
</script>
<style lang="scss" scoped>
.horizontal-bar {
margin: 2vh 0;
.title {
color: #333333;
font-size: 16px;
font-weight: 600;
}
}
</style>