Files
admin-govern/src/views/govern/device/control/tabs/trend.vue
2024-09-30 11:48:34 +08:00

728 lines
30 KiB
Vue

<template>
<div>
<!-- 历史趋势数据 -->
<div>
<TableHeader :showSearch="false" @selectChange="selectChange">
<template v-slot:select>
<!-- <el-form :model="searchForm" class="history_select" id="history_select"> -->
<el-form-item>
<DatePicker ref="datePickerRef"></DatePicker>
</el-form-item>
<el-form-item label="统计指标" label-width="80px">
<el-select multiple :multiple-limit="3" collapse-tags collapse-tags-tooltip
v-model="searchForm.index" placeholder="请选择统计指标" @change="onIndexChange($event)">
<el-option v-for="item in indexOptions" :key="item.id" :label="item.name"
:value="item.id"></el-option>
</el-select>
</el-form-item>
<div v-for="(item, index) in countData" :key="index">
<el-form-item :label="item.name + '谐波次数'" v-if="item.countOptions.length != 0"
label-width="180px">
<el-select v-model="item.count" multiple collapse-tags collapse-tags-tooltip
@change="onCountChange($event, index)" placeholder="请选择谐波次数">
<el-option v-for="vv in item.countOptions" :key="vv" :label="vv"
:value="vv"></el-option>
</el-select>
</el-form-item>
</div>
<!-- </el-form> -->
</template>
<template #operation>
<el-button type="primary" icon="el-icon-Search" @click="init()">查询</el-button>
</template>
</TableHeader>
</div>
<div class="history_chart" :style="pageHeight" v-loading="loading">
<MyEchart ref="historyChart" :options="echartsData" />
</div>
</div>
</template>
<script lang="ts" setup>
import popup from './components/popup.vue'
import schemeTree from './components/schemeTree.vue'
import { mainHeight } from '@/utils/layout'
import { queryByCode, queryByid, queryCsDictTree } from '@/api/system-boot/dictTree'
import { ref, reactive, onMounted, provide, nextTick, watch } from 'vue'
import { ElMessage } from 'element-plus'
import MyEchart from '@/components/echarts/MyEchart.vue'
import { getTestRecordInfo, getHistoryTrend } from '@/api/cs-device-boot/planData'
import { useDictData } from '@/stores/dictData'
import { queryStatistical } from '@/api/system-boot/csstatisticalset'
import * as echarts from 'echarts'
import { isNonNullChain } from 'typescript'
import { position } from 'html2canvas/dist/types/css/property-descriptors/position'
import { read, writeFile, utils } from 'xlsx'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { getTabsDataByType } from '@/api/cs-device-boot/EquipmentDelivery'
import DatePicker from '@/components/form/datePicker/index.vue'
import { color, gradeColor3 } from '@/components/echarts/color'
const dictData = useDictData()
defineOptions({
name: 'govern/device/control'
})
//电压等级
const voltageLevelList = dictData.getBasicData('Dev_Voltage')
//接线方式
const volConTypeList = dictData.getBasicData('Dev_Connect')
//值类型
const pageHeight = ref(mainHeight(290))
const loading = ref(true)
const searchForm = ref({})
const typeOptions = [
{
name: '平均值',
id: 'avg'
},
{
name: '最大值',
id: 'max'
},
{
name: '最小值',
id: 'min'
},
{
name: 'CP95值',
id: 'cp95'
}
]
searchForm.value = {
index: [],
type: typeOptions[0].id,
count: [],
searchBeginTime: '',
searchEndTime: ''
}
//统计指标
const indexOptions: any = ref([])
//谐波次数
const countOptions: any = ref([])
// Harmonic_Type
// portable-harmonic
const legendDictList: any = ref([])
queryByCode('portable-harmonic').then(res => {
queryCsDictTree(res.data.id).then(item => {
indexOptions.value = item.data
searchForm.value.index[0] = indexOptions.value[0].id
})
queryStatistical(res.data.id).then(vv => {
legendDictList.value = vv.data
})
})
const activeName: any = ref()
const deviceData: any = ref([])
//历史趋势devId
const historyDevId: any = ref('')
const chartTitle: any = ref('')
const echartsData = ref<any>(null)
//加载echarts图表
//历史趋势数据
const historyDataList: any = ref([])
const range = (start: any, end: any, step: any) => {
return Array.from({ length: (end - start) / step + 1 }, (_, i) => start + i * step)
}
//获取请求趋势数据参数
const trendRequestData = ref()
const getTrendRequest = (val: any) => {
trendRequestData.value = val
init()
}
//初始化趋势图
const headerRef = ref()
const datePickerRef = ref()
const init = async () => {
loading.value = true
// 选择指标的时候切换legend内容和data数据
let list: any = []
legendDictList.value?.selectedList?.map((item: any) => {
searchForm.value.index.map((vv: any) => {
if (item.dataType == vv) {
list.push(item.eleEpdPqdVOS)
}
})
})
//颜色数组
const colorList = color
//选择的指标使用方法处理
initSearchFormIndexAndCount(searchForm.value.index)
//查询历史趋势
historyDataList.value = []
chartTitle.value = ''
searchForm.value.index.map((item: any, indexs: any) => {
indexOptions.value.map((vv: any) => {
if (vv.id == item) {
chartTitle.value += indexs == searchForm.value.index.length - 1 ? vv.name : vv.name + '/'
}
})
})
let lists: any = []
countData.value.map((item: any, index: any) => {
lists[index] = {
statisticalId: item.index,
frequencys: item.count
}
})
let obj = {
...trendRequestData.value,
list: lists,
valueType: searchForm.value.type,
startTime: datePickerRef.value && datePickerRef.value.timeValue[0],
endTime: datePickerRef.value && datePickerRef.value.timeValue[1]
}
if (obj.devId && obj.list.length != 0) {
try {
await getTabsDataByType(obj)
.then((res: any) => {
if (res.code == 'A0000') {
historyDataList.value = res.data
echartsData.value = null
//icon图标替换legend图例
const iconThree =
'path://M512 85.333333c235.637333 0 426.666667 191.029333 426.666667 426.666667S747.637333 938.666667 512 938.666667 85.333333 747.637333 85.333333 512 276.362667 85.333333 512 85.333333z m214.592 318.677334a32 32 0 0 0-45.248 0.064L544.736 541.066667l-81.792-89.109334a32 32 0 0 0-46.613333-0.576l-119.36 123.733334a32 32 0 1 0 46.058666 44.437333l95.754667-99.264 81.418667 88.704a32 32 0 0 0 46.24 0.96l160.213333-160.693333a32 32 0 0 0-0.064-45.248z'
const iconDanger =
'path://M1001.661867 796.544c48.896 84.906667 7.68 157.013333-87.552 157.013333H110.781867c-97.834667 0-139.050667-69.504-90.112-157.013333l401.664-666.88c48.896-87.552 128.725333-87.552 177.664 0l401.664 666.88zM479.165867 296.533333v341.333334a32 32 0 1 0 64 0v-341.333334a32 32 0 1 0-64 0z m0 469.333334v42.666666a32 32 0 1 0 64 0v-42.666666a32 32 0 1 0-64 0z'
let xAxis: any = [],
timeList: any = []
let unitList: any = []
historyDataList.value.map((item: any) => {
timeList.push(item.time)
if (unitList.indexOf(item.unit) == -1) {
unitList.push(item.unit)
}
})
xAxis = timeList.sort((a: any, b: any) => {
return new Date(a).getTime() - new Date(b).getTime()
})
echartsData.value = {
options: {
title: [
{
left: '10%',
top: 0,
text: chartTitle.value
}
],
toolbox: {
feature: {
// saveAsImage: {
// title: '保存'
// },
// dataView: { readOnly: false },
// }
}
},
tooltip: {
trigger: 'axis',
textStyle: {
color: '#fff',
fontStyle: 'normal',
opacity: 0.35,
fontSize: 14
},
backgroundColor: 'rgba(0,0,0,0.35)',
borderWidth: 0,
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999'
}
},
formatter: function (params: any) {
let list = params.filter((item, index, self) => {
return index === self.findIndex(t => t.seriesName === item.seriesName)
})
var res =
'<span style="margin-right:18px;"></span>' +
list[0].value[0] +
list[0].name +
'<br/>'
for (var i = 0, l = list.length; i < l; i++) {
list[i].unit =
echartsData.value.options.yAxis[
echartsData.value.options.series[list[i].seriesIndex].yAxisIndex
]?.name
res +=
list[i].seriesName +
' ' +
`<div style="width:16px;height:16px;float:left;background:${list[i].color};border-radius:50%;margin:0 15px"></div>` +
`<div style='float:right;padding-left:20px;'>${list[
i
].value[1].toFixed(2)} ${list[i].unit||''} </div><br/>`
}
return res
}
},
legend: {
//legend使用iconfont图标
data: [],
itemWidth: 20,
itemHeight: 10,
itemGap: 15,
type: 'scroll', // 开启滚动分页
// orient: 'vertical', // 垂直排列
right: '3%', // 位置调整
top: 0,
bottom: 20,
width: 430,
height: 50
},
xAxis:{
type: 'time',
axisLabel: {
formatter: {
day: '{MM}-{dd}',
}
},},
// [
// {
// type: 'category',
// name: '时间',
// axisLabel: {
// color: '#A9AEB2',
// fontSize: 12,
// show: function (index: any, value: any) {
// // 检查数据中是否存在这个时间点
// // return data.some(item:any => item[0] === value)
// }
// },
// nameTextStyle: {
// right: 0
// },
// data: Array.from(new Set(xAxis)),
// axisLine: {
// lineStyle: {
// color: '#43485E'
// }
// }
// }
// ],
yAxis: [
{
type: 'value',
name: unitList[0],
axisLabel: {
color: '#000',
fontSize: 14
},
splitNumber:5,
minInterval: 1,
axisTick: {
show: true
},
axisLine: {
show: true,
lineStyle: {
color: colorList[0]
}
},
nameTextStyle: {
color: '#000',
fontSize: '14'
},
splitLine: {
show: false,
lineStyle: {
color: ['#43485E'],
width: 1,
type: 'solid'
}
}
}
],
series: []
}
}
//处理多y轴
if (unitList.length != 0 && unitList.length > 1) {
// echartsData.value.options.yAxis[0].yAxisIndex = 0
unitList.map((item: any, index: any) => {
if (index != unitList.length - 1) {
echartsData.value.options.yAxis.push({
type: 'value',
position: 'right',
offset: index * 80, // y轴位置的偏移量
name: unitList[index + 1],
axisLabel: {
color: '#000',
fontSize: 14
},
splitNumber:5,
minInterval: 1,
axisTick: {
show: true
},
axisLine: {
show: true,
lineStyle: {
color: colorList[index + 1]
}
},
nameTextStyle: {
color: '#000',
fontSize: '14'
},
splitLine: {
show: false,
lineStyle: {
color: ['#43485E'],
width: 1,
type: 'solid'
}
}
})
}
})
}
list.map((item: any, index: any) => {
item.map((vv: any, indexs: any) => {
//处理legend
if (historyDataList.value.length != 0) {
echartsData.value.options.legend.data.push({
name: vv.phase + '相' + vv.showName,
icon: iconThree
})
}
if (
unitList.findIndex((item: any) => {
return item == vv.unit
}) != -1
) {
vv.yAxisIndex = unitList.findIndex((item: any) => {
return item == vv.unit
})
} else {
vv.yAxisIndex = 0
}
//series数据
echartsData.value.options.series.push({
name: vv.phase + '相' + vv.showName,
type: 'line',
smooth: true,
symbol: 'none',
emphasis: {
focus: 'series'
},
itemStyle: {},
//数据
data: historyDataList.value
.map((kk: any) => {
if (kk.statisticalName == vv.name) {
return [kk.time, kk.statisticalData]
} else {
return ''
}
})
.filter((kk: any) => kk !== ''),
//数据对应的y轴
// yAxisIndex:0,
yAxisIndex: vv.yAxisIndex
})
})
})
//设置数据项颜色
echartsData.value.options.series.map((item: any, index: any) => {
item.itemStyle = {
normal: {
color: colorList[index],
lineStyle: {
color: colorList[index]
}
}
}
})
loading.value = false
}
})
.catch(error => {
loading.value = false
})
} catch (error) {
loading.value = false
}
}
}
const selectChange = (flag: boolean) => {
if (flag) {
pageHeight.value = mainHeight(345)
} else {
pageHeight.value = mainHeight(290)
}
}
//导出
const historyChart = ref()
// const chart: any = ref(null)
// chart.value = echarts.init(historyChart.value)
const handleExport = async () => {
const planCsv = ref('')
const chartsCsv = ref('')
if (deviceData.value.records && deviceData.value.records.length != 0) {
let csv = '',
obj: any = {}
obj = deviceData.value.records.find((item: any) => {
return item.id == activeName.value
})
if (obj) {
//测试是否与变量名长度有关系
let cell1 = deviceData.value.itemName,
cell2 = deviceData.value.describe,
cell3 = obj?.itemName,
cell4 = obj?.statisticalInterval,
cell5 = voltageLevelList.find(vv => {
return vv.id == obj.voltageLevel
})?.name,
cell6 = volConTypeList.find(vv => {
return vv.id == obj.volConType
})?.name,
cell7 = obj.capacitySscmin,
cell8 = obj.capacitySi,
cell9 = obj.capacitySscb,
cell10 = obj.capacitySt,
cell11 = obj.pt && obj.pt1 ? obj.pt / obj.pt1 + '\b' : '/',
cell12 = obj.ct && obj.ct1 ? obj.ct / obj.ct1 + '\b' : '/',
cell13 = obj.startTime ? obj.startTime : '/',
cell14 = obj.endTime ? obj.endTime : '/',
cell15 = obj.location
csv = `方案测试项信息,
方案名称, ${cell1},
方案描述, ${cell2},
测试项名称, ${cell2},
测量间隔, ${cell4 + '分钟'},
电压等级, ${cell5},
接线方式, ${cell6},
最小短路容量, ${cell7 + 'MVA'},
用户协议容量, ${cell8 + 'MVA'},
基准短路容量, ${cell9 + 'MVA'},
供电设备容量, ${cell10 + 'MVA'},
PT变比, ${cell11},
CT变比, ${cell12},
起始时间, ${cell13},
结束时间, ${cell14},
监测位置, ${cell15}\n,
`
planCsv.value = csv
}
}
if (historyDataList.value.length != 0) {
let xAxis: any = []
let timeList: any = []
historyDataList.value.map((item: any) => {
timeList.push(item.time)
})
xAxis = timeList.sort((a: any, b: any) => {
return (
a.replace('-', '').replace('-', '').replace(' ', '').replace(':', '').replace(':', '') -
0 -
(b.replace('-', '').replace('-', '').replace(' ', '').replace(':', '').replace(':', '') - 0)
)
})
xAxis = Array.from(new Set(xAxis))
// 使用这个函数转换数据为CSV格式
let csv: any = ''
const list = echartsData.value.options.series
csv = convertToCSV([], [])
chartsCsv.value = csv
// 如果你想提供下载链接
// const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' })
// const link = document.createElement('a')
// link.href = URL.createObjectURL(blob)
// link.download = '历史趋势.csv'
// link.click()
function convertToCSV(data: any, key: any) {
// 添加列头
let title = '统计时间,'
list.map((item: any, index: any) => {
index == list.length - 1 ? (title += `${item.name}\n`) : (title += `${item.name},`)
})
let csv = ''
csv = title
// 遍历数据并添加到CSV字符串中
const listLength = list.length
list[0].data.map((vv: any, indexs: any) => {
let strs = '',
count = null
list.map((item: any, index: any) => {
if (index == 0) {
count = index
}
index == list.length - 1
? (strs += list[index].data[indexs])
: (strs += list[index].data[indexs] + ',')
})
if (count == 0 && xAxis[indexs]) {
csv += `${xAxis[indexs]},` + strs + '\n'
}
})
return csv
}
}
let csvs = ''
if (chartsCsv.value) {
csvs = planCsv.value + '历史趋势数据,\n\n' + chartsCsv.value
} else {
csvs = planCsv.value
}
// 如果你想提供下载链接
const blob = new Blob([csvs], { type: 'text/csv;charset=utf-8;' })
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
let obj = deviceData.value.records.find((item: any) => {
return item.id == activeName.value
})
const date = window.XEUtils.toDateString(new Date(), 'yyyyMMdd HHmmss').replace(' ', '_')
link.download = `${deviceData.value.itemName}_${obj?.itemName}_${date}.csv`
link.click()
return
}
const countData: any = ref([])
//根据选择的指标处理谐波次数
const initSearchFormIndexAndCount = (list: any) => {
if (list.length != 0) {
list.map((item: any, index: any) => {
if (!countData.value[index]) {
countData.value[index] = {
index: item,
countOptions: [],
count: [],
name: indexOptions.value.find((vv: any) => {
return vv.id == item
})?.name
}
}
legendDictList.value?.selectedList?.map((vv: any, vvs: any) => {
//查找相等的指标
if (item == vv.dataType) {
vv.eleEpdPqdVOS.map((kk: any, kks: any) => {
if (kk.harmStart && kk.harmEnd) {
range(0, 0, 0)
countData.value[index].countOptions = range(kk.harmStart, kk.harmEnd, 1)
//添加默认值
if (countData.value[index].count.length == 0) {
countData.value[index].count = [countData.value[index].countOptions[0]]
}
}
})
}
})
})
}
}
// 判断下拉框是否存在
const onCountChange = (val: any, index: any) => {
if (val.length == 0) {
countData.value[index].count = [countData.value[index].countOptions[0]]
}
}
const onIndexChange = (val: any,) => {
if (val.length == 0) {
searchForm.value.index = [indexOptions.value[0].id]
}
}
watch(
() => searchForm.value.index,
(val: any, oldval: any) => {
if (val) {
initSearchFormIndexAndCount(val)
if (val == 0) {
countData.value = []
}
countData.value.map((item: any, key: any) => {
if (
val.findIndex((vv: any) => {
return vv == item.index
}) == -1
) {
countData.value.splice(key, 1)
}
})
}
},
{
deep: true,
immediate: true
}
)
// watch(
// () => trendRequestData.value,
// (val: any, oldval: any) => {
// if (val) {
// init()
// }
// },
// {
// deep: true,
// immediate: true
// }
// )
onMounted(() => {
datePickerRef.value.setInterval(5)
})
defineExpose({ getTrendRequest })
</script>
<style lang="scss" scoped>
.history_header {
display: flex;
// flex-wrap: wrap;
#history_select {
width: 100%;
display: flex;
// justify-content: flex-start;
// overflow-x: auto;
height: auto;
flex-wrap: wrap;
.el-form-item {
flex: none !important;
// max-width: 380px;
}
.el-select {
margin-right: 10px;
}
}
// #history_select::-webkit-scrollbar {
// width: 0 !important;
// display: none !important;
// }
.history_searchBtn {
flex: 1;
display: flex;
justify-content: flex-end;
margin-top: 3px;
}
}
.history_chart {
width: 100%;
// flex: 1;
margin-top: 10px;
}
</style>