yudao-dev/src/views/energy/analysis/contrastAnalysis/components/barChart.vue

148 lines
3.9 KiB
Vue
Raw Normal View History

2023-09-05 15:45:59 +08:00
<template>
<div
id="analysischartBar"
style="width: 100%"
:style="{ height: chartHeight + 'px' }"
></div>
</template>
<script>
import * as echarts from 'echarts'
import resize from '@/utils/chartMixins/resize'
export default {
name: "BarChart",
mixins: [resize],
data() {
return {
chartDom: '',
chart: '',
chartHeight: this.tableHeight(214) - 70
}
},
props: {
chartData: {
type: Array,
required: true,
default: () => {
return []
}
2023-09-27 09:33:28 +08:00
},
timeDim: {
type: String,
default: ''
2023-09-05 15:45:59 +08:00
}
},
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('analysischartBar')
this.chart = echarts.init(this.chartDom)
2023-09-08 16:01:54 +08:00
let tempArr = []
2023-09-05 15:45:59 +08:00
let xData = []
let yData = []
2023-09-08 16:01:54 +08:00
let legendData = []
if (this.chartData.length === 0) {
return false
} else {
tempArr = this.chartData[0].trendRespVOList
}
for (let k = 0; k < tempArr.length; k++) {
2023-09-27 09:33:28 +08:00
let time = ''
if (this.timeDim === '3') {
let year = tempArr[k].time.slice(0,4)
let weak = tempArr[k].time.slice(4,6)
time = year+' 第 '+weak+' 周'
} else {
time = tempArr[k].time
}
xData.push(time)
2023-09-08 16:01:54 +08:00
}
2023-09-05 15:45:59 +08:00
for (let i = 0; i < this.chartData.length; i++) {
2023-09-08 16:01:54 +08:00
let obj = {
name: this.chartData[i].objName + this.chartData[i].objCode,
type: 'bar',
2023-09-27 09:33:28 +08:00
barMaxWidth: 20,
label: {
show: true,
position: 'top'
},
2023-09-08 16:01:54 +08:00
data: []
}
legendData.push(this.chartData[i].objName + this.chartData[i].objCode)
let temp = this.chartData[i].trendRespVOList
for (let j = 0; j < temp.length; j++) {
2023-09-27 09:33:28 +08:00
let num = temp[j].useNum ? temp[j].useNum : ''
2023-09-08 16:01:54 +08:00
obj.data.push(num)
}
yData.push(obj)
2023-09-05 15:45:59 +08:00
}
var option = {
2023-09-27 09:33:28 +08:00
color:['#FFDC94','#8EF0AB','#63BDFF','#288AFF','#7164FF'],
2023-09-08 16:01:54 +08:00
tooltip: {
2023-09-27 09:33:28 +08:00
trigger: 'axis',
axisPointer: {
type: 'shadow'
2023-10-11 14:30:18 +08:00
},
formatter: function(params) {
return (
params[0].axisValue +
`<br>` +
params.map((item) => {
let str = `<span style="display:inline-block;width:8px;height:8px;margin: 0 8px 0 -3px;border-radius:2px;background-color:${item.color};"></span>`
let seriesNameStr = `<span style="display:inline-block;">${item.seriesName}</span>`
let value = item.value ? item.value : '-'
let valueStr = `<span style="display:inline-block;margin-left:10px;color:rgba(0,0,0,0.45);">${value}</span>`
return `<span style="display:flex; justify-content:space-between; margin-bottom: 2px">
<span>${str}${seriesNameStr}</span>
<span>${valueStr}</span>
</span>`
}).join(``)
)
2023-09-27 09:33:28 +08:00
}
},
grid: {
left: '4%',
right: '1%',
bottom: '1%',
containLabel: true
2023-09-08 16:01:54 +08:00
},
legend: {
2023-10-11 14:30:18 +08:00
data: legendData,
right: '1%',
icon: 'rect',
itemHeight: 8,
itemWidth: 8
2023-09-08 16:01:54 +08:00
},
2023-09-05 15:45:59 +08:00
xAxis: {
type: 'category',
2023-09-27 09:33:28 +08:00
data: xData,
axisLabel: {
rotate: "45"
}
2023-09-05 15:45:59 +08:00
},
yAxis: {
type: 'value'
},
2023-09-08 16:01:54 +08:00
series: yData
2023-09-05 15:45:59 +08:00
};
option && this.chart.setOption(option);
}
}
}
</script>