yudao-dev/src/views/energy/analysis/yoyAnalysis/components/lineChart.vue

102 lines
2.5 KiB
Vue
Raw Normal View History

2023-09-05 15:45:59 +08:00
<template>
2024-03-21 08:27:35 +08:00
<div id="analysischartLine" style="width: 100%;height: 100%;"></div>
2023-09-05 15:45:59 +08:00
</template>
<script>
import * as echarts from 'echarts'
import resize from '@/utils/chartMixins/resize'
export default {
name: "LineChart",
mixins: [resize],
data() {
return {
chartDom: '',
chart: '',
chartHeight: this.tableHeight(214) - 70
}
},
props: {
chartData: {
type: Array,
required: true,
default: () => {
return []
}
}
},
watch: {
chartData: function () {
this.getChart()
}
},
mounted() {
window.addEventListener('resize', () => {
this.chartHeight = this.tableHeight(214) - 70
})
},
methods: {
getChart() {
if (
this.chart !== null &&
this.chart !== '' &&
this.chart !== undefined
) {
this.chart.dispose() // 页面多次刷新会出现警告Dom已经初始化了一个实例这是销毁实例
}
this.chartDom = document.getElementById('analysischartLine')
this.chart = echarts.init(this.chartDom)
if (this.chartData.length === 0) {
return false
}
let xData = []
let arr = this.chartData[0].type
let keys = Object.keys(this.chartData[0])
let yData = []
for (let j = 0; j < arr.length; j++) {
for (let k = 0; k < keys.length; k++) {
2024-03-21 08:27:35 +08:00
if (keys[k].indexOf(arr[j] + '_上年同期') > -1 || keys[k].indexOf(arr[j] + '_能源消耗') > -1) {
2023-09-05 15:45:59 +08:00
let obj = {
name: '',
type: 'line',
data: []
}
obj.name = keys[k]
yData.push(obj)
}
}
}
for (let i = 0; i < this.chartData.length; i++) {
xData.push(this.chartData[i].time)
for (let p = 0; p < yData.length; p++) {
2024-03-21 08:27:35 +08:00
yData[p].data.push(this.chartData[i][yData[p].name])
2023-09-05 15:45:59 +08:00
}
}
var option = {
2024-03-21 08:27:35 +08:00
color: ['#FFDC94', '#8EF0AB', '#63BDFF', '#288AFF', '#7164FF', '#FF6860', '#FF9747', '#B0EB42', '#D680FF', '#0043D2'],
2023-09-05 15:45:59 +08:00
legend: {
2023-10-11 14:30:18 +08:00
data: keys,
2024-03-21 08:27:35 +08:00
right: '1%'
2023-09-05 15:45:59 +08:00
},
tooltip: {
trigger: 'axis'
},
grid: {
2023-09-27 09:33:28 +08:00
left: '1%',
right: '1%',
2023-09-05 15:45:59 +08:00
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'category',
data: xData
},
yAxis: {
type: 'value'
},
series: yData
};
option && this.chart.setOption(option);
}
}
}
</script>