前言
基于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 !== '噪声'" class="rect" :style="{ backgroundColor: item.color }" />
<i v-else class="line" :style="{ backgroundColor: item.color }" />
<span>{{ item.name }}</span>
</li>
</ul>
<div id="chart16" class="chart" />
</div>
</template>
<script>
export default {
name: 'Index',
data () {
return {
chart: null,
yData: [
{ name: 'PM2.5', value: [58, 29, 50, 26, 41, 44, 27, 44, 32, 32, 29, 45, 32, 28, 50, 25, 37, 22, 25, 56, 40, 32, 40, 24, 54], color: '#F7B500', selected: true },
{ name: 'PM10', value: [64, 63, 68, 62, 55, 61, 70, 71, 60, 78, 73, 63, 82, 59, 81, 82, 55, 81, 88, 64, 66, 50, 61, 76, 70], color: '#06C88C', selected: true },
{ name: '噪声', value: [30, 38, 30, 33, 54, 49, 54, 57, 33, 52, 59, 44, 47, 31, 40, 55, 53, 48, 46, 52, 53, 41, 55, 33, 46], 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('chart16'))
this.chart.setOption(this.getChartOption(this.yData, this.xData))
window.addEventListener('resize', () => {
setTimeout(() => {
this.chart.resize()
})
})
},
getChartOption (yData, xData) {
return {
color: yData.map(i => i.color),
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',
axisPointer: {
type: 'cross'
},
formatter: function (params) {
const axisLabel = params[0].axisValueLabel
return `${axisLabel}<br>
<span style="display:inline-block;border-radius:50%;width:10px;height:10px;background:${params[0].color};margin-right:15px"></span><span>${params[0].seriesName}:${params[0].value}</span><br>
<span style="display:inline-block;border-radius:50%;width:10px;height:10px;background:${params[1].color};margin-right:15px"></span><span>${params[1].seriesName}:${params[1].value}%</span><br>
<span style="display:inline-block;border-radius:50%;width:10px;height:10px;background:${params[2].color};margin-right:15px"></span><span>${params[2].seriesName}:${params[2].value}%</span>`
},
},
grid: {
top: '5%',
left: '10%',
right: '15%',
bottom: '10%',
containLabel: false
},
legend: {
show: false,
data: yData.map(i => i.name)
},
xAxis: [
{
type: 'category',
axisTick: {
alignWithLabel: true
},
axisLabel: {
textStyle: {
fontSize: 12,
color: '#fff'
}
},
data: xData
}
],
yAxis: [
{
type: 'value',
min: 0,
max: 200,
interval: 40,
position: 'right',
axisLine: {
lineStyle: {
color: yData[0].color
}
},
axisLabel: {
textStyle: {
fontSize: 10,
color: '#fff'
},
formatter: '{value} μg/m³'
}
},
{
show: false,
type: 'value',
name: 'PM10',
min: 0,
max: 200,
position: 'right',
offset: 0,
axisLine: {
lineStyle: {
color: yData[1].color
}
},
axisLabel: {
textStyle: {
fontSize: 10,
color: '#fff'
},
formatter: '{value} μg/m³'
}
},
{
type: 'value',
name: '噪声',
min: 0,
max: 100,
interval: 20,
position: 'left',
axisLine: {
lineStyle: {
color: yData[2].color
}
},
nameTextStyle: {
color: '#fff',
fontSize: 12
},
axisLabel: {
textStyle: {
fontSize: 10,
color: '#fff'
},
formatter: '{value} dB'
}
}
],
series: [
{
name: 'PM2.5',
type: 'bar',
data: yData[0].value
},
{
name: 'PM10',
type: 'bar',
yAxisIndex: 1,
data: yData[1].value
},
{
name: '噪声',
type: 'line',
yAxisIndex: 2,
data: yData[2].value
}
]
}
},
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>