前言
基于echarts5.x和vue2实现
记录以便日后查阅
实现效果

代码实现
<template>
<div class="chart-wrap">
<ul class="legend-list">
<li
v-for="(item, index) in legend"
:key="index"
:class="['legend', item.selected ? '': 'un-active']"
@mouseenter="enterHandler(item)"
@mouseleave="leaveHandler(item)"
@click="clickHandler(item)"
>
<img :src="item.url" :alt="item.title">
<span>{{ item.title }}span>
li>
ul>
<div id="chart01" class="chart" />
div>
template>
<script>
export default {
name: 'Index',
data () {
return {
chart: null,
data: {
dates: ['2017', '2018', '2019', '2020', '2021'],
values: [
[100, 75, 125, 37, 10],
[40, 15, 20, 30, 11],
[40, 55, 23, 30, 12],
[0, 24, 20, 19, 12]
]
},
legend: [
{ url: require('../../../assets/img/01/czrk.png'), title: '常驻人口', selected: true },
{ url: require('../../../assets/img/01/wlrk.png'), title: '外来人口', selected: true },
{ url: require('../../../assets/img/01/70ys.png'), title: '70岁以上', selected: true },
{ url: require('../../../assets/img/01/14yx.png'), title: '14岁以下', selected: true }
]
}
},
mounted() {
this.createChartHandler()
},
methods: {
createChartHandler () {
this.chart = this.$echarts.init(document.getElementById('chart01'))
this.chart.setOption(this.getChartOption(this.data, this.legend))
window.addEventListener('resize', () => {
setTimeout(() => {
this.chart.resize()
})
})
},
getChartOption (data, legend) {
return {
legend: {
show: false,
data: legend.map(i => i.title)
},
tooltip: {
trigger: 'axis',
extraCssText:
'color:#fff;background: rgba(0, 38, 118, 0.5);border:none; box-shadow: 0px 0px 8px 1px rgba(0, 145, 255, 0.5);border-radius: 2px;z-index:99',
formatter: function (e) {
let str = `${e[0].name}`
e.forEach(it => {
str += `${it.marker} ${it.seriesName} ${it.value}人`
})
str += '
'
return str
}
},
textStyle: {
color: '#fff'
},
yAxis: {
type: 'value',
splitLine: {
show: true,
lineStyle: {
color: 'rgba(0,219,255,0.4000)',
type: 'dashed'
}
}
},
xAxis: {
type: 'category',
data: data.dates,
boundaryGap: false,
axisTick: {
show: false
},
axisLine: {
lineStyle: {
type: 'dashed',
color: 'rgba(0,219,255,0.2000)'
}
}
},
grid: {
top: '5%',
left: '5%',
right: '5%',
bottom: '5%',
containLabel: true
},
series: [
{
name: '常驻人口',
data: data.values[0],
type: 'line',
smooth: true,
color: '#3C7CFF'
}, {
name: '外来人口',
data: data.values[1],
type: 'line',
smooth: true,
color: '#42E0FC'
}, {
name: '70岁以上',
data: data.values[2],
type: 'line',
smooth: true,
color: '#3AE4A2'
}, {
name: '14岁以下',
data: data.values[3],
type: 'line',
smooth: true,
color: '#359983'
}
]
}
},
enterHandler (item) {
if (!this.chart) return
this.chart.dispatchAction({
type: 'highlight',
seriesName: item.title
})
},
leaveHandler (item) {
if (!this.chart) return
this.chart.dispatchAction({
type: 'downplay',
name: item.title
})
},
clickHandler (item) {
if (!this.chart) return
item.selected = !item.selected
this.chart.dispatchAction({
type: 'legendToggleSelect',
name: item.title
})
}
}
}
script>
- 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
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169