whychw
2020-11-20 640034fb4f659cbcecbd23df75016d97231ad7bd
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<template>
  <div class="main">
    <!-- list -->
    <div class="list">
      <div class="list-title">
        <div class="">设备列表</div>
      </div>
      <div class="list-content posR">
        <div class="posA_full">
          <div class="inner">
            <div class="list-item" :class="{'isRun': item.connection_state, 'disabled': !item.connection_state, 'active': cur_id == item.dev_id}" v-for="(item, index) in list" :key="'list_' + index" @click="selectDev(item)">
              <div class="name">{{item.dev_name}}</div>
            </div>
            <!-- 无数据 -->
            <div v-if="!list.length" class="empty-list">暂无数据</div>
          </div>
        </div>
      </div>
    </div>
    <div class="page-content">
      <div class="page-banner tac">
        <div class="f_right d_flex">
          <div class="btn-grp" v-show="!showTable">
 
            <div class="btn_3d" :class="{'disabled': prevDisabled}" @click="prevGrp">上一组</div>
            <div class="btn_3d" :class="{'disabled': nextDisabled}" @click="nextGrp">下一组</div>
            <div class="btn_3d" @click="showGOptions">图表选项</div>
          </div>
          <!-- <div class="btn_3d" @click="toggle">显示{{showTable ? '图表' : '表格'}}</div> -->
        </div>
        <div class="content-title">设备名称</div>
      </div>
      <div class="wraper">
        <div class="container" :class="{'showTable': showTable}">
          <div class="wrap-graph">
          <line-chart-mul id="G_linechart" ref="G_linechart" 
            title="我是标题"
            height="100%"
            :max-size='true'
            :show-xaxis="true"
            :categoryLen="categoryLen"
            :show-zoom='false'
            sub-title-width="160"
            :title-split-len="12"
            :subtext="true"  trigger-on="mousemove"></line-chart-mul>
          </div>
          <div class="wrap-table">
            <assemble-table
              :data=rtData
              :len="3"
              ></assemble-table>
          </div>
        </div>
      </div>
    </div>
    <!-- 图表配置 -->
    <el-drawer
      title="图表配置"
      direction="rtl"
      size="800px"
      custom-class="drawer-bg drawer-options"
      :visible.sync="G_options.show" >
      <div class="drawer-content">
        <el-form ref="form" :inline="true" size="mini" :model="form.data" :rules="form.rules" label-width="8em">
          <el-form-item label="显示属性数量" prop="categoryLen">
            <el-input type="number" v-model="form.data.categoryLen"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="setCategoryLen">设定</el-button>
          </el-form-item>
        </el-form>
        <el-transfer
            class=""
            v-model="selectedLegends"
            :titles="['不查看属性', '待查看属性']"
            :button-texts="['移除', '添加']"
            :data="allLegends"
            :filterable="true"
            @change="selectChange"
            >
          </el-transfer>
      </div>
    </el-drawer>
    <!-- ROOTEND -->
  </div>
</template>
 
<script>
import Timeout from '@/script/Timeout'
import Mock from 'mockjs'
import AssembleTable from '@/components/AssembleTable';
import LineChartMul from '@/components/chart/LineChartMul';
import CONFIG from '@/script/config_subject';
 
// 设定值的备份 因为要处理最后的余项
let category_len = 10;
let unit = {};
let config = null;
export default {
  name: 'subject_realtime',
  components: {
    AssembleTable
    ,LineChartMul
  },
  data () {
    const dev_id = this.$route.query.dev_id;
    return {
      list: []
      ,timer: new Timeout()
      ,cur_id: 0
      ,cur_type: 0
      ,showTable: false
      ,rtData: []
      ,G_data: []
      ,curLegendIdx: 0
      // 当前选中的
      ,selectedLegends: []
      // 图表显示的
      ,curLegends: []
      // 查询出的全量
      ,allLegends: []
      // 筛选后的
      ,categoryList: []
      ,record_time: []
      ,categoryLen_old: 10
      ,categoryLen: 10
      ,G_options: {
        show: false
      }
      ,keyword: ''
      ,form: {
        data: {
          categoryLen: 10
        }
        ,rules: {
          categoryLen: [{
            validator: (rule, value, callback) => {
              if (!('' + value).trim()) {
                callback(new Error('显示属性数量必填'));
              }
              if (/[^0-9]/.test(value)) {
                callback(new Error('显示属性数量必须为数值'));
              }
              if (value < 1 || value > 10) {
                callback(new Error('显示属性数量应介于1~10之间'));
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }]
        }
      }
    }
  },
  computed: {
    prevDisabled () {
      return this.curLegendIdx < category_len;
    }
    ,nextDisabled () {
      // 收集依赖
      this.categoryLen;
      return this.curLegendIdx + category_len >= this.selectedLegends.length;
    }
  },
  methods: {
    // 获取列表
    getList () {
      let list = [];
      this.$api.subject.getList().then((res) => {
        res = JSON.parse(res.data.result);
        // console.log(res, '++++++++++++++++++++');
        if (res.code) {
          list = res.data;
          list.sort((a, b) => {
            return b.connection_state - a.connection_state;
          });
        }
        this.list = list;
      });
    }
    ,toggle () {
      this.showTable = !this.showTable;
    }
    // 选定设备
    ,selectDev (obj) {
      // 根据id 查询实时信息
      // 选取前100笔数据
      // 拿到配置
      // console.log(obj, 'obj');
      this.cur_id = obj.dev_id;
      this.cur_type = obj.dev_type;
      let param = [];
      switch (obj.dev_type) {
        case 11:
        case 12:
        case 19:
          param = [obj.dev_type, obj.dev_id];
          break;
        default:
          param = [obj.dev_type];
          break;
      }
      // 初始化属性列表
      this.initLegends(...param);
      this.get100Data(obj.dev_type, obj.dev_id);
    }
    // 初始化要显示的属性列表
    ,initLegends (type, id) {
      config = id ? CONFIG.DIR[type][id] : CONFIG.DIR[type];
      unit = config.unit;
      let allLegends = [];
      let selectedLegends = [];
      if (config) {
        // console.log(config);
        let props = Object.keys(config);
        for (let i = 0, j = props.length; i < j; i++) {
          const v = props[i];
          // debugger;
          if (v != 'unit' && config[v] != '备用') {
            allLegends.push({
              key: v,
              label: config[v],
              unit: config.unit[v]
            });
            selectedLegends.push(v);
          }
        }
      }
      this.allLegends = allLegends;
      this.selectedLegends = selectedLegends;
      this.curLegends = selectedLegends.slice(this.curLegendIdx, category_len);
      this.updateGraph();
 
    }
    // 根据id查询实时数据
    ,getRTData () {
      /*if (!this.cur_id) {
        return false;
      }*/
      let param = {
        type: this.cur_type
        ,data: {
          dev_id: this.cur_id
        }
      };
 
      this.$api.subject.getRTData(param).then((res) => {
        res = JSON.parse(res.data.result);
        // console.log(res, 'rtdata');
        if (res.code) {
          this.G_data.push(res.data[0]);
        }
        this.updateGraph();
      });
    }
    // 根据id查询前100笔数据
    ,get100Data (type, id) {
      this.timer.stop();
      let param = {
        type,
        data: {
          dev_id: id
        }
      };
      this.$api.subject.get100Data(param).then((res) => {
        res = JSON.parse(res.data.result);
        // console.log(res, 'res');
        let list = [];
        if (res.code) {
          list = res.data;
        }
        // debugger;
        this.G_data = list;
        this.timer.open();
      });
    }
    ,format (data) {
      let xLabel = [],
          series = [],
          obj = {},
          attrs = {};
      let curLegends = this.curLegends;
      if (data.length > 100) {
        data.shift();
      }
      // debugger;
      curLegends.forEach((v) => {
        obj[v] = [];
        attrs[v] = attrs[v] || [];
        series.push({
          name: config[v],
          unit: unit[v],
          data: obj[v],
          step: '' == unit[v]
        })
      });
 
      data.forEach((v) => {
        xLabel.push(v.record_time);
        Object.keys(v).forEach((val) => {
          attrs[val] = attrs[val] || [];
          attrs[val].push(v[val]);
        });
      });
          
      Object.keys(obj).forEach((v) => {
        obj[v].push(...attrs[v]);
      });
 
      // console.log(xLabel, series, 99899);
      
      return {
        xLabel
        ,series
      }
    }
    // 选配置项 上一组
    ,prevGrp () {
      if (this.curLegendIdx < category_len) {
        return false;
      }
      this.curLegendIdx -= category_len;
      this.curLegends = this.selectedLegends.slice(this.curLegendIdx, this.curLegendIdx + category_len);
      // 更新图表
      this.updateGraph();
    }
    // 选配置项 下一组
    ,nextGrp () {
      if (this.curLegendIdx + category_len >= this.selectedLegends.length) {
        return false;
      }
      // debugger;
      this.curLegendIdx += category_len;
      this.curLegends = this.selectedLegends.slice(this.curLegendIdx, this.curLegendIdx + category_len);
      // 更新图表
      this.updateGraph();
    }
    // 更新图表
    ,updateGraph () {
      /*if (!this.G_data.length) {
        return false;
      }*/
 
      this.categoryLen = this.curLegends.length;
 
      if (this.categoryLen != this.categoryLen_old) {
        // console.log('clear');
        this.$G.chartManage.get('G_linechart').clear();
      }
      this.categoryLen_old = this.categoryLen;
      this.$nextTick(() => {
        this.$refs['G_linechart'].setOption(this.format(this.G_data));
      });
    }
    // 设定categoryLen
    ,setCategoryLen () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.categoryLen = this.form.data.categoryLen * 1;
          category_len = this.form.data.categoryLen * 1;
          this.curLegendIdx = 0;
          this.curLegends = this.selectedLegends.slice(this.curLegendIdx, this.curLegendIdx + category_len);
          this.updateGraph();
        }
      });
    }
    ,showGOptions () {
      // console.log(this.selectedLegends, 765)
      // debugger;
      this.G_options.show = true;
    }
    ,selectChange (list, type, values) {
      // console.log('hhh',list, type, values);
      this.curLegendIdx = 0;
      this.selectedLegends = list;
      this.curLegends = this.selectedLegends.slice(this.curLegendIdx, this.curLegendIdx + category_len);
      this.updateGraph();
    }
    // 定时器初始化
    ,timerInit () {
      this.timer.init(() => {
        this.getRTData();
        this.timer.open();
      }, 1000);
    }
  },
  mounted () {
    this.getList();
    this.timerInit();
  },
  destroyed() {
    this.timer.stop();
  }
}
</script>
<style scoped>
.main {
  display: -webkit-flex;
  display: flex;
  height: 100%;
}
.list {
  display: -webkit-flex;
  display: flex;
  flex-direction: column;
  width: 14em;
  margin-right: .4em;
}
.list-title {
  background: #24a3bf;
  padding: 10px 0;
  text-align: center;
}
.list-content {
  flex: 1;
  background: #00324b;
}
.inner {
  height: 100%;
  padding: .6em 0 .6em .6em;
  overflow-y: auto;
}
.list-item {
  margin: .2em 0;
  color: #999;
}
.list-item .name {
  font-style: italic;
}
.list-item.isRun {
  color: #fff;
  cursor: pointer;
}
.list-item.disabled {
  cursor: not-allowed;
}
.list-item.active {
  background: rgba(200, 200, 200, .4);
}
.list-item.isRun .name {
  font-style: normal;
}
.list-item:hover {
  background: rgba(200, 200, 200, .2);
}
.page-content {
  flex: 1;
  /*background: rgba(0,0,0,.2);*/
  display: -webkit-flex;
  display: flex;
  flex-direction: column;
}
.wraper {
  flex: 1;
  overflow: hidden;
}
.page-banner {
  padding: 6px;
}
.page-banner .btn_3d {
  width: 6em;
  margin-left: 1em;
}
.container {
  width: 200%;
  height: 100%;
  display: -webkit-flex;
  display: flex;
  -webkit-transition: -webkit-transform 0.4s;
  transition: transform 0.4s;
}
.showTable {
  -webkit-transform: translateX(-50%);
  transform: translateX(-50%);
}
.wrap-graph,
.wrap-table {
  flex: 1;
}
.wrap-graph {
  background: rgba(0,0,0,.2);
}
.empty-list {
  text-align: center;
  -webkit-transform: translate(-.3em, 1em);
  transform: translate(-.3em, 1em);
}
.btn-grp {
  display: -webkit-flex;
  display: flex;
}
>>> .drawer-options {
  background: #005074;
}
.drawer-content {
  height: 100%;
  display: -webkit-flex;
  display: flex;
  flex-direction: column;
  padding: 10px;
}
</style>