vue 可视化大屏 项目开发中,需要折线图中显示最大值的label,这里我们通过lodash来获取数组的最大值,然后判断,如果当前值和最大值一致就显示对应区域的label
演示实例
组件代码
<template>
<div class="echarts1" ref="echarts">
</div>
</template>
<script>
import * as echarts from "echarts"
import lodash from "lodash";
export default {
name: 'echarts1',
components: {},
props: {
id: {
type: String,
default() {
return ''
}
}
},
data() {
return {
status: '',
active: false,
xData: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
yData1: [10, 20, 30, 40, 20, 30, 20, 10, 20, 30, 38, 20]
}
},
watch: {},
mounted() {
this.drawLine()
},
methods: {
drawLine() {
// 基于准备好的dom,初始化echarts实例
window.addEventListener('resize', this.drawLine)
let myChart = echarts.init(this.$refs.echarts)
var max = lodash.max(this.yData1)
console.log(max)
var yData = []
this.yData1.forEach((type) => {
yData.push({
value: type,
label: {
normal: {
show: max == type,
position: 'top',
color: '#fff',
align: 'center',
fontWeight: 500,
fontSize: 12
}
},
})
});
console.log(yData)
var option = {
grid: {
top: '30px',
bottom: '20px',
left: '20px',
right: '20px',
containLabel: true
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow',
shadowStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(255, 174, 0, 0.1)'
}, {
offset: 1,
color: 'rgba(255, 174, 0, 0.5)'
}], false),
}
}
},
xAxis: [
{
data: this.xData,
axisLabel: {
margin: 10,
color: 'rgba(202, 215, 245, 1)',
textStyle: {
fontSize: 14
},
},
axisLine: {
lineStyle: {
color: 'rgba(49, 119, 214, 1)',
width: 1
}
},
splitLine: {
show: false,
lineStyle: {
color: 'rgba(39, 76, 129, 0.26)',
width: 1,
}
},
axisTick: {
show: false
},
}],
yAxis: [
{
axisLabel: {
color: 'rgba(202, 215, 245, 1)',
textStyle: {
fontSize: 14
},
},
axisLine: {
lineStyle: {
color: 'rgba(49, 119, 214, 1)',
}
},
axisTick: {
show: false
},
splitLine: {
show: true,
lineStyle: {
color: 'rgba(39, 76, 129, 0.26)',
width: 1,
}
}
}],
series: [
{
name: '目标达成率',
type: 'line',
data: yData,
smooth: true,
symbol: 'circle', //数值点设定为实心点
// symbolSize: 0, // 折线的点的大小
itemStyle: {
normal: {
color: '#00fbd3', //点的颜色
lineStyle: {
color: '#00fbd3', //线的颜色
width: 1, // 折线图线条粗细设置
},
},
},
areaStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(0, 240, 203, 0.5)'
},
{
offset: 1,
color: 'rgba(0, 240, 203, 0)'
}
], false),
}
},
}]
}
myChart.clear()
myChart.resize()
myChart.setOption(option)
},
}
}
</script>
<style lang="scss" scoped>
.echarts1 {
position: relative;
width: 100%;
height: 100%;
}
</style>