lineChart.vue.js
32.3 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
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/**
* 线路模拟图vue组件。
* 属性说明:
* 1、name:线路名称,类型:字符串,必须明确指定(required)
* 2、code:线路编码,类型:字符串,必须明确指定(required)
* 3、route:线路路由数据,类型:数组,默认值:[]
* 数据示例:var test_route_data = [
* {
* names: ["A起点站"], // 站点名字数组
* ids: ["ACODE_0", "ACODE_1"], // 站点编码_上下行
* type: 2, // 0:上行 1:下行 2:同名合并 3:异名合并
* stationMark: "B"
* },
* {
* names: ["B中途站", "C1中途站"],
* ids: ["BCODE_0", "C1CODE_1"],
* type: 3,
* stationMark: "Z"
* },
* {
* names: ["C中途站"],
* ids: ["CCODE_0"],
* type: 0,
* stationMark: "Z"
* },
* {
* names: ["D中途站"],
* ids: ["DCODE_0", "DCODE_1"],
* type: 2,
* stationMark: "Z"
* },
* {
* names: ["E中途站", "E1中途站"],
* ids: ["ECODE_0", "E1CODE_1"],
* type: 3,
* stationMark: "Z"
* },
* {
* names: ["F终点站"],
* ids: ["FCODE_0", "FCODE_1"],
* type: 2,
* stationMark: "E"
* }];
* 4、gps:线路实时车辆数据,类型:数组,默认值:[]
* 数据示例:var test_gps_data = [
* {
* stopNo: 'BCODE', // 站点编码
* upDown: 0, // 上下行(0 上行 , 1 下行 , -1 无效)
* deviceId: '559L1014', // 设备编码
* instation: 1, // 0: 站外 1:站内 2:场内
* nbbm: 'W2B-064' // 车辆内部编码(自编号)
* },
* {
* stopNo: 'BCODE', // 站点编码
* upDown: 0, // 上下行(0 上行 , 1 下行 , -1 无效)
* deviceId: '559L1013', // 设备编码
* instation: 1, // 0: 站外 1:站内 2:场内
* nbbm: 'W2B-065' // 车辆内部编码(自编号)
* },
* {
* stopNo: 'CCODE', // 站点编码
* upDown: 0, // 上下行(0 上行 , 1 下行 , -1 无效)
* deviceId: '559L1015', // 设备编码
* instation: 0, // 0: 站外 1:站内 2:场内
* nbbm: 'W2B-066' // 车辆内部编码(自编号)
* },
* {
* stopNo: 'E1CODE', // 站点编码
* upDown: 1, // 上下行(0 上行 , 1 下行 , -1 无效)
* deviceId: '559L1025', // 设备编码
* instation: 1, // 0: 站外 1:站内 2:场内
* nbbm: 'W2B-026' // 车辆内部编码(自编号)
* },
*
* {
* stopNo: 'ACODE', // 站点编码
* upDown: 1, // 上下行(0 上行 , 1 下行 , -1 无效)
* deviceId: '559L1035', // 设备编码
* instation: 1, // 0: 站外 1:站内 2:场内
* nbbm: 'W2B-036' // 车辆内部编码(自编号)
* },
* {
* stopNo: 'ACODE', // 站点编码
* upDown: 1, // 上下行(0 上行 , 1 下行 , -1 无效)
* deviceId: '559L1045', // 设备编码
* instation: 1, // 0: 站外 1:站内 2:场内
* nbbm: 'W2B-046' // 车辆内部编码(自编号)
* },
* {
* stopNo: 'ACODE', // 站点编码
* upDown: 1, // 上下行(0 上行 , 1 下行 , -1 无效)
* deviceId: '559L1055', // 设备编码
* instation: 1, // 0: 站外 1:站内 2:场内
* nbbm: 'W2B-056' // 车辆内部编码(自编号)
* }];
* 4、width:图表宽度:类型:数字,默认值:600
* 5、height:图表高度:类型:数字,默认值:240,建议:内部svg高度122加上一些padding,总高度建议240及以上
*
* TODO:遍历对象属性的时候,以后建议使用Object.keys方法,使用$jQuery.each会遍历到原型链上的属性(代码内部逻辑不需要)
* 如:Object.prototype.temp1=11,使用$jQuery遍历所有对象都会得到temp1这个属性,代码内部的逻辑可能会产生问题
*/
var line_chart_vue = (function() {
/**
* 比较两个对象内部值是否相等。
*/
var _objectEquals = function(o1, o2) {
// 判断当前的属性是不是对象
var o1_is_object = o1 instanceof Object;
var o2_is_object = o2 instanceof Object;
var o1_is_array = o1 instanceof Array;
var o2_is_array = o2 instanceof Array;
if (o1_is_object && o2_is_object) { // 都是对象比较
// 如果都是对象,判断对象的属性数量是否一致
if (Object.keys(o1).length !== Object.keys(o2).length) {
return false;
}
// 递归判断每个属性是否一致
var o1_property_names = Object.keys(o1);
for (var i = 0; i < o1_property_names.length; i++) {
var o1_property_value = o1[o1_property_names[i]];
var o2_property_value = o2[o1_property_names[i]];
var equal = _objectEquals(o1_property_value, o2_property_value);
if (!equal) {
return equal;
}
}
} else if (o1_is_array && o2_is_array) { // 都是数组比较
// 如果是数组,判断长度是否一致
if (o1.length !== o2.length) {
return false;
}
// 每个数组元素顺序比对
for (var n = 0; n < o1.length; n++) {
var equal2 = _objectEquals(o1[n], o2[n]);
if (!equal2) {
return equal2;
}
}
} else {
return o1 === o2;
}
return true;
};
/**
* 创建线路名称title。
* @param createElement vue的render函数参数
* @param componentContext vue组件的this
*/
var createLineNameTitle = function(createElement, componentContext) {
return createElement(
'div',
{
class: {
'top-center-big-text' : true
},
domProps: {
innerText: componentContext.name
}
}
);
};
var createSvgDom = function(wrapDom, lineCode, height) {
return d3.select(wrapDom).append('svg')
.classed('line-chart', true)
.attr('data-code', lineCode)
.attr('onselectstart', 'return false')
.style('height', height);
};
var createGpsWrapSvg = function(svgDom) {
// 添加svg wrap
return svgDom.append('g').classed('gps-wrap', true);
};
var createGpsMarkerClusterSvg = function(svgDom) {
// 添加svg marker clusterer wrap(指多个图像叠加在一起)
return svgDom.append('g').classed('marker-clusterer', true);
};
var refreshLineSvg = function(componentContext) {
var $jQuery = componentContext._jQuery;
var test_route_data = componentContext.route;
var width = componentContext.width;
var height = componentContext.height;
var svgNameSpace = componentContext._svgns;
var svg = componentContext._svg;
var chart_height = componentContext._chart_height;
var chart_top_padding = componentContext._chart_top_padding;
var chart_left_padding = componentContext._chart_left_padding;
var chart_right_padding = componentContext._chart_right_padding;
var chart_stop_name_max_size = componentContext._chart_stop_name_max_size;
//------------ 1、添加g元素 ----------//
var items_update = svg.selectAll('g.item')
.data(test_route_data, function(d) {
return d.ids.join("#");
});
var items_enter = items_update;
var items_exit = items_update.exit();
items_exit.remove();
items_enter = items_enter.enter().append('g').classed('item', true);
// 创建x轴比例尺
var xScale = d3.scaleLinear()
.domain([0, test_route_data.length - 1]) // 定义域
.range([chart_left_padding, width - chart_right_padding]); // 值域
//------------ 2、添加/更新线路上行path元素 ----------//
var upLineFun = d3.line()
.x(xScale)
.y(function() {
return (height - chart_height) / 2 + chart_top_padding;
});
items_enter.append('path')
.classed('station_link', true)
.attr('d', function(d, i) {
return i < test_route_data.length - 1 ? upLineFun([i, i + 1]) : '';
});
items_update.select('path')
.attr('d', function(d, i) {
return i < test_route_data.length - 1 ? upLineFun([i, i + 1]) : '';
});
//------------ 3、添加/更新线路下行path元素 ----------//
var downLineFun = d3.line()
.x(xScale)
.y(function() {
return (height - chart_height) / 2 + chart_top_padding + chart_height;
});
items_enter.append('path')
.classed('station_link', true)
.classed('down', true)
.attr('d', function(d, i) {
return i < test_route_data.length - 1 ? downLineFun([i, i + 1]) : '';
});
items_update.select('path.down')
.attr('d', function(d, i) {
return i < test_route_data.length - 1 ? downLineFun([i, i + 1]) : '';
});
//------------ 4、添加/更新线路上行circle元素 ----------//
items_enter.select(function(d) {
return d.type != 1 ? this : null;
}).append('circle')
.classed('station_circle', true)
.attr('cx', function(d, i) {
return xScale(i);
})
.attr('cy', function() {
return (height - chart_height) / 2 + chart_top_padding;
})
.attr('data-id', function(d) {
return d.ids[0];
});
items_update.select('circle')
.attr('cx', function(d, i) {
return xScale(i);
})
.attr('cy', function() {
return (height - chart_height) / 2 + chart_top_padding;
});
//------------ 5、添加/更新线路下行circle元素 ----------//
items_enter.select(function(d) {
return d.type != 0 ? this : null;
}).append('circle')
.classed('station_circle', true)
.classed('down', true)
.attr('cx', function(d, i) {
return xScale(i);
})
.attr('cy', function(d, i) {
return (height - chart_height) / 2 + chart_top_padding + chart_height;
})
.attr('data-id', function(d) {
return d.type == 1 ? d.ids[0] : d.ids[1];
});
items_update.select('circle.down')
.attr('cx', function(d, i) {
return xScale(i);
})
.attr('cy', function(d, i) {
return (height - chart_height) / 2 + chart_top_padding + chart_height;
});
//------------ 6、添加线路上行行站点名text元素 ----------//
items_enter.append('text')
.classed('station_text', true)
.classed('up', function(d) {
return d.type == 3 || d.type == 0 ? true : false;
})
.classed('down', function(d) {
return d.type == 1 ? true : false;
})
.text(function(d) {
if (!d.names[0]) {
return 0;
}
return d.names[0].length > chart_stop_name_max_size
? d.names[0].substr(0, chart_stop_name_max_size) : d.names[0];
})
.attr('title', function(d) {
return d.names[0];
})
.attr('x', function(d, i) {
if (d.type != 3) {
return xScale(i);
}
// 处理多文本
var svg_text = document.createElementNS(svgNameSpace, 'text');
var text_str = d.names[1].length > chart_stop_name_max_size
? d.names[1].substr(0, chart_stop_name_max_size) : d.names[1];
var text_x = xScale(i);
var text_y1 = (height - chart_height) / 2 + chart_top_padding;
var text_y2 = (chart_height - (chart_height / chart_stop_name_max_size * text_str.length)) / 2 + 5;
d3.select(svg_text)
.attr('class', 'station_text down')
.attr('title', text_str)
.attr('x', text_x + 8)
.attr('y', text_y1 + text_y2)
.text(text_str);
// svg_text添加在当前元素后面
$jQuery(this).after(svg_text);
// this.insertAdjacentElement('afterend', svg_text);
return xScale(i) - 8;
})
.attr('y', function(d) {
var y1 = (height - chart_height) / 2 + chart_top_padding;
var text = d.names[0].length > chart_stop_name_max_size
? d.names[0].substr(0, chart_stop_name_max_size) : d.names[0];
var y2 = (chart_height - (chart_height / chart_stop_name_max_size * text.length)) / 2 + 5;
return y1 + y2;
});
items_update.select('text.station_text')
.classed('up', function(d) {
return d.type == 3 || d.type == 0 ? true : false;
})
.classed('down', function(d) {
return d.type == 1 ? true : false;
})
.text(function(d) {
if (!d.names[0]) {
return 0;
}
return d.names[0].length > chart_stop_name_max_size
? d.names[0].substr(0, chart_stop_name_max_size) : d.names[0];
})
.attr('title', function(d) {
return d.names[0];
})
.attr('x', function(d, i) {
if (d.type != 3) {
return xScale(i);
}
// 处理多文本
$jQuery(this).next().remove(); // 删除之前的多文本,next()就是下一个兄弟节点
var svg_text = document.createElementNS(svgNameSpace, 'text');
var text_str = d.names[1].length > chart_stop_name_max_size
? d.names[1].substr(0, chart_stop_name_max_size) : d.names[1];
var text_x = xScale(i);
var text_y1 = (height - chart_height) / 2 + chart_top_padding;
var text_y2 = (chart_height - (chart_height / chart_stop_name_max_size * text_str.length)) / 2 + 5;
d3.select(svg_text)
.attr('class', 'station_text down')
.attr('title', text_str)
.attr('x', text_x + 8)
.attr('y', text_y1 + text_y2)
.text(text_str);
// svg_text添加在当前元素后面
$jQuery(this).after(svg_text);
return xScale(i) - 8;
})
.attr('y', function(d) {
var y1 = (height - chart_height) / 2 + chart_top_padding;
var text = d.names[0].length > chart_stop_name_max_size
? d.names[0].substr(0, chart_stop_name_max_size) : d.names[0];
var y2 = (chart_height - (chart_height / chart_stop_name_max_size * text.length)) / 2 + 5;
return y1 + y2;
});
};
var findLineCircle = function(componentContext, stopNo, upDown) {
var $jQuery = componentContext._jQuery;
// 查找对应的circle
var svg_circle;
try {
svg_circle = $jQuery('.station_circle[data-id=' + stopNo + "_" + upDown + ']');
if (svg_circle.length == 0) {
svg_circle = null;
}
} catch (e) {
console.log('未找到svg circle, data-id = ' + stopNo + "_" + upDown);
svg_circle = null;
}
return svg_circle;
};
var refreshGpsSvg = function(componentContext) {
var $jQuery = componentContext._jQuery;
var gps = componentContext.gps;
var svg = componentContext._svg;
var gps_wrap_svg = componentContext._gps_wrap_svg;
var gps_marker_cluster_svg = componentContext._gps_marker_cluster_svg;
var test_route_data = componentContext.route;
var test_gps_data = componentContext.gps;
var width = componentContext.width;
var chart_left_padding = componentContext._chart_left_padding;
var chart_right_padding = componentContext._chart_right_padding;
//-------------- 0、数据处理 --------------//
/*
* 按照站点编码和上下行分组
* 数据示例:
* gps_group_by_stopNo_upDown = {
* 'BCODE_0': ['559L1014', '559L1013'],
* 'CCODE_0': ['559L1015'],
* 'E1CODE_1': ['559L1025'],
* 'ACODE_1': ['559L1035', '559L1045', '559L1055']
* };
*/
var gps_group_by_stopNo_upDown = {};
$jQuery.each(gps, function(i, d) {
var stopNo = d.stopNo; // 站点编码
var upDown = d.upDown; // 上下行(0 上行 , 1 下行 , -1 无效)
var groupKey = stopNo + "_" + upDown;
if (!gps_group_by_stopNo_upDown[groupKey]) {
gps_group_by_stopNo_upDown[groupKey] = [];
}
var deviceId = d.deviceId; // 设备编码
gps_group_by_stopNo_upDown[groupKey].push(deviceId);
});
/*
* 将分组后的设备编码array转换成map
* 数据示例:
* test_gps_index_mapp = {
* 'BCODE_0': {
* '559L1014': 0, '559L1013' : 1
* },
* 'CCODE_0': {
* '559L1015': 0
* },
* 'E1CODE_1': {
* '559L1025': 0
* },
* 'ACODE_1': {
* '559L1035': 0, '559L1045': 1, '559L1055': 2
* }
* };
*/
var test_gps_index_mapp = {};
$jQuery.each(gps_group_by_stopNo_upDown, function(key, value) {
if (!test_gps_index_mapp[key]) {
test_gps_index_mapp[key] = {};
}
$jQuery.each(value, function(index, deviceId) {
test_gps_index_mapp[key][deviceId] = index;
})
});
//--------------- 1、添加/更新车辆rect元素 --------------//
$jQuery('.merge_hide', svg).removeAttr('class'); // 删除.mverge_hide元素的class属性(样式)
// 绘制rect
var gps_rect_update = gps_wrap_svg.selectAll('rect')
.data(test_gps_data, function(d) {
return d.deviceId; // 指定主键,默认使用数组下标
});
var gps_rect_enter = gps_rect_update;
var gps_rect_exit = gps_rect_update.exit();
gps_rect_exit.remove();
gps_rect_x = function(d) {
// 查找对应的circle
var svg_circle = findLineCircle(componentContext, d.stopNo, d.upDown);
// 计算x
var x;
if (!svg_circle) {
x = -100;
return x;
}
var x1 = parseInt(svg_circle.attr('cx')) - 16.5;
var s = (width - (chart_left_padding + chart_right_padding)) / test_route_data.length;
if (d.instation == 0) {
if (d.upDown == 0) {
x = x1 + s;
} else {
x = x1 - s;
}
} else {
x = x1;
}
// 显示/隐藏
if (x == -100) {
$jQuery(this).css('transition-duration', 0).hide();
} else {
$jQuery(this).show();
}
return x;
};
gps_rect_y = function(d) {
// 查找对应的circle
var svg_circle = findLineCircle(componentContext, d.stopNo, d.upDown);
// 计算y
var y;
if (!svg_circle) {
y = -100;
return y;
}
y = parseInt(svg_circle.attr('cy'));
var index = test_gps_index_mapp[d.stopNo + "_" + d.upDown][d.deviceId];
if (d.upDown == 0) {
y = y - 22 - (index * 17);
} else {
y = y + 6 + (index * 19);
}
return y;
};
gps_rect_enter.enter().append('rect')
.attr('_id', function(d) {
return 'rct_' + d.deviceId;
})
.transition()
.attr('x', gps_rect_x)
.attr('y', gps_rect_y)
.attr('updown', function(d) {
return d.upDown;
});
gps_rect_update
.attr('_id', function(d) {
return 'rct_' + d.deviceId;
})
.transition()
.attr('x', gps_rect_x)
.attr('y', gps_rect_y)
.attr('updown', function(d) {
return d.upDown;
});
//--------------- 3、添加/更新车辆text元素 --------------//
var gps_text_update = gps_wrap_svg.selectAll('text')
.data(test_gps_data, function(d) {
return d.deviceId; // 指定主键,默认使用数组下标
});
var gps_text_enter = gps_text_update;
var gps_text_exit = gps_text_update.exit();
gps_text_exit.remove();
var gps_text_x = function(d) {
// 查找对应的circle
var svg_circle = findLineCircle(componentContext, d.stopNo, d.upDown);
// 计算x
var x;
if (!svg_circle) {
x = -100;
return x;
}
var x1 = parseInt(svg_circle.attr('cx')) - 16.5;
var s = (width - (chart_left_padding + chart_right_padding)) / test_route_data.length;
if (d.instation == 0) {
if (d.upDown == 0) {
x = x1 + s;
} else {
x = x1 - s;
}
} else {
x = x1;
}
// 显示/隐藏
if (x == -100) {
$jQuery(this).css('transition-duration', 0).hide();
} else {
$jQuery(this).show();
}
return x;
};
var gps_text_y = function(d) {
// 查找对应的circle
var svg_circle = findLineCircle(componentContext, d.stopNo, d.upDown);
// 计算y
var y;
if (!svg_circle) {
y = -100;
return y;
}
y = parseInt(svg_circle.attr('cy'));
var index = test_gps_index_mapp[d.stopNo + "_" + d.upDown][d.deviceId];
if (d.upDown == 0) {
y = y - 22 - (index * 17);
} else {
y = y + 6 + (index * 19);
}
return y;
};
var gps_text_text = function(d) {
if (d.nbbm) {
var nbbm_length = d.nbbm.length;
if (nbbm_length > 3) {
var nbbm_text = d.nbbm.substr(nbbm_length - 3);
if (d.nbbm.indexOf('-') > 0) {
return d.nbbm.substr(d.nbbm.indexOf('-') - 1, 1) + nbbm_text;
} else {
return nbbm_text;
}
} else {
return d.nbbm;
}
} else {
return d.nbbm;
}
};
gps_text_enter.enter().append('text')
.attr('_id', function(d) {
return "tx_" + d.deviceId;
})
.transition()
.attr('x', gps_text_x)
.attr('y', gps_text_y)
.attr('updown', function(d) {
return d.upDown;
})
.text(gps_text_text);
gps_text_update
.attr('_id', function(d) {
return "tx_" + d.deviceId;
})
.transition()
.attr('x', gps_text_x)
.attr('y', gps_text_y)
.attr('updown', function(d) {
return d.upDown;
})
.text(gps_text_text);
//--------------- 4、绘制/更新车辆合并显示元素(g rect text) --------------//
// 绘制聚合点的数据集对象列表
var _merger_data = [];
$jQuery.each(test_gps_index_mapp, function(key, value) {
var stopNo = key.split("_")[0];
var upDown = key.split("_")[1];
var deviceIds = [];
$jQuery.each(value, function(deviceId) {
deviceIds.push(deviceId);
});
// 删除旧的合并点
$jQuery('g[_id=' + 'merger_' + key + ']').remove();
if (deviceIds.length <= 2) { // 小于等于2个gps,不合并
return true;
}
// 查找circle
var svg_circle = findLineCircle(componentContext, stopNo, upDown);
if (!svg_circle) {
return true;
}
var x = parseInt(svg_circle.attr('cx'));
var y = parseInt(svg_circle.attr('cy'));
// 隐藏车辆rect
$jQuery.each(deviceIds, function(i, d) {
$jQuery('rect[_id=rct_' + d + '],text[_id=tx_' + d + ']').attr('class', 'merge_hide');
});
_merger_data.push({
x: x,
y: y,
key: key,
upDown: upDown,
length: deviceIds.length
});
// // 创建 merge g rect text
// var merge_g = svg.selectAll('g.marker-clusterer')
// .append('g')
// .classed('merge-item', true)
// .attr('_id', 'merger_' + key);
// merge_g.append('rect')
// .attr('x', x - 11)
// .attr('y', function() {
// return upDown == 0 ? y - 31 : y + 8;
// });
// merge_g.append('text')
// .text(deviceIds.length)
// .attr('x', x - ((deviceIds.length + '').length * 4.5))
// .attr('y', upDown == 0 ? y - 14 : y + 24);
});
var merge_update = gps_marker_cluster_svg.selectAll('g')
.data(_merger_data, function(d) {
return d.key;
});
var merge_enter = merge_update;
var merge_exit = merge_update.exit();
merge_exit.remove();
merge_enter = merge_enter.enter().append('g')
.classed('merge-item', true)
.attr('_id', function(d) {
return 'merger_' + d.key;
});
merge_enter.append('rect')
.attr('x', function(d) {
return d.x - 11;
})
.attr('y', function(d) {
return d.upDown == 0 ? d.y - 31 : d.y + 8;
});
merge_update.select('rect')
.attr('x', function(d) {
return d.x - 11;
})
.attr('y', function(d) {
return d.upDown == 0 ? d.y - 31 : d.y + 8;
});
merge_enter.append('text')
.text(function(d) {
return d.length;
})
.attr('x', function(d) {
return d.x - ((d.length + '').length * 4.5);
})
.attr('y', function(d) {
return d.upDown == 0 ? d.y - 14 : d.y + 24;
});
merge_update.select('text')
.text(function(d) {
return d.length;
})
.attr('x', function(d) {
return d.x - ((d.length + '').length * 4.5);
})
.attr('y', function(d) {
return d.upDown == 0 ? d.y - 14 : d.y + 24;
});
};
return {
data: function() {
// 不被vue监控的属性放在这里
// 作用是每个组件实例内部的一组局部变量
// 所有局部变量以_开头
this._chart_height = 122; // 线路图高度
this._chart_left_padding = 30; // 线路图距离左边的padding
this._chart_right_padding = 30; // 线路图距离右边的padding
this._chart_top_padding = 4; // 线路图距离上边的padding
this._chart_stop_name_max_size = 7; // 站定名最大文字数量
this._svgns = 'http://www.w3.org/2000/svg'; // svg元素名字空间
this._svg; // 创建的根svg元素,由mounted钩子函数创建
this._gps_wrap_svg; // 创建包含gps设备显示的最外层g元素
this._gps_marker_cluster_svg; // 创建包含聚合显示gps的最外层g元素
this._jQuery = jQuery.noConflict(); // jquery引用
return {
// 被vue监控的属性
// 如:this.text = 'dfdfdf';
};
},
props: { // 属性
name: { // 线路名称
type: String,
required: true,
},
code: { // 线路代码
type: String,
required: true
},
route: { // 路由数据
type: Array,
default: function() {
return [];
}
},
gps: { // gps数据
type: Array,
default: function() {
return [];
}
},
width: { // 图宽度
type: Number,
default: 600
},
height: { // 图高度
type: Number,
default: 240
}
},
watch: {
code: function(currentVal) {
this._svg.attr('data-code', currentVal);
},
route: function(currentVal, oldVal) {
// console.log(this.route);
// console.log("route change");
if (!_objectEquals(currentVal, oldVal)) {
refreshLineSvg(this);
refreshGpsSvg(this);
}
},
gps: function(currentVal, oldVal) {
if (!_objectEquals(currentVal, oldVal)) {
// refreshLineSvg(this);
refreshGpsSvg(this);
}
},
width: function() {
refreshLineSvg(this);
refreshGpsSvg(this);
},
height: function() {
refreshLineSvg(this);
refreshGpsSvg(this);
}
},
/**
* render函数代替template写法
* @param createElement
*/
render(createElement) {
return createElement(
'div',
{
style: {
'width': this.width + "px",
'height': this.height + 'px',
'border': '1px solid black',
'margin': '5px',
'margin-bottom': '0px',
'position': 'relative'
}
},
[
createLineNameTitle(createElement, this)
]
)
},
mounted: function() {
// console.log("mounted");
this._svg = createSvgDom(this.$el, this.code, this.height);
this._gps_wrap_svg = createGpsWrapSvg(this._svg);
this._gps_marker_cluster_svg = createGpsMarkerClusterSvg(this._svg);
refreshLineSvg(this);
refreshGpsSvg(this);
}
};
}());