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

代码实现
<template>
<div class="chart-wrap">
<ul class="legend-list">
<li
v-for="(item, index) in yData"
:key="index"
:class="['legend', item.selected ? '': 'un-active']"
@mouseenter="enterHandler(item)"
@mouseleave="leaveHandler(item)"
@click="clickHandler(item)"
>
<i v-if="item.name === 'PH'" class="rect" :style="{ backgroundColor: item.color }" />
<i v-else class="line" :style="{ backgroundColor: item.color }" />
<span>{{ item.name }}span>
li>
ul>
<div id="chart15" class="chart" />
div>
template>
<script>
export default {
name: 'Index',
data () {
return {
chart: null,
yData: [
{ name: 'PH', value: [7.4, 7.4, 7.3, 7.5, 7.3, 7.3, 7.3, 7.3, 7.3, 7.4, 7.6, 7.7, 8.1, 7.4, 7.6, 7.6, 7.3, 7.2, 7.2, 7.5, 7.4, 8.2, 7.5, 7.4, 7.2], color: '#45de81', selected: true },
{ name: '含氮量', value: [17, 16, 16, 10, 18, 14, 18, 18, 14, 10, 17, 10, 17, 18, 19, 17, 10, 17, 19, 16, 12, 15, 12, 16, 10], color: '#0091FF', selected: true }
],
xData: ['01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00']
}
},
mounted() {
this.createChartHandler()
},
methods: {
createChartHandler () {
this.chart = this.$echarts.init(document.getElementById('chart15'))
this.chart.setOption(this.getChartOption(this.yData, this.xData))
window.addEventListener('resize', () => {
setTimeout(() => {
this.chart.resize()
})
})
},
getChartOption (yData, xData) {
return {
grid: {
top: '5%',
left: '5%',
right: '5%',
bottom: '5%',
containLabel: true
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999'
}
},
textStyle: {
color: '#fff'
},
formatter: function (params) {
const axisLabel = params[0].axisValueLabel
return `${axisLabel}
${params[0].color};margin-right:15px">${params[0].seriesName}:${params[0].value}
${params[1].color};margin-right:15px">${params[1].seriesName}:${params[1].value}%`
},
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'
},
legend: {
show: false,
data: yData.map(i => i.name)
},
xAxis: [
{
type: 'category',
data: xData,
axisLabel: {
textStyle: {
fontSize: 12,
color: '#fff'
}
},
axisLine: {
lineStyle: {
color: 'rgba(42, 109, 251, 0.79)'
}
}
}
],
yAxis: [
{
type: 'value',
name: 'PH',
min: 0,
max: 30,
interval: 5,
axisLabel: {
textStyle: {
fontSize: 12,
color: '#fff'
}
},
axisLine: {
show: true,
lineStyle: {
color: 'rgba(42, 109, 251, 0.79)'
}
},
nameTextStyle: {
color: '#fff',
fontSize: 12
},
splitLine: {
show: true,
lineStyle: {
type: 'dashed',
color: 'rgba(0,145,255,.3)'
}
}
},
{
type: 'value',
name: '含氮量',
min: 0,
max: 30,
interval: 5,
axisLabel: {
textStyle: {
fontSize: 12,
color: '#fff'
},
formatter: '{value} %'
},
nameTextStyle: {
color: '#fff',
fontSize: 12,
align: 'center',
padding: [0, 0, 0, 0]
},
splitLine: {
show: true,
lineStyle: {
type: 'dashed',
color: 'rgba(0,145,255,.3)'
}
}
}
],
series: [
{
name: 'PH',
type: 'bar',
data: yData[0].value,
barWidth: 6,
itemStyle: {
color: yData[0].color
}
},
{
name: '含氮量',
type: 'line',
yAxisIndex: 1,
data: yData[1].value,
smooth: true,
symbol: 'circle',
showSymbol: false,
symbolSize: 5,
itemStyle: {
color: yData[1].color,
shadowColor: 'rgba(255, 255, 255, .5)',
shadowBlur: 10
},
lineStyle: {
color: yData[1].color,
shadowColor: '#fff',
shadowBlur: 10
}
}
]
}
},
enterHandler (item) {
if (!this.chart) return
this.chart.dispatchAction({
type: 'highlight',
seriesName: item.name
})
},
leaveHandler (item) {
if (!this.chart) return
this.chart.dispatchAction({
type: 'downplay',
seriesName: item.name
})
},
clickHandler (item) {
if (!this.chart) return
item.selected = !item.selected
this.chart.dispatchAction({
type: 'legendToggleSelect',
name: item.name
})
}
}
}
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
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221