Files
tk-mini-program-PC/src/components/EChartsComponent.vue

126 lines
4.9 KiB
Vue
Raw Normal View History

2025-04-07 13:14:56 +08:00
<template>
<div ref="chart" style="width: 500px; height: 300px;"></div>
</template>
<script>
import * as echarts from 'echarts';
import { tkhostdata, dicts, tkhostdetail } from '@/api/account';
export default {
name: 'EChartsComponent',
props: {
title: {
type: String,
required: true
},
id: {
type: String,
required: true
},
dataType: {
type: String,
required: true
}
},
2025-04-09 21:07:15 +08:00
data() {
return {
seriesData: [],
num: 0
}
},
2025-04-07 13:14:56 +08:00
mounted() {
2025-04-09 21:07:15 +08:00
2025-04-07 13:14:56 +08:00
this.getTkhostdetail();
2025-04-09 21:07:15 +08:00
2025-04-07 13:14:56 +08:00
},
methods: {
initChart() {
2025-04-09 21:07:15 +08:00
if (!this.$refs.chart) {
console.error('DOM element not found');
return;
}
2025-04-07 13:14:56 +08:00
const myChart = echarts.init(this.$refs.chart);
const option = {
title: {
text: this.title
},
tooltip: {},
legend: {
2025-04-09 21:07:15 +08:00
data: [this.title]
2025-04-07 13:14:56 +08:00
},
xAxis: {
2025-04-09 21:07:15 +08:00
data: [this.getCurrentDate()[0].slice(4), this.getCurrentDate()[1].slice(4), this.getCurrentDate()[2].slice(4), this.getCurrentDate()[3].slice(4), this.getCurrentDate()[4].slice(4), this.getCurrentDate()[5].slice(4), this.getCurrentDate()[6].slice(4)]
2025-04-07 13:14:56 +08:00
},
yAxis: {},
series: [
{
2025-04-09 21:07:15 +08:00
name: this.title,
2025-04-07 13:14:56 +08:00
type: 'line',
2025-04-09 21:07:15 +08:00
data: [this.seriesData[0], this.seriesData[1], this.seriesData[2], this.seriesData[3], this.seriesData[4], this.seriesData[5], this.seriesData[6]],
2025-04-07 13:14:56 +08:00
smooth: true
}
]
};
myChart.setOption(option);
},
getTkhostdetail() {
tkhostdetail({
hostId: this.id,
dataType: this.dataType,
searchTimeStart: this.getCurrentDate()[0],
searchTimeEnd: this.getCurrentDate()[6]
}).then(res => {
2025-04-09 21:07:15 +08:00
// console.log("返回数据", res[0][this.getCurrentDate()[2]])
//echarts 数据初始化
this.seriesData = [
res[0][this.getCurrentDate()[0]] == null ? 0 : Number(res[0][this.getCurrentDate()[0]][this.dataType]),
res[0][this.getCurrentDate()[1]] == null ? 0 : Number(res[0][this.getCurrentDate()[1]][this.dataType]),
res[0][this.getCurrentDate()[2]] == null ? 0 : Number(res[0][this.getCurrentDate()[2]][this.dataType]),
res[0][this.getCurrentDate()[3]] == null ? 0 : Number(res[0][this.getCurrentDate()[3]][this.dataType]),
res[0][this.getCurrentDate()[4]] == null ? 0 : Number(res[0][this.getCurrentDate()[4]][this.dataType]),
res[0][this.getCurrentDate()[5]] == null ? 0 : Number(res[0][this.getCurrentDate()[5]][this.dataType]),
res[0][this.getCurrentDate()[6]] == null ? 0 : Number(res[0][this.getCurrentDate()[6]][this.dataType]),
]
// this.seriesData = {
// [this.getCurrentDate()[0]]: res[0][this.getCurrentDate()[0]] == null ? 0 : res[0][this.getCurrentDate()[0]][this.dataType],
// [this.getCurrentDate()[1]]: res[0][this.getCurrentDate()[1]] == null ? 0 : res[0][this.getCurrentDate()[1]][this.dataType],
// [this.getCurrentDate()[2]]: res[0][this.getCurrentDate()[2]] == null ? 0 : res[0][this.getCurrentDate()[2]][this.dataType],
// [this.getCurrentDate()[3]]: res[0][this.getCurrentDate()[3]] == null ? 0 : res[0][this.getCurrentDate()[3]][this.dataType],
// [this.getCurrentDate()[4]]: res[0][this.getCurrentDate()[4]] == null ? 0 : res[0][this.getCurrentDate()[4]][this.dataType],
// [this.getCurrentDate()[5]]: res[0][this.getCurrentDate()[5]] == null ? 0 : res[0][this.getCurrentDate()[5]][this.dataType],
// [this.getCurrentDate()[6]]: res[0][this.getCurrentDate()[6]] == null ? 0 : res[0][this.getCurrentDate()[6]][this.dataType],
// }
this.initChart();
this.num++
console.log("返回数据", this.seriesData)
console.log("返回数据", this.num)
2025-04-07 13:14:56 +08:00
})
},
getCurrentDate() {
const dates = [];
const today = new Date();
for (let i = 6; i >= 0; i--) {
const date = new Date(today);
date.setDate(today.getDate() - i);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
dates.push(`${year}${month}${day}`);
}
return dates;
}
}
};
</script>