折线图dashboard\big\LineIndex.vue

折线图dashboard\big\lineChart.vue
后续:lineChart.vue里面暂时用生成的数据可视化后面可以改成向后端请求数据
这里折线图新加了一个页面,所以影响了前端路由
顺便完善了一下index.vue
This commit is contained in:
Huiting Pei 2025-05-06 16:14:44 +08:00
parent b357bc793c
commit 30520ffafa
5 changed files with 446 additions and 19 deletions

View File

@ -125,4 +125,7 @@ src/
│ └── routerView/ #路由视图
```
重要的文件
G:\Projects\VueProject\SamATV\src\views\common
src/views/common/dashboard/big/index.vue dashboard仪表盘
前端路由G:\Projects\VueProject\SamATV\src\router\route.ts

View File

@ -19,7 +19,7 @@ export const dynamicRoutes: Array<RouteRecordRaw> = [
path: '/home', name: 'home',
component: () => import('/@/views/common/dashboard/atv/index.vue'),
meta: {
title: 'message.router.home',
title: '首页',
isLink: '', isHide: false, isKeepAlive: true,
isAffix: true, isIframe: false,
roles: ['admin', 'common'], icon: 'fa fa-home',
@ -33,7 +33,16 @@ export const dynamicRoutes: Array<RouteRecordRaw> = [
path: '/big', name: 'grafana',
component: () => import('/@/views/common/dashboard/big/index.vue'),
meta: {
title: 'message.router.bigscreen', isIframe: false, isLink: '/big',
title: '数据监测', isIframe: false, isLink: '/big',
isHide: false, isKeepAlive: false, isAffix: false,
roles: ['admin'], icon: 'fa fa-window-restore'
},
},
{
path: '/line', name: 'line',
component: () => import('/@/views/common/dashboard/big/LineIndex.vue'),
meta: {
title: '具体传感器', isIframe: false, isLink: '/line',
isHide: false, isKeepAlive: false, isAffix: false,
roles: ['admin'], icon: 'fa fa-window-restore'
},
@ -42,7 +51,7 @@ export const dynamicRoutes: Array<RouteRecordRaw> = [
path: '/set_config', name: 'config',
component: () => import('/@/views/common/dashboard/config_page/index.vue'),
meta: {
title: 'message.router.set_config', isIframe: false, isLink: '/set_config',
title: '配置管理', isIframe: false, isLink: '/set_config',
isHide: false, isKeepAlive: false, isAffix: false,
roles: ['admin'], icon: 'fa fa-window-restore'
},

View File

@ -0,0 +1,204 @@
<template>
<div className="home">
<!-- 这个单选框不知道为啥一直有问题-->
<!-- <div class="mb-2 ml-4">-->
<!-- <el-radio-group v-model="picRadio">-->
<!-- <el-radio value="1" size="large">温度监测图</el-radio>-->
<!-- <el-radio value="2" size="large">湿度监测图</el-radio>-->
<!-- <el-radio value="3" size="large">应力监测图</el-radio>-->
<!-- </el-radio-group>-->
<!-- </div>-->
<!-- 查询时间范围和按钮 -->
<div className="form-container">
<el-form-item label="可视化类型:" className="form-item">
<el-select
v-model="typeValue"
placeholder="Select"
size="large"
style="width: 130px"
>
<el-option
v-for="item in typeOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
<el-form-item label="查询时间范围:" className="form-item">
<el-date-picker
v-model="timeValue"
type="datetimerange"
range-separator="到"
start-placeholder="开始时间"
end-placeholder="结束时间"
@change="handleDateChange"
style="width: 380px"
/>
</el-form-item>
<el-form-item label="光栅编号范围:" className="form-item">
<el-input-number
v-model="gratingIdMin"
:min="1"
:max="1399"
@change="updateSeries"
placeholder="最小值"
style="width: 150px;"
/>
<span style="margin: 0 5px;"> - </span>
<el-input-number
v-model="gratingIdMax"
:min="1"
:max="1400"
@change="updateSeries"
placeholder="最大值"
style="width: 150px;"
/>
<el-cascader
v-model="value"
:options="options"
style="width: 150px; margin-left: 10px;"
placeholder="其他选项"
@change="handleOtherChange"
/>
</el-form-item>
</div>
<div className="heatmap-container"></div>
<line-chart
:gratingIdArray="gratingIdArray"
:timeRange="timeValue"
:time-granularity="value[1]"
:picType="typeValue"
/>
<!-- 折线图-->
</div>
</template>
<script>
import axios from "axios";
import LineChart from "/@/views/common/dashboard/big/lineChart.vue"; // 线
export default {
components: { LineChart },
data() {
return {
typeValue: "temperature", //
typeOptions: [
{
value: "temperature",
label: "温度监测图",
},
{
value: "humidity",
label: "湿度监测图",
},
{
value: "stress",
label: "应力监测图",
}
],
// picType: "temperature", //
timeValue: [], //
gratingIdMin: 1, //
gratingIdMax: 12, //
gratingIdArray: [], //
value: [], //
options: [
{
value: "timeGranularity",
label: "时间粒度",
children: [
{
value: "hour",
label: "每小时",
},
{
value: "quarterHour",
label: "每15分钟",
},
{
value: "minute",
label: "每分钟",
}
],
}
]
};
},
methods: {
handleDateChange() {
console.log("选中的时间范围:", this.timeValue);
},
//
updateSeries() {
if (this.gratingIdMax - this.gratingIdMin < 0) {
this.$message.error("请输入正确的光栅编号范围!");
}
else if (this.gratingIdMax - this.gratingIdMin >=20) {
this.$message.error( "请选择20个以内的光栅");
}
else{
//
this.gratingIdArray = Array.from(
{ length: this.gratingIdMax - this.gratingIdMin + 1 },
(_, i) => this.gratingIdMin + i
)
}
},
// ,beforeMount()
setDefaultXYRange() {
// 1
const now = new Date();
const oneDayAgo = new Date();
oneDayAgo.setDate(now.getDate() - 1);
this.timeValue = [oneDayAgo, now];
// 1 12
this.gratingIdMin = 1;
this.gratingIdMax = 12;
//
this.gratingIdArray = Array.from(
{ length: this.gratingIdMax - this.gratingIdMin + 1 },
(_, i) => this.gratingIdMin + i
)
},
handleOtherChange(value) {
console.log("级联选择器的值:", value);
console.log("时间粒度:", value[1]);
}
},
mounted() {
},
beforeMount() {
this.setDefaultXYRange();
}
};
</script>
<style scoped>
.home {
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
.form-container {
display: flex;
align-items: center; /* 垂直居中对齐 */
flex-wrap: wrap; /* 当屏幕宽度不足时自动换行 */
margin-top: 10px; /* 与顶部的间距 */
margin-bottom: 10px; /* 和 iframe 的间距 */
margin-left: 10px;
width: 100%;
justify-content: flex-start;
}
.form-item {
margin-right: 10px; /* 按钮和输入框之间的间距 */
}
</style>

View File

@ -61,7 +61,6 @@
placeholder="其他选项"
@change="handleOtherChange"
/>
<!-- <el-button type="primary" @click="updateXYRange" style="margin: 0 30px;">查询</el-button>-->
</el-form-item>
</div>
<!-- 热力图的容器 -->
@ -72,12 +71,14 @@
:picType="typeValue"
/>
<div class="heatmap-container"></div>
<!-- 折线图-->
</div>
</template>
<script>
import axios from "axios";
import Heatmap from "/@/views/common/dashboard/big/heatmap.vue"; //
import LineChart from "/@/views/common/dashboard/big/lineChart.vue"; // 线
export default {
components: { Heatmap },
@ -135,20 +136,6 @@ export default {
}
},
//
updateXYRange() {
//
if (this.timeValue.length !== 2) {
this.$message.error("请选择完整的时间范围!");
return;
}
if (this.gratingIdMax - this.gratingIdMin < 9) {
this.$message.error("至少选择10个光栅");
return;
}
const from = new Date(this.timeValue[0]).getTime(); //
const to = new Date(this.timeValue[1]).getTime(); //
},
setDefaultXYRange() {
// 1
const now = new Date();
@ -160,7 +147,6 @@ export default {
this.gratingIdMin = 1;
this.gratingIdMax = 12;
//
this.updateXYRange();
},
handleOtherChange(value) {
console.log("级联选择器的值:", value);

View File

@ -0,0 +1,225 @@
<template>
<div ref="chartContainer" style="width: 100%; height: 800px;"></div>
</template>
<script>
import * as echarts from 'echarts';
import { markRaw } from 'vue'
export default {
name: 'LineChart',
props: {
timeRange: {
type: Array,
default: () => []
},//
gratingIdArray: {
type: Array,
default: () => []
},//
timeGranularity: {
type: String,
default: 'hour'
},//
picType: {
type: String,
default: 'temperature'
}//
},
data() {
return {
chart: null,
seriesData: [] // 线
};
},
mounted() {
this.initChart();
},
watch: {
// timeRange
timeRange: {
handler() {
this.initChart();
},
deep: true
},
// gratingIdArray
gratingIdArray: {
handler() {
this.initChart();
},
deep: true
},
// timeGranularity
timeGranularity() {
this.initChart();
},
// picType
picType() {
this.initChart();
}
},
methods: {
initChart() {
// ECharts
if (this.chart) {
this.chart.dispose();
}
console.log("picType:", this.picType);
// DOM
const chartDom = this.$refs.chartContainer;
// ECharts
this.chart = markRaw(echarts.init(chartDom));
console.log('timeGranularity:', this.timeGranularity);
//
let timeData = [];
if (this.timeGranularity === 'quarterHour') {
timeData = this.generateQuarterHourData();
//console.log('timeData:', timeData);
} else if (this.timeGranularity === 'minute') {
timeData = this.generateMinuteData();
//console.log('timeData:', timeData);
} else {
timeData = this.generateHourData();
//console.log('timeData:', timeData);
}
//
this.seriesData = this.getGatingData(this.gratingIdArray.length, timeData.length);
// 线
const option = {
tooltip: {
trigger: 'axis',
formatter: function (params) {
// params
let tip = `<div style="font-size: 14px;">${params[0].name}</div>`; // x
tip += params.map(item => {
return `<div style="color: ${item.color}; margin: 4px 0;">${item.seriesName}: ${item.value}</div>`;
}).join('');
return tip;
}
},
legend: {
data: this.$props.gratingIdArray.map(id => `光栅 ${id}`)
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
toolbox: {
feature: {
saveAsImage: {}
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: timeData
},
yAxis: {
type: 'value'
},
series: this.seriesData
};
console.log('seriesData:', this.seriesData);
// 使
// console.log('option:', option)
// console.log('Legend Data:', option.legend.data);
// console.log('Series Names:', option.series.map(s => s.name));
this.chart.setOption(option);
},
//
generateHourData() {
const [start, end] = this.timeRange;
const startTime = new Date(start);
const endTime = new Date(end);
const timeData = [];
const currentTime = new Date(startTime);
while (currentTime < endTime) {
timeData.push(currentTime.toLocaleString());
currentTime.setHours(currentTime.getHours() + 1);
}
return timeData;
},
// 15
generateQuarterHourData() {
const [start, end] = this.timeRange;
const startTime = new Date(start);
const endTime = new Date(end);
const timeData = [];
const currentTime = new Date(startTime);
while (currentTime < endTime) {
timeData.push(currentTime.toLocaleString());
currentTime.setMinutes(currentTime.getMinutes() + 15);
}
return timeData;
},
// 1
generateMinuteData() {
const [start, end] = this.timeRange;
const startTime = new Date(start);
const endTime = new Date(end);
const timeData = [];
const currentTime = new Date(startTime);
while (currentTime < endTime) {
timeData.push(currentTime.toLocaleString());
currentTime.setMinutes(currentTime.getMinutes() + 1);
}
return timeData;
},
//
// propsdata
getGatingData(rows, cols) {
let data = [];
console.log('cols:', cols, 'rows:', rows);
for (let i = 0; i < rows; i++) {
let oneLine = [];
for (let j = 0; j < cols; j++) {
oneLine.push(Math.floor(Math.random() * 100));
}
data.push(oneLine);
}
console.log("data", data);
return this.processSeriesData(data);
},
// ECharts
// processSeriesData(seriesData) {
// const rows = seriesData.length;
// const cols = seriesData[0].length;
// const data = [];
// let legendData = this.gratingIdArray.map(id => ` ${id}`);
// for (let i = 0; i < rows; i++) {
// let oneLine = {};
// oneLine['name'] = legendData[i];
// oneLine['type'] = 'line';
// // oneLine['stack'] = 'Total';
// oneLine['data'] = seriesData[i];
// data.push(oneLine);
// }
//
// return data;
// }
processSeriesData(data) {
const legendData = this.gratingIdArray.map(id => `光栅 ${id}`);
return data.map((rowData, index) => ({
name: legendData[index],
type: 'line',
// stack: 'Total'
data: rowData
}));
}
},
beforeDestroy() {
if (this.chart) {
this.chart.dispose();
}
}
};
</script>
<style scoped>
/* 组件样式 */
</style>