ReportPreviewModal.vue
4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<template>
<div>
<BasicModal
destroyOnClose
v-bind="$attrs"
width="60rem"
:height="heightNum"
@register="register"
title="报表趋势图"
:showOkBtn="false"
>
<div class="wrapper">
<div class="inner item" v-for="(item, index) in initChartData" :key="index">
<p style="display: none">{{ item }}</p>
<div :id="`chart${index}`" :style="{ height, width }"></div>
</div>
</div>
</BasicModal>
</div>
</template>
<script setup lang="ts">
import { ref, PropType, nextTick } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import * as echarts from 'echarts';
import { exportViewChartApi } from '/@/api/export/exportManager';
import moment from 'moment';
defineProps({
width: {
type: String as PropType<string>,
default: '100%',
},
height: {
type: String as PropType<string>,
default: '400px',
},
});
defineEmits(['register']);
const heightNum = ref(800);
const getItem: any = ref([]);
const initChartData: any = ref([]);
//生成随机颜色
let getRandomColor = function () {
return (
'rgb(' +
Math.round(Math.random() * 255) +
',' +
Math.round(Math.random() * 255) +
',' +
Math.round(Math.random() * 10) +
')'
);
};
const [register, { setModalProps }] = useModalInner(async (data) => {
setModalProps({ loading: true });
try {
const entityId = data.record.executeCondition?.executeAttributes.map((m) => m?.device);
let key = data.record.executeCondition?.executeAttributes.map((m) => m?.attributes);
let params: any = {};
params = {
...data.record.executeCondition?.executeCondition,
...{
keys: key.length !== 0 ? key.join(',') : '',
},
};
const result = await exportViewChartApi(entityId[0], params);
getItem.value = Object.entries(result).map((item) => {
const [attr, val] = item;
return { attr, val };
});
//服务端返回值
initChartData.value = getItem.value.map((item): any => {
const seriesData = item.val.map((m1) => Number(m1.value));
return {
attr: item.attr,
lengendData: [item.attr],
xAxisData: item.val.map((m) => moment(m.ts).format('YYYY-MM-DD HH:mm:ss')),
seriesData: [
{
name: item.attr,
type: 'line',
data: seriesData,
lineStyle: {
color: getRandomColor(),
},
},
],
};
});
nextTick(() => {
initChartData.value.forEach((item, index) => {
let myChart = echarts.init(document.getElementById(`chart${index}`) as HTMLElement);
let myOption = {
title: {
text: `${item.attr}趋势图`,
subtext: `${item.attr}`,
left: 'left',
},
tooltip: {
trigger: 'axis',
},
legend: {
data: item.lengendData,
top: '20px',
},
toolbox: {},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true,
},
dataZoom: [
{
type: 'slider',
show: true,
start: 0,
end: 30,
xAxisIndex: [0],
},
],
xAxis: {
type: 'category',
data: item.xAxisData,
boundaryGap: false,
axisPointer: { type: 'shadow' },
axisLabel: {
color: '#333',
// 让x轴文字方向为竖向
interval: 0,
rotate: 90,
// formatter: function (value) {
// return value.split('').join('\n');
// },
},
},
yAxis: {
type: 'value',
boundaryGap: false,
},
series: item.seriesData,
};
myChart.setOption(myOption);
//自适应
window.addEventListener('resize', () => {
myChart.resize();
});
});
});
} finally {
setModalProps({ loading: false });
}
});
</script>
<style lang="less" scoped>
@import url('./ReportPreviewModal.less');
</style>