基于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 class="rect" :style="{ backgroundColor: item.color }" />
<span>{{ item.name }}span>
li>
ul>
<div id="chart10" class="chart" />
div>
template>
<script>
import echarts from '@/utils/echarts'
export default {
name: 'Index',
data () {
return {
chart: null,
yData: [
{ name: '综合能耗', value: [20, 80, 30, 40, 60, 40], color: '#0091FF', selected: true },
{ name: 'GDP能耗', value: [20, 60, 20, 100, 35, 90], color: '#45DE81', selected: true }
],
xData: ['00:05', '00:10', '00:15', '00:20', '00:25', '00:30']
}
},
mounted() {
this.createChartHandler()
},
methods: {
// 创建图表
createChartHandler () {
this.chart = this.$echarts.init(document.getElementById('chart10'))
this.chart.setOption(this.getChartOption(this.yData, this.xData))
window.addEventListener('resize', () => {
setTimeout(() => {
this.chart.resize()
})
})
},
// 获取图表配置项
getChartOption (yData, xData) {
const hexToRgba = (hex, opacity) => {
let rgbaColor = ''
const reg = /^#[\da-f]{6}$/i
if (reg.test(hex)) {
rgbaColor = `rgba(${parseInt('0x' + hex.slice(1, 3))},${parseInt(
'0x' + hex.slice(3, 5)
)},${parseInt('0x' + hex.slice(5, 7))},${opacity})`
}
return rgbaColor
}
return {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
},
extraCssText:
'fontFamily: PingFangReg;color:#fff;background:rgba(0,0,0,0.6000);border: unset;border-radius: 2px;z-index:99',
formatter: (e) => {
const div = document.createElement('div')
div.innerHTML = `
近期接能
${e
.map((i) => {
return ` ${i.color};\"> ${i.seriesName}:${i.value}
`
})
.join('')}
`
return div
}
},
legend: {
show: false,
data: yData.map((i) => i.name)
},
grid: {
top: '5%',
left: '10%',
right: '5%',
bottom: '15%'
},
xAxis: [
{
type: 'category',
data: xData,
boundaryGap: false,
axisLabel: {
interval: 0,
fontFamily: 'PingFangReg',
lineHeight: 17,
fontSize: 14,
color: 'rgba(255,255,255,0.6500)'
},
axisTick: {
show: true,
alignWithLabel: true,
lineStyle: {
color: 'rgba(255,255,255,0.1500)'
}
},
axisLine: {
show: true,
lineStyle: {
color: 'rgba(255,255,255,0.1500)'
}
}
}
],
yAxis: [
{
type: 'value',
axisLabel: {
fontFamily: 'PingFangReg',
fontSize: 14,
color: 'rgba(255,255,255,0.6500)'
},
axisLine: {
show: false,
lineStyle: {
color: '#24DCF0'
}
},
splitLine: {
show: true,
lineStyle: {
color: ' rgba(255,255,255,0.1500)'
}
}
}
],
series: yData.map((i) => {
return {
name: i.name,
type: 'line',
symbol: 'none',
smooth: true,
data: i.value,
itemStyle: {
color: i.color
},
lineStyle: {
shadowColor: i.color, // 透明的颜色
shadowOffsetX: 0,
shadowOffsetY: 0,
opacity: 1, // 透明度
shadowBlur: 20, // 阴影大小
type: 'solid', // 实线
width: 3
},
areaStyle: {
normal: {
color: new echarts.graphic.LinearGradient(
0,
0,
0,
1,
[{
offset: 0,
color: hexToRgba(i.color, 0.3)
},
{
offset: 1,
color: hexToRgba(i.color, 0.01)
}
],
false
)
}
}
}
})
}
},
// 鼠标移入图例触发
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>