You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
4.9 KiB
189 lines
4.9 KiB
<template>
|
|
<div>
|
|
<div :id="idName" ref="linesRef" :style="{ height: height, width: width }" />
|
|
</div>
|
|
</template>
|
|
<script>
|
|
import moment from 'moment' // 导入模块
|
|
export default {
|
|
props: {
|
|
idName: { type: String, default: 'chart' },
|
|
width: { type: String, default: '100%' },
|
|
height: { type: String, default: '200px' },
|
|
chartData: { type: Array, default: () => [] },
|
|
desc: { type: String, default: '' }
|
|
},
|
|
data() {
|
|
return {
|
|
date: [],
|
|
typeList: {
|
|
OD: [],
|
|
OS: [],
|
|
typeNull: []
|
|
},
|
|
legendData: [],
|
|
seriesData: [],
|
|
disabled: false
|
|
|
|
}
|
|
},
|
|
watch: {
|
|
chartData: {
|
|
handler(val, olVal) {
|
|
this.initFun()
|
|
},
|
|
deep: true
|
|
}
|
|
},
|
|
mounted() {
|
|
this.initFun()
|
|
},
|
|
methods: {
|
|
initFun() {
|
|
if (this.chartData && this.chartData.length > 0) {
|
|
this.disabled = false
|
|
this.chartData.forEach(item => {
|
|
this.date.push(moment(item.date).format('l'))
|
|
if (item.type === 'OD') {
|
|
this.typeList.OD.push([item.date, item.value])
|
|
this.legendData[0] = 'OD'
|
|
}
|
|
if (item.type === 'OS') {
|
|
this.typeList.OS.push([item.date, item.value])
|
|
this.legendData[1] = 'OS'
|
|
}
|
|
if (item.type === null) {
|
|
this.typeList.typeNull.push([item.date, item.value])
|
|
this.legendData = []
|
|
}
|
|
})
|
|
} else {
|
|
this.disabled = true
|
|
}
|
|
this.$nextTick(() => {
|
|
this.visionFun()
|
|
})
|
|
},
|
|
visionFun() {
|
|
// 基于准备好的dom,初始化echarts实例
|
|
const echartsLine = this.$echarts.init(document.getElementById(this.idName))
|
|
const colors = ['#4462FF', '#0DB760', '#000000']
|
|
this.seriesData = []
|
|
Object.keys(this.typeList).forEach((item, index) => {
|
|
if (this.typeList[item].length > 0) {
|
|
this.seriesData.push({
|
|
name: item,
|
|
type: 'line',
|
|
data: this.typeList[item],
|
|
smooth: true,
|
|
itemStyle: {
|
|
color: colors[index]
|
|
},
|
|
symbolSize: 10
|
|
})
|
|
}
|
|
})
|
|
|
|
// 基于准备好的dom,初始化echarts实例
|
|
echartsLine.setOption({
|
|
title: {
|
|
text: this.desc,
|
|
textStyle: {
|
|
'color': '#fff'
|
|
},
|
|
left: 10
|
|
},
|
|
tooltip: {
|
|
trigger: 'axis',
|
|
backgroundColor: 'rgba(255,255,255,0.8)',
|
|
textStyle: {
|
|
color: 'black' // 设置文字颜色
|
|
},
|
|
formatter(params) {
|
|
const thisDate = moment(params[0].axisValue).format('YYYY/M/D')
|
|
let result = `${thisDate} <br/>`
|
|
params.forEach(item => {
|
|
if (item.data.length) {
|
|
item.seriesName = item.seriesName === 'typeNull' ? '' : item.seriesName
|
|
result += `${item.marker}${item.seriesName}: ${item.data[1]}<br/>`
|
|
} else {
|
|
result += `${item.marker}${item.seriesName}: - <br/>`
|
|
}
|
|
})
|
|
return result
|
|
}
|
|
},
|
|
legend: {
|
|
data: this.legendData,
|
|
icon: 'pin',
|
|
textStyle: {
|
|
color: '#ffffff'
|
|
},
|
|
right: 30
|
|
},
|
|
dataZoom: [
|
|
{
|
|
type: 'inside',
|
|
disabled: this.disabled,
|
|
realtime: true,
|
|
start: 0,
|
|
end: 100
|
|
}
|
|
],
|
|
grid: {
|
|
top: 45,
|
|
left: 50,
|
|
right: 50,
|
|
bottom: 30,
|
|
containLabel: false
|
|
},
|
|
xAxis: {
|
|
type: 'time',
|
|
boundaryGap: false,
|
|
axisLabel: {
|
|
show: true,
|
|
textStyle: {
|
|
color: '#ffffff'
|
|
},
|
|
formatter: function(value, index) {
|
|
const showD = moment(value).format('YYYY/M/D')
|
|
return showD
|
|
}
|
|
},
|
|
// 显示网格线
|
|
splitLine: {
|
|
show: true
|
|
}
|
|
},
|
|
|
|
yAxis: {
|
|
type: 'value',
|
|
axisLabel: {
|
|
show: true,
|
|
textStyle: {
|
|
color: '#ffffff'
|
|
}
|
|
},
|
|
axisLine: {
|
|
lineStyle: {
|
|
color: '#ffffff'
|
|
}
|
|
}
|
|
},
|
|
series: this.seriesData
|
|
})
|
|
// 随窗口变化
|
|
window.addEventListener('resize', () => { echartsLine.resize() })
|
|
// 随外层div的大小变化自适应
|
|
this.$erd.listenTo(this.$refs.linesRef, () => {
|
|
this.$nextTick(() => {
|
|
echartsLine.resize()
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
</script>
|
|
<style style="scss" scoped>
|
|
</style>
|