lishifeng
2020-09-15 ce10677f47a14879424e7f562f78442cc03cfda1
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
function Diagram() {
    this.stc = "";       // 静态层canvas
    this.flush = "";        // 刷新层canvas
    this.s_ctx = "";        // 静态层上下文
    this.f_ctx = "";        // 刷新层上下文
    this.width = 1500;      // 默认宽度
    this.height = 750;      // 默认高度
    this.options = [];      // 所有配置对象
    this.flushOptions = []; // 需要刷新的对象
    this.handleObj = [];    // 可以触发事件的对象
    this.opts = {           // 默认配置
        lineWidth: 1,       // 线宽
        onOffBase: {        // 开关的基础配置
            radius: 4,
            width: 28,
            lineWidth: 2,
        },
        moveDot: {
            radius: 6,
            step: 1
        }
    };
    
}
 
// 设置canvas
Diagram.prototype.setCanvas = function(stc, flush) {
    // 设置静态canvas
    this.stc = document.createElement('canvas');
    stc.appendChild(this.stc);
    this.stc.width = this.width;
    this.stc.height = this.height;
    this.s_ctx = this.stc.getContext('2d');
 
    // 设置动态canvas
    this.flush = document.createElement('canvas');
    flush.appendChild(this.flush);
    this.flush.width = this.width;
    this.flush.height = this.height;
    this.f_ctx = this.flush.getContext('2d');
 
    this.startState = false;
    
    // 启动更新
    this.start(true);
};
 
 
// 启动更新
Diagram.prototype.start = function(start) {
    if(start) {
        this.startState = true;
    }
 
    // 停止更新
    if(!this.startState) {
        return;
    }
    // 更新所有的配置项
    this._update();
    // 持续更新函数
    requestAnimationFrame(()=>{
        this.start();
    });
}
 
// 停止更新
Diagram.prototype.stop = function() {
    this.startState = false;
    console.log('已经停止持续更新');
};
 
// 更新整个图表
Diagram.prototype._update = function() {
    // 清空图表
    this.f_ctx.clearRect(0, 0, this.width, this.height);
    let options = this.flushOptions;
    options.forEach(option=> {
        this.update(option);
    });
};
// 根据配置项更新图表
Diagram.prototype.update = function(option) {
    if(option && option.method && typeof this[option.method] == 'function') {
        this[option.method](option);
    }
}
 
/**
 * 检测对象是否在数组中
 *
 * @param   {[object]}  option  需要检测的对象
 * 
 * @param   {[Array]}  options 对象数组
 *
 * @return  {[Boolean]}       返回对象是否在对象数组中
 */
Diagram.prototype.checkObjInArr = function(option, options) {
    // 如果未设置id
    if(!option.id) {
        return true;
    }
 
    // 根据id判断
    let isIn = false;
    for(let i=0; i<options.length; i++) {
        let _option = options[i];
        if(option.id == _option.id) {
            isIn  = true;
            break;
        }
    }
    // 返回结果
    return isIn;
}
 
// 添加配置项
Diagram.prototype.addOptions = function(option) {
    // 检测配置项是否在配置项中
    let options = this.options;
    // 检测对象不在对象中
    if(!this.checkObjInArr(option, options)) {
        options.push(option);
        // 可刷新,添加到刷新配置项
        if(option.flush) {
            this.flushOptions.push(option);
        }
    }
};
 
// 修改配置信息
Diagram.prototype.changeOption = function(id, attr, value, options) {
    for(let i=0; i<options.length; i++) {
        let _option = options[i];
        if(id == _option.id) {
            _option[attr] = value;
            break;
        }
    }
};
 
// 设置配置的值(仅可以设置刷新层的内容)
Diagram.prototype.setOption = function(id, attr, value) {
    let options = this.flushOptions;
    this.changeOption(id, attr, value, options);
};
 
// 获取option
Diagram.prototype.getOption = function(id) {
    let options = this.options;
    let result = false;
    for(let i=0; i<options.length; i++) {
        let option = options[i];
        if(option.id == id) {
            result = option;
        }
    }
    return result;
}
 
// 绘制线
Diagram.prototype.line = function(option) {
    let ctx = option.flush?this.f_ctx:this.s_ctx;       // 获取上下文
    let points = option.points;
    let opts = this.opts;
    ctx.beginPath();
    ctx.lineWidth = option.lineWidth?option.lineWidth:opts.lineWidth;
    ctx.strokeStyle = option.strokeStyle?option.strokeStyle:"#FF0000";
    points.forEach((point, index)=> {
        if(index != 0) {
            ctx.lineTo(point[0], point[1]);
        }else {
            ctx.moveTo(point[0], point[1]);
        }
    });
    ctx.stroke();
    if(false) {
        let point = [];
        if(points[0][0] == points[1][0]) {
            // 设置x轴坐标
            point[0] = points[0][0];
            // 设置y轴坐标
            if(points[0][1]>points[1][1]) {
                point[1] = points[1][1] + (points[0][1]-points[1][1])/2;
            }else {
                point[1] = points[1][1] + (points[0][1]-points[1][1])/2;
            }
        }else {
            // 设置y轴坐标
            point[1] = points[0][1];
            // 设置x轴坐标
            if(points[0][0]>points[1][0]) {
                point[0] = points[1][0] + (points[0][0]-points[1][0])/2;
            }else {
                point[0] = points[1][0] + (points[0][0]-points[1][0])/2;
            }
 
        }
        this.text({
            text: option.id?option.id:'',
            point: point
        });
    }
    // 设置执行的方法
    option.method = "line";
    // 添加配置项
    this.addOptions(option);
    return points;
};
 
// 绘制点线
Diagram.prototype.dashLine = function(option) {
    let ctx = option.flush?this.f_ctx:this.s_ctx;       // 获取上下文
    let opts = this.opts;
    let points = option.points;
    ctx.beginPath();
    // 设置间距(参数为无限数组,虚线的样式会随数组循环)
    ctx.setLineDash([2, 4]);
    ctx.lineWidth = option.lineWidth?option.lineWidth:opts.lineWidth;
    ctx.strokeStyle = option.strokeStyle?option.strokeStyle:"#FF0000";
    points.forEach((point, index)=> {
        if(index != 0) {
            ctx.lineTo(point[0], point[1]);
        }else {
            ctx.moveTo(point[0], point[1]);
        }
    });
    ctx.stroke();
    // 切回实线
    ctx.setLineDash([]);
    return points;
};
 
// 绘制充放电单元
Diagram.prototype.chargeAndDischarge = function(option) {
    let ctx = option.flush?this.f_ctx:this.s_ctx;       // 获取上下文
    let defaultOption = {
        width: 120,
    };
    let point = option.point;
    let strokeStyle = '#FFFFFF';
    let lineWidth = option.lineWidth?option.lineWidth:2;
    // 线条1
    let line1 = this.line({
        strokeStyle: strokeStyle,
        lineWidth: lineWidth,
        points: [
            point,
            [point[0], point[1]-defaultOption.width/2]
        ],
    });
 
    // 线条2
    let line2 = this.line({
        strokeStyle: strokeStyle,
        lineWidth: lineWidth,
        points: [
            line1[1],
            [line1[1][0]+defaultOption.width, line1[1][1]]
        ],
    });
 
    // 线条3
    let line3 = this.line({
        strokeStyle: strokeStyle,
        lineWidth: lineWidth,
        points: [
            line2[1],
            [line2[1][0]+defaultOption.width, line2[1][1]]
        ],
    });
 
    // 线条4
    let line4 = this.line({
        strokeStyle: strokeStyle,
        lineWidth: lineWidth,
        points: [
            line3[1],
            [line3[1][0], line3[1][1]+defaultOption.width]
        ],
    });
 
    // 线条5
    let line5 = this.line({
        strokeStyle: strokeStyle,
        lineWidth: lineWidth,
        points: [
            line4[1],
            [line1[1][0], line1[1][1]+defaultOption.width]
        ],
    });
 
    // 线条6
    let line6 = this.line({
        strokeStyle: strokeStyle,
        lineWidth: lineWidth,
        points: [
            line5[1],
            [line1[0][0], line1[0][1]]
        ],
    });
 
    // 线条7
    let line7 = this.line({
        strokeStyle: strokeStyle,
        lineWidth: lineWidth,
        points: [
            [line1[1][0]+defaultOption.width, line1[1][1]],
            [line1[1][0]+defaultOption.width, line1[1][1]+defaultOption.width]
        ],
    });
 
    return [
        [line1[1][0]+defaultOption.width/2, line1[1][1]],
        [line1[1][0]+defaultOption.width*3/2, line1[1][1]],
        [line1[1][0]+defaultOption.width*3/2, line1[1][1]+defaultOption.width],
        [line1[1][0]+defaultOption.width/2, line1[1][1]+defaultOption.width],
    ]
};
 
// 绘制圆
Diagram.prototype.arc = function(option) {
    let ctx = option.flush?this.f_ctx:this.s_ctx;       // 获取上下文
    let point = option.point;   //圆心坐标
    // 不隐藏绘制圆圈
    if(!option.hide) {
        ctx.beginPath();
        ctx.lineWidth = option.lineWidth?option.lineWidth:1;
        ctx.strokeStyle = option.strokeStyle;
        ctx.fillStyle = option.fillStyle;
        ctx.arc(point[0], point[1], option.radius, 0, 2*Math.PI);
        
        switch(option.type) {
            case 'stroke':
                ctx.stroke();
                break;
            case 'fill':
                ctx.fill();
                break;
            default:
                ctx.stroke();
                ctx.fill();
                break;
        }
    }
    
    let info = {
        center: [point[0], point[1]],
        left: [point[0]-option.radius, point[1]],
        right: [point[0]+option.radius, point[1]],
        top: [point[0], point[1]-option.radius],
        bottom: [point[0], point[1]+option.radius],
        tl: [point[0]-option.radius/2, point[1]-option.radius/2],
        tr: [point[0]+option.radius/2, point[1]-option.radius/2],
        bl: [point[0]-option.radius/2, point[1]+option.radius/2],
        br: [point[0]+option.radius/2, point[1]+option.radius/2],
        strokeStyle: option.strokeStyle,
        fillStyle: option.fillStyle,
        radius: option.radius
    };
    // 添加配置项
    option.method = "arc";
    this.addOptions(option);
    return info;
};
 
// 绘制开关
Diagram.prototype.onOff = function(option) {
    let result;
    switch(option.direction) {
        case 'top':
        case 'bottom':
            result = this.onOff_h(option);
        break;
        default:
            result = this.onOff_v(option);
        break;
    }
    // 添加配置项
    option.method = "onOff";
    this.addOptions(option);
    // 返回开关的信息
    return result;
};
 
// 水平方向
Diagram.prototype.onOff_v = function(option) {
    let point = option.point;
    let state = option.state?true:false;
    let onOffBase = this.opts.onOffBase;
 
    // 开关的第一个圆
    let arc1 = this.arc({
        point: [point[0]+onOffBase.radius/2, point[1]],
        flush: option.flush,
        fillStyle: '#ccc',
        strokeStyle: '#ccc',
        radius: onOffBase.radius
    });
 
    // 开关的第二个圆
    let arc2 = this.arc({
        point: [arc1.right[0]+onOffBase.width*4/3, arc1.right[1]],
        flush: option.flush,
        fillStyle: arc1.fillStyle,
        strokeStyle: arc1.fillStyle,
        radius: arc1.radius
    });
    let line1;
    switch(option.direction) {
        case 'right':
            // 绘制线条
            line1 = this.line({
                points: [
                    arc2.left,
                    [
                        state?arc1.top[0]:arc1.center[0],
                        state?arc1.top[1]:arc1.center[1]-(arc1.radius+onOffBase.width*3/4)
                    ],
                ],
                flush: option.flush,
                strokeStyle: state?'#FF0000':'#7AD038',
                lineWidth: onOffBase.lineWidth
            });
        break;
        default:
            // 绘制线条
            line1 = this.line({
                points: [
                    arc1.right,
                    [
                        state?arc2.top[0]:arc2.center[0],
                        state?arc2.top[1]:arc2.center[1]-(arc2.radius+onOffBase.width*3/4)
                    ],
                ],
                flush: option.flush,
                strokeStyle: state?'#FF0000':'#7AD038',
                lineWidth: onOffBase.lineWidth
            });
        break;
    }
    return {
        arc1: arc1,
        arc2: arc2,
        line1: line1
    }
};
// 竖直方向
Diagram.prototype.onOff_h = function(option) {
    let point = option.point;
    let state = option.state?true:false;
    let onOffBase = this.opts.onOffBase;
 
    // 开关的第一个圆
    let arc1 = this.arc({
        point: [point[0], point[1]+onOffBase.radius],
        flush: option.flush,
        fillStyle: '#ccc',
        strokeStyle: '#ccc',
        radius: onOffBase.radius
    });
 
    // 开关的第二个圆
    let arc2 = this.arc({
        point: [arc1.bottom[0], arc1.right[1]+onOffBase.width*4/3],
        flush: option.flush,
        fillStyle: arc1.fillStyle,
        strokeStyle: arc1.fillStyle,
        radius: arc1.radius
    });
 
    let line1;
    switch(option.direction) {
        case 'top':
            // 绘制线条
            line1 = this.line({
                points: [
                    arc1.bottom,
                    [
                        state?arc2.right[0]:arc2.center[0]+(arc1.radius+onOffBase.width*3/4),
                        state?arc2.right[1]:arc2.center[1]
                    ],
                ],
                flush: option.flush,
                strokeStyle: state?'#FF0000':'#7AD038',
                lineWidth: onOffBase.lineWidth
            });
        break;
        default:
            // 绘制线条
            line1 = this.line({
                points: [
                    arc2.top,
                    [
                        state?arc1.right[0]:arc1.center[0]+(arc2.radius+onOffBase.width*3/4),
                        state?arc1.right[1]:arc1.center[1]
                    ],
                ],
                flush: option.flush,
                strokeStyle: state?'#FF0000':'#7AD038',
                lineWidth: onOffBase.lineWidth
            });
        break;
    }
    return {
        arc1: arc1,
        arc2: arc2,
        line1: line1
    }
};
 
// BAT
Diagram.prototype.bat = function(option) {
    let ctx = option.flush?this.f_ctx:this.s_ctx;
    let point = option.point;
    let bat = {
        width: 14
    };
    ctx.fillStyle = option.fillStyle?option.fillStyle:"#FF0000";
    ctx.strokeStyle = option.strokeStyle;
    ctx.lineWidth = option.lineWidth?option.lineWidth: 3;
    
    ctx.fillRect(point[0]-bat.width/2, point[1]-bat.width/2, bat.width, bat.width);
    
    ctx.strokeRect(point[0]-bat.width/2, point[1]-bat.width/2, bat.width, bat.width)
    return {
        top: [point[0], point[1]-bat.width/2],
        bottom: [point[0], point[1]+bat.width/2],
        left: [point[0]-bat.width/2-ctx.lineWidth/2, point[1]],
        right: [point[0]+bat.width/2+ctx.lineWidth/2, point[1]]
    }
};
 
// 绘制图片
Diagram.prototype.drawImage = function(option) {
    let ctx = option.flush?this.f_ctx:this.s_ctx;
    let point = option.point;
    // 绘制内容
    if(!option.hide) {
        let img = new Image();
        img.src = option.url;
        if(option.flush) {
            ctx.drawImage(img, point[0], point[1], option.width, option.height);
        }else {
            img.onload = function() {
                ctx.drawImage(img, point[0], point[1], option.width, option.height);
            }
        }
        
    }
 
    // 返回图片信息
    let info = {
        tl: [point[0], point[1]],
        tr: [point[0]+option.width, point[1]],
        bl: [point[0], point[1]+option.height],
        br: [point[0]+option.width, point[1]+option.height],
        left: [point[0], point[1]+option.height/2],
        right: [point[0]+option.width, point[1]+option.height/2],
        top: [point[0]+option.width/2, point[1]],
        bottom: [point[0]+option.width/2, point[1]+option.height],
        width: option.width,
        height: option.height,
    };
 
    // 设置执行的方法
    option.method = "drawImage";
    // 添加配置项
    this.addOptions(option);
    return info;
};
 
// 绘制字体
Diagram.prototype.text = function(option) {
    let ctx = option.flush?this.f_ctx:this.s_ctx;
    let point = option.point;
    let fontSize = option.fontSize?option.fontSize:12;
    let texts = option.text.split('&');
    ctx.beginPath();
    ctx.fillStyle = option.fillStyle?option.fillStyle: '#FFFFFF';
    ctx.lineWidth = option.lineWidth?option.lineWidth:1;
    ctx.font = fontSize+'px Arial';
    ctx.textAlign = option.align?option.align:'start';
    ctx.textBaseline = option.baseline?option.baseline: 'top';
    texts.forEach((text, index)=> {
        if(index == 0 ) {
            ctx.fillText(text, point[0], point[1]+fontSize*index);
        }else {
            ctx.fillText(text, point[0], point[1]+(fontSize+4)*index);
        }
        
    });
 
    // 设置执行的方法
    option.method = "text";
    // 添加配置项
    this.addOptions(option);
};
 
// 绘制可移动的点
Diagram.prototype.moveDot = function(option) {
    let ctx = this.f_ctx;
    let opts = this.opts.moveDot;
    let points = option.points;
    let start = option.start?option.start:[points[0][0], points[0][1]];
    let fillStyle = option.fillStyle?option.fillStyle:"#FF0000";
    let strokeStyle = option.strokeStyle?option.strokeStyle: "#FF0000";
    if(this.getOption(option.id)) {
        switch(option.type) {
            case 'up':
                start[1] -= opts.step;
                // 约束限制
                if(start[1] < points[1][1]) {
                    start[1] = points[0][1];
                }
            break;
            case 'down':
                start[1] += opts.step;
                // 约束限制
                if(start[1] > points[1][1]) {
                    start[1] = points[0][1];
                }
            break;
            case 'left':
                start[0] += opts.step;
                // 约束限制
                if(start[0] > points[1][0]) {
                    start[0] = points[0][0];
                }
            break;
            case  'right':
                start[0] -= opts.step;
                // 约束限制
                if(start[0] < points[1][0]) {
                    start[0] = points[0][0];
                }
        }
        // 设置值
        this.setOption(option.id, 'start', start);
    }else {
        // 竖直运动
        if(points[0][0] == points[1][0]) {
            // 向上运动
            if(points[0][1]>points[1][1]) {
                option.type = 'up';
            }else {
                option.type = 'down';
            }
        }else {
            // 向左运动
            if(points[0][0] < points[1][0]) {
                option.type = 'left';
            }else {
                option.type = 'right';
            }
        }
        option.start = start;
    }
    this.arc({
        flush: true,
        point: start,
        fillStyle: fillStyle,
        strokeStyle: strokeStyle,
        radius: opts.radius
    });
 
    // 添加配置项
    option.method = "moveDot";
    this.addOptions(option);
};
 
// 根据id正则删除内容
Diagram.prototype.del = function(pattern) {
    // 清空options配置项
    this.options = this.options.filter(item=> {
        if(!pattern.test(item.id)) {
            return item;
        }
    });
 
    // 清空刷新对象
    this.flushOptions = this.flushOptions.filter(item=>{
        if(!pattern.test(item.id)) {
            return item;
        }
    });
};
 
export default Diagram;