pageTest.js
16.9 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
/**
* 使用d3配合jquery绘制线路模拟图。
*/
(function($jQuery) {
/**
* 测试的线路路由数据
* 上行路由起点战到终点战:A B C D E F
* 下行路由终点战到起点站:A C1 D E1 F
* 按照绘制需求合并路由数据图示如下:
* A B C D E F
* A C1 D E1 F
*/
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"
}
];
var width = $jQuery('.svg-chart-wrap').actual('outerWidth'); // 最外层元素宽度
var height = $jQuery('.svg-chart-wrap').actual('outerHeight'); // 最外层元素高度
if (height < 20) { // 隐藏元素取最外层的高度
height = $jQuery('.svg-chart-wrap').parent().actual('outerHeight') - 2;
}
var chart_height = 122; // 线路图高度
var chart_left_padding = 30; // 线路图距离左边的padding
var chart_right_padding = 30; // 线路图距离右边的padding
var chart_top_padding = 4; // 线路图距离上边的padding
var chart_stop_name_max_size = 7; // 站定名最大文字数量
var svgns = 'http://www.w3.org/2000/svg'; // svg元素名字空间
// 创建x轴比例尺
var xScale = d3.scaleLinear()
.domain([0, test_route_data.length - 1]) // 定义域
.range([chart_left_padding, width - chart_right_padding]); // 值域
// 添加svg dom
var svg = d3.select('.svg-chart-wrap').append('svg')
.classed('line-chart', true)
.attr('data-code', '线路编码1')
.attr('onselectstart', 'return false')
.style('height', height);
// 添加g元素
var items = svg.selectAll('g.item')
.data(test_route_data)
.enter()
.append('g')
.classed('item', true);
// 上行线path
var upLineFun = d3.line()
.x(xScale)
.y(function() {
return (height - chart_height) / 2 + chart_top_padding;
});
items.append('path')
.classed('station_link', true)
.attr('d', function(d, i) {
return i < test_route_data.length - 1 ? upLineFun([i, i + 1]) : '';
});
// 下行线path
var downLineFun = d3.line()
.x(xScale)
.y(function() {
return (height - chart_height) / 2 + chart_top_padding + chart_height;
});
items.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]) : '';
});
// 上行线circle
items.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];
});
// 下行线cricle
items.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.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(svgns, '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;
$jQuery(svg_text)
.attr('class', 'station_text down')
.attr('x', text_x + 8)
.attr('y', text_y1 + text_y2)
.text(text_str);
$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;
});
//---------- 绘制gps车辆 ---------//
// 添加svg wrap
svg.append('g').classed('gps-wrap', true);
// 添加svg marker clusterer wrap(指多个图像叠加在一起)
svg.append('g').classed('marker-clusterer', true);
var svgsIsHidden = function(svgs) { // 判断svg是否隐藏
$jQuery.each(svgs, function(i, d) {
if ($jQuery(d).parent().is(":visible")) {
return false;
}
});
return true;
};
/**
* 测试用gps数据(对应后台GpsEntity)。
* @type {*[]}
*/
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' // 车辆内部编码(自编号)
}
];
var 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
}
};
// 绘制gps点位
$jQuery('.merge_hide', svg).removeAttr('class'); // 删除.mverge_hide元素的class属性(样式)
// 绘制rect
svg.select('.gps-wrap').selectAll('rect')
.data(test_gps_data, function(d) {
return d.deviceId; // 指定主键,默认使用数组下标
})
.enter()
.append('rect')
.attr('_id', function(d) {
return 'rct_' + d.deviceId;
})
.transition()
.attr('x', function(d) {
// 查找对应的circle
var svg_circle;
try {
svg_circle = $jQuery('.station_circle[data-id=' + d.stopNo + "_" + d.upDown + ']');
if (svg_circle.length == 0) {
svg_circle = null;
}
} catch (e) {
console.log('未找到svg circle, data-id = ' + d.stopNo + "_" + d.upDown);
svg_circle = null;
}
// 计算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;
})
.attr('y', function(d) {
// 查找对应的circle
var svg_circle;
try {
svg_circle = $jQuery('.station_circle[data-id=' + d.stopNo + "_" + d.upDown + ']');
if (svg_circle.length == 0) {
svg_circle = null;
}
} catch (e) {
console.log('未找到svg circle, data-id = ' + d.stopNo + "_" + d.upDown);
svg_circle = null;
}
// 计算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;
})
.attr('updown', function(d) {
return d.upDown;
});
// 绘制文本
svg.select('.gps-wrap').selectAll('text')
.data(test_gps_data, function(d) {
return d.deviceId; // 指定主键,默认使用数组下标
})
.enter()
.append('text')
.attr('_id', function(d) {
return "tx_" + d.deviceId;
})
.transition()
.attr('x', function(d) {
// 查找对应的circle
var svg_circle;
try {
svg_circle = $jQuery('.station_circle[data-id=' + d.stopNo + "_" + d.upDown + ']');
if (svg_circle.length == 0) {
svg_circle = null;
}
} catch (e) {
console.log('未找到svg circle, data-id = ' + d.stopNo + "_" + d.upDown);
svg_circle = null;
}
// 计算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;
})
.attr('y', function(d) {
// 查找对应的circle
var svg_circle;
try {
svg_circle = $jQuery('.station_circle[data-id=' + d.stopNo + "_" + d.upDown + ']');
if (svg_circle.length == 0) {
svg_circle = null;
}
} catch (e) {
console.log('未找到svg circle, data-id = ' + d.stopNo + "_" + d.upDown);
svg_circle = null;
}
// 计算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;
})
.attr('updown', function(d) {
return d.upDown;
})
.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;
}
});
// 绘制可能合并的rect
$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;
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;
}
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');
});
// 创建 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);
});
}(jQuery.noConflict()));