-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
355 lines (296 loc) · 10.5 KB
/
script.js
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
import {
routes,
icons,
zones
} from './data.js'
import {
tooltip
}/* from ' ./tooltip.esm.js' */ from './tooltip.mjs'
/* import 'core-js/stable';
import 'regenerator-runtime/runtime';
import * as am4core from '@amcharts/amcharts4/core';
import * as am4charts from '@amcharts/amcharts4/charts';
import * as am4plugins_bullets from '@amcharts/amcharts4/plugins/bullets';
import am4themes_animated from '@amcharts/amcharts4/themes/animated';
*/
am4core.ready(function () {
am4core.useTheme(am4themes_animated);
let chart = am4core.create('chartdiv', am4charts.XYChart);
chart.padding(0, 0, 0, 0);
chart.background.fill = am4core.color("#dee3eeff");
let title = chart.tooltipContainer.createChild(am4core.Label);
/* title.text = "TransLink"; */
title.fill = am4core.color("#00254b");
title.fontSize = 25;
title.width = am4core.percent(100);
title.textAlign = "middle";
title.x = 10;
title.y = 10;
function createAxis(list) {
let axis = list.push(new am4charts.ValueAxis());
axis.min = 0;
axis.max = 100;
axis.strictMinMax = true;
axis.renderer.grid.template.disabled = true;
axis.renderer.labels.template.disabled = true;
axis.renderer.baseGrid.disabled = true;
}
createAxis(chart.xAxes);
createAxis(chart.yAxes);
function createLine(name, color, main, icon) {
let series = chart.series.push(new am4charts.LineSeries());
series.data = main;
series.name = name;
//series.color = color;
series.dataFields.valueX = "x";
series.dataFields.valueY = "y";
series.dummyData = {
icon: icon
}
series.strokeWidth = 6;
series.connect = false;
series.stroke = color;
series.tooltip.pointerOrientation = "vertical";
series.tooltip.autoTextColor = false;
series.tooltip.fontFamily = 'Arial '
series.tooltip.fontWeight = 'bold'
series.tooltip.dy = -7.5
series.tooltip.getFillFromObject = false;
series.tooltip.background.strokeWidth = 0
series.tooltip.label.paddingTop = 0
series.tooltip.label.paddingBottom = 0
series.tooltip.label.paddingLeft = 0
series.tooltip.background.fill = am4core.color("#052e51ff");//background border
series.tooltip.label.paddingRight = 0/*
series.tooltip.background.stroke = am4core.color("#052e51ff");//background border
series.tooltip.label.fill = am4core.color("#fff");//text */
series.propertyFields.strokeDasharray = "dash";
let bullet = series.bullets.push(new am4charts.CircleBullet());
bullet.circle.radius = 4;
bullet.circle.fill = color
/* bullet.circle.fill = am4core.color("#052e51ff"); */
bullet.circle.stroke = am4core.color("#fff");
bullet.circle.strokeWidth = 2;
/* need to check if there is data for tooltip to render */
if (series.data[0] && series.data[0].stationCode !== undefined) {
/* need to set both tooltiptext and tooltiphtml,
the tooltiphtml isnt compatable with older browsers
so tooltiptext is fallback */
bullet.circle.tooltipText = tooltip.text;
bullet.tooltipHTML = tooltip.html;
} else {
bullet.tooltipHTML = tooltip.htmlBasic;
}
bullet.circle.strokeOpacity = .8;
return series
}
function createPathingLine(name, color, data) {
let series = chart.series.push(new am4charts.LineSeries());
series.data = data;
series.name = name;
series.hiddenInLegend = true;
series.dataFields.valueX = "x";
series.dataFields.valueY = "y";
series.stroke = color;
series.strokeWidth = 6;
series.connect = false;
series.propertyFields.strokeDasharray = "dash";
return series
}
function createZoneLine(color, data) {
let series = chart.series.push(new am4charts.LineSeries());
series.data = data;
series.hiddenInLegend = true;
series.dataFields.valueX = "x";
series.dataFields.valueY = "y";
series.stroke = color;
series.strokeWidth = 6;
series.connect = false;
series.propertyFields.strokeDasharray = "dash";
}
function createConnector(data) {
// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.data = data;
series.hiddenInLegend = true;
// Set up binding to data
series.dataFields.valueX = "x";
series.dataFields.valueY = "y";
// Set up appearance
series.stroke = am4core.color("#999");
series.strokeWidth = 12;
series.connect = false;
// Add bullets (stations)
var bullet = series.bullets.push(new am4charts.CircleBullet());
bullet.circle.radius = 8;
bullet.circle.fill = am4core.color("#fff");
bullet.circle.stroke = am4core.color("#999");
bullet.circle.strokeWidth = 2;
series.tooltip.pointerOrientation = "vertical";
series.tooltip.autoTextColor = false;
series.tooltip.fontFamily = 'Arial '
series.tooltip.fontWeight = 'bold'
series.tooltip.dy = -7.5
series.tooltip.getFillFromObject = false;
series.tooltip.background.strokeWidth = 0
series.tooltip.label.paddingTop = 0
series.tooltip.label.paddingBottom = 0
series.tooltip.label.paddingLeft = 0
series.tooltip.label.paddingRight = 0
// series.tooltip.background.fill = am4core.color("#052e51ff");//background border
// series.tooltip.label.fill = am4core.color("#fff");//text
/* need to check if there is data for tooltip to render */
if (series.data[0] && series.data[0].stationCode !== undefined) {
/* need to set both tooltiptext and tooltiphtml,
the tooltiphtml isnt compatable with older browsers
so tooltiptext is fallback */
bullet.circle.tooltipText = tooltip.text;
bullet.tooltipHTML = tooltip.html
} else {
bullet.circle.tooltipText = '{station}'
}
bullet.circle.strokeOpacity = .8;
return series
}
function createIconPin(color, data, radius) {
let series = chart.series.push(new am4charts.LineSeries());
series.data = data;
series.dataFields.valueX = "x";
series.dataFields.valueY = "y";
series.hiddenInLegend = true;
series.stroke = color
series.strokeWidth = 1;
series.connect = false;
series.propertyFields.strokeDasharray = "dash";
let icon = series.bullets.push(new am4plugins_bullets.PinBullet());
icon.locationX = 1;
/* icon.stroke = am4core.color('#fff'); */
icon.stroke = am4core.color('#00000000');
icon.background.fill = color
icon.background.radius = radius
icon.background.pointerBaseWidth = 10
icon.background.pointerLength = 10
icon.background.propertyFields.pointerAngle = 'angle'
icon.image = new am4core.Image();
icon.image.propertyFields.href = 'icon'
icon.image.scale = .7;
icon.circle.radius = am4core.percent(100);
return series
}
function createBullet(label) {
let series = chart.series.push(new am4charts.LineSeries());
/* let {
size,
data,
angle,
align,
icon
} = {
label
} */
let size = label.size
let data = label.data
let angle = label.angle
let align = label.align
let icon = label.icon
let [horizontal, vertical] = [align[0], align[1]]
series.data = data;
series.dataFields.valueX = "x";
series.dataFields.valueY = "y";
series.hiddenInLegend = true;
let bullet = series.bullets.push(new am4charts.Bullet());
let routeLabel = bullet.createChild(am4core.Image);
routeLabel.width = size;
routeLabel.height = size;
routeLabel.horizontalCenter = horizontal;
routeLabel.verticalCenter = vertical;
routeLabel.rotation = angle;
routeLabel.href = icon;
return series
}
const buildRoutes = (routes) => {
let seriesMap = {};
let masterSeries = createLine('Toggle All', null, null, 'cycle.png')
routes.forEach(route => {
const [name, color, main, icon, pathing, connectors, icons, label] = [
route.name,
route.color,
route.main,
route.icon,
route.pathing,
route.connectors,
route.icons,
route.label
]
seriesMap[name] = []
/* pathing line is needed for any bend in the
line that doesnt have a station/stop while create
line is the main route with bullets for stations */
/* pairing the pathing line with the main route so that
the user can toggle both lines from a single button in the
legend */
// label !== undefined && seriesMap[name].push(createBullet( label.size, label.data, label.angle, label.icon))
seriesMap[name].push(createLine(name, color, main, icon))
seriesMap[name].push(createPathingLine(name, color, pathing))
label !== undefined && seriesMap[name].push(createBullet(label))
connectors !== undefined && seriesMap[name].push(createConnector(connectors))
icons !== undefined && icons.forEach(icon => {
seriesMap[name].push(createIconPin(icon.color, icon.data, icon.radius))
})
//create a master toggle button
masterSeries.events.on('hidden', () => {
seriesMap[name][0].hide();
})
masterSeries.events.on('shown', () => {
seriesMap[name][0].show();
})
//merge functionality for each route with all pathing and icons
seriesMap[name][0].events.on('hidden', () => {
seriesMap[name].forEach(series => {
series.hide()
});
});
seriesMap[name][0].events.on('shown', () => {
seriesMap[name].forEach(series => {
series.show()
});
});
});
icons.forEach(icon => {
icon
createIconPin(icon.color, icon.data, icon.radius)
})
zones.forEach(zone => {
createZoneLine(zone.color, zone.data)
})
/* createConnector(connectors) */
}
buildRoutes(routes)
chart.legend = new am4charts.Legend();
chart.legend.position = "right";
chart.legend.useDefaultMarker = true;
chart.legend.labels.template.text = "[bold ]{name}[/]"; //{color}
chart.legend.background.fill = am4core.color("#dee3eeff");
let marker = chart.legend.markers.template;
marker.disposeChildren();
marker.width = 20;
marker.height = 20;
let icon = marker.createChild(am4core.Image);
icon.width = 20;
icon.height = 20;
icon.adapter.add("href", function (href, target) {
if (
target.dataItem &&
target.dataItem.dataContext &&
target.dataItem.dataContext.dummyData) {
console.log(target.dataItem)
return target.dataItem.dataContext.dummyData.icon;
} else {
return href;
}
});
let bg = chart.plotContainer.createChild(am4core.Image);
bg.width = am4core.percent(100);
bg.height = am4core.percent(100);
bg.href = 'transitMapCleaned2.svg'
});