2023-09-05 15:45:59 +08:00
|
|
|
|
<template>
|
2024-04-02 16:54:36 +08:00
|
|
|
|
<div
|
|
|
|
|
id="analysischartLine"
|
|
|
|
|
style="width: 100%"
|
|
|
|
|
:style="{ height: chartHeight + 'px' }"></div>
|
2023-09-05 15:45:59 +08:00
|
|
|
|
</template>
|
|
|
|
|
<script>
|
2024-04-02 16:54:36 +08:00
|
|
|
|
import * as echarts from 'echarts';
|
|
|
|
|
import resize from '@/utils/chartMixins/resize';
|
2023-09-05 15:45:59 +08:00
|
|
|
|
export default {
|
2024-04-02 16:54:36 +08:00
|
|
|
|
name: 'LineChart',
|
|
|
|
|
mixins: [resize],
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
chartDom: '',
|
|
|
|
|
chart: '',
|
|
|
|
|
chartHeight: this.tableHeight(214) - 100,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
chartData: {
|
|
|
|
|
type: Array,
|
|
|
|
|
required: true,
|
|
|
|
|
default: () => {
|
|
|
|
|
return [];
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
timeDim: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
|
let xData = [];
|
|
|
|
|
let yData = [];
|
|
|
|
|
for (let i = 0; i < this.chartData.length; i++) {
|
|
|
|
|
let time = '';
|
|
|
|
|
if (this.timeDim === '3') {
|
|
|
|
|
let year = this.chartData[i].time.slice(0, 4);
|
|
|
|
|
let weak = this.chartData[i].time.slice(4, 6);
|
|
|
|
|
time = year + ' 第 ' + weak + ' 周';
|
|
|
|
|
} else {
|
|
|
|
|
time = this.chartData[i].time;
|
|
|
|
|
}
|
|
|
|
|
xData.push(time);
|
|
|
|
|
yData.push(this.chartData[i].useNum);
|
|
|
|
|
}
|
2023-09-05 15:45:59 +08:00
|
|
|
|
|
2024-04-02 16:54:36 +08:00
|
|
|
|
var option = {
|
|
|
|
|
color: ['#288AFF'],
|
|
|
|
|
// tooltip: {
|
|
|
|
|
// trigger: 'axis'
|
|
|
|
|
// },
|
|
|
|
|
grid: {
|
|
|
|
|
left: '4%',
|
|
|
|
|
right: '1%',
|
|
|
|
|
bottom: '1%',
|
|
|
|
|
containLabel: true,
|
|
|
|
|
},
|
|
|
|
|
xAxis: {
|
|
|
|
|
type: 'category',
|
|
|
|
|
data: xData,
|
|
|
|
|
axisLabel: {
|
|
|
|
|
rotate: '45',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
yAxis: {
|
|
|
|
|
type: 'value',
|
|
|
|
|
},
|
|
|
|
|
series: [
|
|
|
|
|
{
|
|
|
|
|
data: yData,
|
|
|
|
|
type: 'line',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
2023-09-05 15:45:59 +08:00
|
|
|
|
|
2024-04-02 16:54:36 +08:00
|
|
|
|
option && this.chart.setOption(option);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
2023-09-05 15:45:59 +08:00
|
|
|
|
</script>
|