he wei
15 小时以前 011aab07af85ba820b9f96a02c249b7ba26c8d26
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
<script setup>
    import { onMounted, ref, reactive, watchEffect, computed, watch, nextTick, onBeforeUnmount } from "vue";
    import * as echarts from 'echarts';
  import baseChart from "./BaseChart.vue";
 
  const chart = ref(null);
  const props = defineProps({
    type: {
      type: String,
      default: '电流'
    },
    yLabels: {
      type: Array,
      default: () => []
    },
    startIdx: {
      type: [Number, String],
      default: 0
    },
    modeCount: {
      type: [Number, String],
      default: 4
    },
    title: {
      type: String,
      default: ''
    },
    unit: {
      type: String,
      default: ''
    }
  });
 
  const fullFlag = ref(false);
 
  // 这两个数值是百分比 
    const baseTop = 10;
    let gridHeight = 20;
 
  let gridCount = computed(() => {
    return props.yLabels.length || 1;
  });
 
  const emit = defineEmits(['scroll']);
 
 
    function makeXAxis(gridIndex, opt) {
        let res = echarts.util.merge({
            type: 'category',
      boundaryGap: false,
            gridIndex: gridIndex,
            axisLine: { onZero: false, lineStyle: { color: '#ddd' } },
            axisTick: { show: false },
            axisLabel: { show: false },
            splitLine: { show: false, lineStyle: { color: '#ddd' } },
        }, opt || {}, true);
    return JSON.parse(JSON.stringify(res));
    }
 
    function makeYAxis(gridIndex, opt) {
        let res = echarts.util.merge({
            type: 'value',
            gridIndex: gridIndex,
            nameLocation: 'middle',
            nameTextStyle: {
                color: '#fff',
                lineHeight: 18
            },
            nameRotate: 0,
            boundaryGap: ['30%', '30%'],
            axisTick: { show: false },
            axisLine: { lineStyle: { color: '#ccc' } },
            axisLabel: { show: false },
            splitLine: { show: false }
        }, opt || {}, true);
    return JSON.parse(JSON.stringify(res));
    }
 
 
    function makeGrid(top, opt) {
        let res = echarts.util.merge({
            top: top + '%',
            height: gridHeight - 0.5 + '%',
      bottom: top + gridHeight - 0.5 + '%',
            left: 90,
            right: 14,
        }, opt || {}, true);
    return JSON.parse(JSON.stringify(res));
    }
 
 
    function getSeries(datas) {
        let res = [];  
    
        datas.forEach((item, idx) => {
            res.push({
                type: 'line',
        name: props.yLabels[idx],
                smooth: true,
                symbol: 'circle',
                symbolSize: 5,
                showSymbol: true,
        xAxisIndex: idx,
        yAxisIndex: idx,
        gridIndex: idx,
        seriesIndex: idx,
        // 禁用动画,确保立即渲染
        animation: false,
        // silent: true,
                lineStyle: {
                    width: 1
                },
        tooltip: {
          show: true,
        },
                data: datas[idx].map(v => v + 3),
            });
        });
 
        // return JSON.parse(JSON.stringify(res));
    return res;
    }
 
  let lastXLabels = [];
  let lastDatas = [];
 
  watch(
    () => props.yLabels,
    () => {
      nextTick(() => {
        chart.value.setOption(getOptions(lastXLabels, lastDatas));
      });
    },
  );
 
 
    function getOptions(xLabels, datas) {
        xLabels = xLabels || [];
    datas = datas || [];
 
    lastXLabels = xLabels;
    lastDatas = datas;
 
 
    let series = getSeries(datas);
    
    let grid = [];
    let xAxis = [];
    let yAxis = [];
    let counter = gridCount.value;
    gridHeight = (100 - 20) / counter;
    
    for (let i = 0; i < counter; i++) {
      grid.push(makeGrid(baseTop + gridHeight * i));
      let xAxisOption = i == counter - 1 ? {
          data: xLabels,
          axisTick: { show: true },
          axisLabel: { show: true },
          axisLine: {
            lineStyle: {
              color: '#fff'
            }
          },
      } : { axisLine: { show: false }};
      xAxis.push(makeXAxis(i, xAxisOption));
      yAxis.push(makeYAxis(i, {
        name: props.yLabels.length ? props.yLabels[i] : '',
      }));
    }
 
        const option = {
      animation: false,
      color: ['#1186ce', '#e5c619', '#1d1dfd', '#2dbfae'],
            tooltip: {
                trigger: 'axis',
        // 强制显示所有系列
        alwaysShowContent: true,
                axisPointer: {
          snap: true,
                    lineStyle: {
                        color: '#fff'
                    }
                },
        formatter: function (params) {
          // console.log('params:', params);
          // console.log('捕获的seriesIndex:', params.map(p => p.seriesIndex));
                    if (params.length) {
            // params.unshift({ seriesName: 'time', value: params[0].name, color: '#5193f2' })
            let _label = '';
                        let res =  props.yLabels.map((seriesName, idx) => {
                            for (var i = 0; i < params.length; i++) {
                                var param = params[i];
                                var style = 'color: ' + param.color;
                                if (param.seriesIndex === idx) {
                  _label = param.name;
                                    return '<span style="' + style + '">'
                                        + seriesName + props.type
                                        + ':</span><span style="'
                                        + style + '">' + param.value + '</span>';
                                }
                            }
                        });
            res.push('<span style="color: #000">' + _label + '</span>');
            res = res.join('<br>');
            // console.log('res', res, params, '=====tooltip========', series, datas, xLabels[xLabels.length - 1]);
            return res;
            
                    }
                }
            },
            axisPointer: {
                link: [{ xAxisIndex: 'all' }],
        lineStyle: {
          color: '#fff'
        },
                snap: true
            },
            grid,
            xAxis,
            yAxis,
            series
        };
 
    // console.log('option', option, '=======dcout======');
    
        return option;
    }
 
  let myChart = null;
  const startIndex = computed(() => {
    return Math.floor(props.startIdx);
  });
 
    function initChart() {
        if (chart.value) {
            // myChart = chart.value.getChart();
 
            let option = getOptions();
      chart.value.setOption(option);
        }
    }
 
    function updateChart(xLabels, datas) {
    if (chart.value) {
            let option = getOptions(xLabels, datas);
      // chart.value.setOption(option, {notMerge: true});
      chart.value.setOption(option);
      
      // tooltip Bug 重新渲染了一下 就没问题了
      if (props.startIdx == 0) {
        nextTick(() => {
          emit('scroll', 0.1);
        });
      }
        }
    }
 
  function scrollToTop() {
    if (startIndex.value > 0) {
      emit('scroll', -1);      
    }
    
  }
 
  function scrollToBottom() {
    if (startIndex.value < props.modeCount - 4) {
      nextTick(() => {
        emit('scroll', 1);
      });
    }
  }
 
  defineExpose({
    updateChart
  });
 
    onMounted(() => {
    // console.log('line mounted', '=============');
    
        initChart();
    });
</script>
 
<template>
  <div class="line-chart">
    <base-chart ref="chart" v-model:fullFlag="fullFlag" :key="startIdx">
      <template #tools v-if="modeCount > 4">
        <div class="t-contain">
          <div :class="['btn', 'btn-top', {disabled: startIndex <= 0}]" @click="scrollToTop">
            <svg-icon icon-class="arrow-right" ></svg-icon>
          </div>
          <div :class="['btn', 'btn-bottom', {disabled: startIndex == modeCount - 4}]" @click="scrollToBottom">
            <svg-icon icon-class="arrow-right" ></svg-icon>
          </div>
        </div>
      </template>
    </base-chart>
  </div>
</template>
 
<style scoped lang="less">
.line-chart {
  width: 100%;
  height: 100%;
 
  .t-contain {
    position: absolute;
    right: 4px;
    top: 0;
    bottom: 0;
    padding-top: 40px;
    padding-bottom: 40px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    
 
    .btn {
      cursor: pointer;
      font-size: 14px;
      &:hover {
        color: #0ff;
      }
      &.disabled {
        cursor: not-allowed;
        color: #666;
      }
      &.btn-top {
        transform: rotate(-90deg);
      }
      &.btn-bottom {
        transform: rotate(90deg);
      }
    }
  }
}
</style>