@hpcc-js/map 2.79.3 → 2.79.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE +43 -43
  2. package/README.md +88 -88
  3. package/TopoJSON/BR.json +122 -122
  4. package/TopoJSON/GB_idx.json +1 -1
  5. package/TopoJSON/IE_idx.json +1 -1
  6. package/TopoJSON/ND_idx.json +1 -1
  7. package/TopoJSON/countries.json +257 -257
  8. package/TopoJSON/us-counties.json +16550 -16550
  9. package/TopoJSON/us-states.json +458 -458
  10. package/dist/index.es6.js +49 -49
  11. package/dist/index.es6.js.map +1 -1
  12. package/dist/index.js +49 -49
  13. package/dist/index.js.map +1 -1
  14. package/dist/index.min.js +1 -1
  15. package/dist/index.min.js.map +1 -1
  16. package/package.json +7 -7
  17. package/src/CanvasPinLayer.ts +99 -99
  18. package/src/CanvasPins.ts +396 -396
  19. package/src/Choropleth.css +27 -27
  20. package/src/Choropleth.ts +195 -195
  21. package/src/ChoroplethContinents.ts +13 -13
  22. package/src/ChoroplethCounties.ts +109 -109
  23. package/src/ChoroplethCountries.ts +100 -100
  24. package/src/ChoroplethStates.ts +104 -104
  25. package/src/ChoroplethStatesHeat.ts +8 -8
  26. package/src/GMap.css +16 -16
  27. package/src/GMap.ts +879 -879
  28. package/src/GMapCounties.ts +93 -93
  29. package/src/GMapGraph.ts +61 -61
  30. package/src/GMapHeat.ts +27 -27
  31. package/src/GMapLayered.ts +94 -94
  32. package/src/GMapPin.ts +115 -115
  33. package/src/GMapPinLine.ts +125 -125
  34. package/src/GeoHash.css +15 -15
  35. package/src/GeoHash.ts +125 -125
  36. package/src/Graph.css +10 -10
  37. package/src/Graph.ts +98 -98
  38. package/src/Graticule.css +13 -13
  39. package/src/Graticule.ts +90 -90
  40. package/src/Heat.css +2 -2
  41. package/src/Heat.ts +82 -82
  42. package/src/IChoropleth.ts +8 -8
  43. package/src/Layer.ts +90 -90
  44. package/src/Layered.css +19 -19
  45. package/src/Layered.ts +206 -206
  46. package/src/Lines.css +9 -9
  47. package/src/Lines.ts +75 -75
  48. package/src/OpenStreet.css +15 -15
  49. package/src/OpenStreet.ts +123 -123
  50. package/src/Pins.css +18 -18
  51. package/src/Pins.ts +319 -319
  52. package/src/Projection.ts +42 -42
  53. package/src/TestHeatMap.ts +8 -8
  54. package/src/TopoJSONChoropleth.ts +114 -114
  55. package/src/Utility.ts +482 -482
  56. package/src/__package__.ts +3 -3
  57. package/src/index.ts +34 -34
  58. package/src/leaflet/AlbersPR.ts +48 -48
  59. package/src/leaflet/Blank.ts +9 -9
  60. package/src/leaflet/Circles.ts +139 -139
  61. package/src/leaflet/ClusterCircles.css +26 -26
  62. package/src/leaflet/ClusterCircles.ts +88 -88
  63. package/src/leaflet/Countries.ts +43 -43
  64. package/src/leaflet/DrawLayer.ts +167 -167
  65. package/src/leaflet/FeatureLayer.ts +138 -138
  66. package/src/leaflet/GMap.ts +44 -44
  67. package/src/leaflet/HeatLayer.ts +77 -77
  68. package/src/leaflet/Icons.ts +60 -60
  69. package/src/leaflet/Leaflet.css +3 -3
  70. package/src/leaflet/Leaflet.ts +238 -238
  71. package/src/leaflet/MapBox.ts +35 -35
  72. package/src/leaflet/Markers.ts +109 -109
  73. package/src/leaflet/OpenStreet.ts +27 -27
  74. package/src/leaflet/Path.ts +138 -138
  75. package/src/leaflet/Pins.ts +73 -73
  76. package/src/leaflet/Polygons.ts +113 -113
  77. package/src/leaflet/Region.ts +138 -138
  78. package/src/leaflet/Text.ts +99 -99
  79. package/src/leaflet/TileLayer.ts +81 -81
  80. package/src/leaflet/TopoJSON.ts +146 -146
  81. package/src/leaflet/US.ts +15 -15
  82. package/src/leaflet/USCounties.ts +43 -43
  83. package/src/leaflet/USStates.ts +41 -41
  84. package/src/leaflet/World.css +3 -3
  85. package/src/leaflet/World.ts +171 -171
  86. package/src/leaflet/index.ts +18 -18
  87. package/src/test.ts +114 -114
  88. package/types/__package__.d.ts +2 -2
  89. package/types-3.4/__package__.d.ts +2 -2
package/src/CanvasPins.ts CHANGED
@@ -1,396 +1,396 @@
1
- import { CanvasWidget } from "@hpcc-js/common";
2
- import { quadtree as d3quadtree } from "d3-quadtree";
3
-
4
- export interface CanvasPinRow {
5
- 0: number;
6
- 1: number;
7
- is_cluster: boolean;
8
- already_flagged: boolean;
9
- weight: number;
10
- fillStyle: string;
11
- strokeStyle: string;
12
- overlap_arr: CanvasPinRow[];
13
- }
14
-
15
- interface Rect {
16
- left: number;
17
- right: number;
18
- top: number;
19
- bottom: number;
20
- }
21
-
22
- class Quadtree {
23
- protected _pin_h = 0;
24
- protected _pin_w = 0;
25
- protected _tree: any;
26
- constructor(extent: any, raw_data: any, pin_h: number, pin_w: number) {
27
- this._pin_h = pin_h;
28
- this._pin_w = pin_w;
29
- this._tree = d3quadtree()
30
- .extent(extent)
31
- .addAll(raw_data);
32
- }
33
-
34
- searchRect(left: number, top: number, right: number, bottom: number): CanvasPinRow[] {
35
- const ret: any = [];
36
- this._tree.visit((node: any, x1: number, y1: number, x2: number, y2: number) => {
37
- let next_exists = false;
38
- do {
39
- if (!node.length) {
40
- if (node.data && !node.data.already_flagged) {
41
- const is_overlapping = overlaps({ left, right, top, bottom }, node.data);
42
- if (is_overlapping) {
43
- node.data.already_flagged = true;
44
- ret.push(node.data);
45
- }
46
- }
47
- }
48
- next_exists = node = node.next;
49
- } while (next_exists);
50
- return x1 >= right || y1 >= bottom || x2 < left || y2 < top;
51
- });
52
- return ret;
53
- function overlaps(r1: Rect, point: CanvasPinRow) {
54
- return (point[0] < r1.right || point[0] > r1.left) && (point[1] < r1.bottom || point[1] > r1.top);
55
- }
56
- }
57
- getTreeRects(): any {
58
- const ret = [];
59
- this._tree.visit((node: any, x1: number, y1: number, x2: number, y2: number) => {
60
- ret.push([x1, y1, x2 - x1, y2 - y1]);
61
- });
62
- return ret;
63
- }
64
- }
65
-
66
- export class CanvasPins extends CanvasWidget {
67
- _ctx;
68
- _quadtree_rect_arr;
69
- _drawData;
70
- _overlap_count = 0;
71
- _sub_overlap_count = 0;
72
-
73
- constructor() {
74
- super();
75
- }
76
-
77
- enter(domNode, element) {
78
- super.enter.apply(this, arguments);
79
- this.resize(this._size);
80
- }
81
-
82
- update(domNode, element) {
83
- super.update.apply(this, arguments);
84
- this._ctx = this.element().node().getContext("2d");
85
- const needs_data_skew = this.topLeftX_exists() && this.topLeftY_exists() && this.bottomRightX_exists() && this.bottomRightY_exists();
86
- const _data = (needs_data_skew ? this.skewedData() : this.data())
87
- .map(row => [...row])
88
- .map(row => {
89
- row.is_cluster = false;
90
- row.already_flagged = false;
91
- row.weight = row[2];
92
- row.fillStyle = "#FFFFFF";
93
- row.strokeStyle = "#000000";
94
- row.overlap_arr = [];
95
- return row;
96
- });
97
- this._ctx.clearRect(0, 0, this.width(), this.height());
98
-
99
- this._drawData = this.enableClustering() ? this.applyClustering(_data) : _data;
100
-
101
- this.draw(this._drawData);
102
- }
103
-
104
- applyClustering(_data: Readonly<CanvasPinRow[]>): Readonly<CanvasPinRow[]> {
105
- const context = this;
106
- this._overlap_count = 0;
107
- const arrow_height = 8;
108
- const pin_h = this.pinHeight();
109
- const pin_w = this.pinWidth();
110
- const half_w = pin_w / 2;
111
- const half_h = pin_h / 2;
112
- const qt = new Quadtree([[0, 0], [this.size().width, this.size().height]], _data, pin_h, pin_w);
113
- this._quadtree_rect_arr = qt.getTreeRects();
114
- switch (this.clusterMode()) {
115
- case "default":
116
- const defData = _data.map(row => {
117
- if (!row.already_flagged) {
118
- const mult = this.searchRectMult();
119
- const left = row[0] - half_w * mult;
120
- const top = row[1] - half_h * mult - arrow_height;
121
- const right = row[0] + half_w * mult;
122
- const bottom = row[1] + half_h * mult - arrow_height;
123
- row.overlap_arr = qt.searchRect(left, top, right, bottom).filter(n => n !== row);
124
- if (row.overlap_arr.length === 0) {
125
- row.already_flagged = false;
126
- }
127
- }
128
- return row;
129
- });
130
- defData.forEach(data_row => {
131
- if (data_row.already_flagged && data_row.overlap_arr.length) {
132
- defData.push(cluster_arr([data_row, ...data_row.overlap_arr]));
133
- }
134
- });
135
- return defData;
136
- case "grid":
137
- const gridData = [..._data];
138
- const grid_cell_w = this.gridCellSize();
139
- const grid_cell_h = this.gridCellSize();
140
- const grid_row_count = Math.ceil(this.size().width / grid_cell_w);
141
- const grid_col_count = Math.ceil(this.size().height / grid_cell_h);
142
- for (let _col = 0; _col < grid_col_count; _col++) {
143
- for (let _row = 0; _row < grid_row_count; _row++) {
144
- const left = grid_cell_w * _row;
145
- const top = grid_cell_h * _col;
146
- const right = grid_cell_w * (_row + 1);
147
- const bottom = grid_cell_h * (_col + 1);
148
- const overlap_arr = qt.searchRect(left, top, right, bottom);
149
- if (overlap_arr.length > 1) {
150
- const x = left + (grid_cell_w / 2);
151
- const y = top + (grid_cell_h / 2);
152
- gridData.push(cluster_arr(overlap_arr, x, y));
153
- }
154
- }
155
- }
156
- return gridData;
157
- }
158
- return _data;
159
-
160
- function cluster_arr(arr: CanvasPinRow[], x?: number, y?: number): CanvasPinRow {
161
- const arr_weight = arr.reduce((a, b) => {
162
- b.already_flagged = true;
163
- return a + b[2];
164
- }, 0);
165
- const _x = typeof x !== "undefined" ? x : context.useAveragePos() ? arr.reduce((a, b) => a + b[0] * b[2], 0) / arr_weight : arr[0][0];
166
- const _y = typeof y !== "undefined" ? y : context.useAveragePos() ? arr.reduce((a, b) => a + b[1] * b[2], 0) / arr_weight : arr[0][1];
167
- return {
168
- 0: _x,
169
- 1: _y,
170
- weight: arr.reduce((a, b) => a + b[2], 0),
171
- is_cluster: true,
172
- already_flagged: false,
173
- fillStyle: "#FFFFFF",
174
- strokeStyle: "#000000",
175
- overlap_arr: []
176
- };
177
- }
178
- }
179
-
180
- drawQuadtree() {
181
- if (!this._quadtree_rect_arr) return;
182
- this._ctx.strokeStyle = "#000000";
183
- this._quadtree_rect_arr.forEach(n => {
184
- this._ctx.strokeRect(n[0], n[1], n[2], n[3]);
185
- });
186
- }
187
- draw(data_arr) {
188
- const context = this;
189
- const ctx = this._ctx;
190
- if (this.showQuadtree()) this.drawQuadtree();
191
- const arrow_height = this.arrowHeight();
192
- const arrow_width = this.arrowWidth();
193
- const pin_h = this.pinHeight();
194
- const pin_w = this.pinWidth();
195
- const weight_bonus: number = this.radiusWeightMult();
196
- let heaviest_cluster = 0;
197
- if (this.useWeightedRadius()) {
198
- data_arr.filter(n => n.is_cluster).forEach(n => {
199
- if (heaviest_cluster < n.weight) {
200
- heaviest_cluster = n.weight;
201
- }
202
- });
203
- }
204
- data_arr
205
- .filter(n => !n.already_flagged)
206
- .sort((a, b) => a[1] > b[1] ? 1 : -1)
207
- .forEach(d => {
208
- if (d.is_cluster || this.allCircles()) {
209
- let _radius = pin_w / 2;
210
- if (this.useWeightedRadius()) {
211
- _radius += pin_w * (weight_bonus * d.weight / heaviest_cluster);
212
- }
213
- drawCirclePin({
214
- icon: d.weight,
215
- left: Math.floor(d[0] - _radius) + 0.5,
216
- top: Math.floor(d[1] - _radius - arrow_height) + 0.5,
217
- radius: _radius,
218
- arrow_height,
219
- arrow_width
220
- });
221
- } else {
222
- drawSquarePin({
223
- fillStyle: d.fillStyle,
224
- strokeStyle: d.strokeStyle,
225
- icon: d.weight,
226
- left: Math.floor(d[0] - (pin_w / 2)) + 0.5,
227
- top: Math.floor(d[1] - pin_h - arrow_height) + 0.5,
228
- width: pin_w,
229
- height: pin_h,
230
- arrow_height,
231
- arrow_width
232
- });
233
- }
234
- });
235
-
236
- function drawCirclePin(p: any) {
237
- p.width = p.radius * 2;
238
- p.height = p.width;
239
- ctx.fillStyle = "#FFFFFF";
240
- ctx.strokeStyle = "#000000";
241
- ctx.beginPath();
242
- ctx.arc(p.left + (p.width / 2), p.top + (p.height / 2), p.radius, 0, 2 * Math.PI);
243
- ctx.closePath();
244
- ctx.fill();
245
- ctx.stroke();
246
- drawPinText(p);
247
- }
248
- function drawSquarePin(p: any) {
249
- ctx.fillStyle = p.fillStyle;
250
- ctx.strokeStyle = "#000000";
251
- ctx.fillRect(p.left, p.top, p.width, p.height);
252
- ctx.strokeRect(p.left, p.top, p.width, p.height);
253
- drawArrow(p);
254
- drawPinText(p);
255
- }
256
- function drawArrow(p: any, is_offset?: boolean) {
257
- const a_x0 = p.left + (p.width / 2) - (p.arrow_width / 2);
258
- const a_x1 = p.left + (p.width / 2);
259
- const a_x2 = p.left + (p.width / 2) + (p.arrow_width / 2);
260
- let a_y0 = p.top + p.height;
261
- let a_y1 = a_y0 + p.arrow_height;
262
- let a_y2 = a_y0;
263
- if (!is_offset) {
264
- ctx.fillStyle = "#FFFFFF";
265
- ctx.strokeStyle = "#000000";
266
- } else {
267
- ctx.fillStyle = "#FFFFFF";
268
- ctx.strokeStyle = "#FFFFFF";
269
- a_y0 -= 2;
270
- a_y1 -= 2;
271
- a_y2 -= 2;
272
- }
273
- ctx.beginPath();
274
- ctx.moveTo(a_x0, a_y0);
275
- ctx.lineTo(a_x1, a_y1);
276
- ctx.lineTo(a_x2, a_y2);
277
- ctx.lineTo(a_x0, a_y0);
278
- ctx.closePath();
279
- ctx.stroke();
280
- ctx.fill();
281
- if (!is_offset) drawArrow(p, true);
282
- }
283
- function drawPinText(p: any) {
284
- ctx.font = `${context.pinFontSize()}px '${context.pinFontFamily()}'`;
285
- const x = p.left + (p.width / 2);
286
- const y = p.top + (p.height / 2);
287
- ctx.textAlign = "center";
288
- ctx.textBaseline = "middle";
289
- ctx.fillStyle = "#000000";
290
- let size_dec = 0;
291
- let txt_w = ctx.measureText(p.icon).width;
292
- while (txt_w > p.width || !context.shrinkFontToPin()) {
293
- size_dec++;
294
- ctx.font = `${context.pinFontSize() - size_dec}px '${context.pinFontFamily()}'`;
295
- txt_w = ctx.measureText(p.icon).width;
296
- }
297
- ctx.fillText(p.icon, x, y);
298
- }
299
- }
300
-
301
- skewedData() {
302
- const context = this;
303
- const retArr = [];
304
- const arr = this.data();
305
- const box = this.size();
306
-
307
- const coordsWidth = this.bottomRightX() - this.topLeftX();
308
- const coordsHeight = this.bottomRightY() - this.topLeftY();
309
-
310
- const pixelValueX = coordsWidth / box.width;
311
- const pixelValueY = coordsHeight / box.height;
312
-
313
- arr.forEach(function (n) {
314
- const left = Math.abs(n[0] - context.topLeftX());
315
- const top = Math.abs(n[1] - context.topLeftY());
316
-
317
- const newX = left / pixelValueX;
318
- const newY = top / pixelValueY;
319
-
320
- retArr.push([newX, newY, n[2]]);
321
- });
322
-
323
- return retArr;
324
- }
325
- }
326
- CanvasPins.prototype._class += " map_CanvasPins";
327
-
328
- export interface CanvasPins {
329
- clusterMode(): any;
330
- clusterMode(_: any): CanvasPins;
331
- allCircles(): boolean;
332
- allCircles(_: boolean): CanvasPins;
333
- showQuadtree(): boolean;
334
- showQuadtree(_: boolean): CanvasPins;
335
- useAveragePos(): boolean;
336
- useAveragePos(_: boolean): CanvasPins;
337
- useWeightedRadius(): boolean;
338
- useWeightedRadius(_: boolean): CanvasPins;
339
- radiusWeightMult(): number;
340
- radiusWeightMult(_: number): CanvasPins;
341
- shrinkFontToPin(): boolean;
342
- shrinkFontToPin(_: boolean): CanvasPins;
343
- enableClustering(): boolean;
344
- enableClustering(_: boolean): CanvasPins;
345
- searchRectMult(): number;
346
- searchRectMult(_: number): CanvasPins;
347
- gridCellSize(): number;
348
- gridCellSize(_: number): CanvasPins;
349
- arrowHeight(): number;
350
- arrowHeight(_: number): CanvasPins;
351
- arrowWidth(): number;
352
- arrowWidth(_: number): CanvasPins;
353
- bottomRightX(): number;
354
- bottomRightX(_: number): CanvasPins;
355
- bottomRightY(): number;
356
- bottomRightY(_: number): CanvasPins;
357
- topLeftX(): number;
358
- topLeftX(_: number): CanvasPins;
359
- topLeftY(): number;
360
- topLeftY(_: number): CanvasPins;
361
- pinHeight(): number;
362
- pinHeight(_: number): CanvasPins;
363
- pinWidth(): number;
364
- pinWidth(_: number): CanvasPins;
365
- pinFontFamily(): string;
366
- pinFontFamily(_: string): CanvasPins;
367
- pinFontSize(): number;
368
- pinFontSize(_: number): CanvasPins;
369
- bottomRightX_exists(): boolean;
370
- bottomRightY_exists(): boolean;
371
- topLeftX_exists(): boolean;
372
- topLeftY_exists(): boolean;
373
- }
374
-
375
- CanvasPins.prototype.publish("clusterMode", "default", "set", "clusterMode", ["defualt", "grid"], { tags: ["Basic"], optional: true });
376
- CanvasPins.prototype.publish("gridCellSize", 80, "number", "gridCellSize", null, { tags: ["Basic"], optional: true });
377
-
378
- CanvasPins.prototype.publish("allCircles", false, "boolean", "allCircles", null, { tags: ["Basic"], optional: true });
379
- CanvasPins.prototype.publish("showQuadtree", false, "boolean", "showQuadtree", null, { tags: ["Basic"], optional: true });
380
- CanvasPins.prototype.publish("useAveragePos", false, "boolean", "useAveragePos", null, { tags: ["Basic"], optional: true });
381
- CanvasPins.prototype.publish("shrinkFontToPin", true, "boolean", "shrinkFontToPin", null, { tags: ["Basic"], optional: true });
382
- CanvasPins.prototype.publish("enableClustering", true, "boolean", "enableClustering", null, { tags: ["Basic"], optional: true });
383
- CanvasPins.prototype.publish("useWeightedRadius", false, "boolean", "useWeightedRadius", null, { tags: ["Basic"], optional: true });
384
- CanvasPins.prototype.publish("radiusWeightMult", 0.5, "number", "radiusWeightMult", null, { tags: ["Basic"], optional: true });
385
- CanvasPins.prototype.publish("searchRectMult", 3, "number", "searchRectMult", null, { tags: ["Basic"], optional: true });
386
- CanvasPins.prototype.publish("bottomRightX", null, "number", "Bottom right x-value", null, { tags: ["Basic"], optional: true });
387
- CanvasPins.prototype.publish("bottomRightY", null, "number", "Bottom right y-value", null, { tags: ["Basic"], optional: true });
388
- CanvasPins.prototype.publish("topLeftX", null, "number", "Top left x-value", null, { tags: ["Basic"], optional: true });
389
- CanvasPins.prototype.publish("topLeftY", null, "number", "Top left y-value", null, { tags: ["Basic"], optional: true });
390
-
391
- CanvasPins.prototype.publish("pinHeight", 20, "number", "pinHeight", null, { tags: ["Basic"], optional: true });
392
- CanvasPins.prototype.publish("pinWidth", 20, "number", "pinWidth", null, { tags: ["Basic"], optional: true });
393
- CanvasPins.prototype.publish("pinFontFamily", "Arial", "string", "pinFontFamily", null, { tags: ["Basic"], optional: true });
394
- CanvasPins.prototype.publish("pinFontSize", 14, "number", "pinFontSize", null, { tags: ["Basic"], optional: true });
395
- CanvasPins.prototype.publish("arrowHeight", 8, "number", "arrowHeight", null, { tags: ["Basic"], optional: true });
396
- CanvasPins.prototype.publish("arrowWidth", 8, "number", "arrowWidth", null, { tags: ["Basic"], optional: true });
1
+ import { CanvasWidget } from "@hpcc-js/common";
2
+ import { quadtree as d3quadtree } from "d3-quadtree";
3
+
4
+ export interface CanvasPinRow {
5
+ 0: number;
6
+ 1: number;
7
+ is_cluster: boolean;
8
+ already_flagged: boolean;
9
+ weight: number;
10
+ fillStyle: string;
11
+ strokeStyle: string;
12
+ overlap_arr: CanvasPinRow[];
13
+ }
14
+
15
+ interface Rect {
16
+ left: number;
17
+ right: number;
18
+ top: number;
19
+ bottom: number;
20
+ }
21
+
22
+ class Quadtree {
23
+ protected _pin_h = 0;
24
+ protected _pin_w = 0;
25
+ protected _tree: any;
26
+ constructor(extent: any, raw_data: any, pin_h: number, pin_w: number) {
27
+ this._pin_h = pin_h;
28
+ this._pin_w = pin_w;
29
+ this._tree = d3quadtree()
30
+ .extent(extent)
31
+ .addAll(raw_data);
32
+ }
33
+
34
+ searchRect(left: number, top: number, right: number, bottom: number): CanvasPinRow[] {
35
+ const ret: any = [];
36
+ this._tree.visit((node: any, x1: number, y1: number, x2: number, y2: number) => {
37
+ let next_exists = false;
38
+ do {
39
+ if (!node.length) {
40
+ if (node.data && !node.data.already_flagged) {
41
+ const is_overlapping = overlaps({ left, right, top, bottom }, node.data);
42
+ if (is_overlapping) {
43
+ node.data.already_flagged = true;
44
+ ret.push(node.data);
45
+ }
46
+ }
47
+ }
48
+ next_exists = node = node.next;
49
+ } while (next_exists);
50
+ return x1 >= right || y1 >= bottom || x2 < left || y2 < top;
51
+ });
52
+ return ret;
53
+ function overlaps(r1: Rect, point: CanvasPinRow) {
54
+ return (point[0] < r1.right || point[0] > r1.left) && (point[1] < r1.bottom || point[1] > r1.top);
55
+ }
56
+ }
57
+ getTreeRects(): any {
58
+ const ret = [];
59
+ this._tree.visit((node: any, x1: number, y1: number, x2: number, y2: number) => {
60
+ ret.push([x1, y1, x2 - x1, y2 - y1]);
61
+ });
62
+ return ret;
63
+ }
64
+ }
65
+
66
+ export class CanvasPins extends CanvasWidget {
67
+ _ctx;
68
+ _quadtree_rect_arr;
69
+ _drawData;
70
+ _overlap_count = 0;
71
+ _sub_overlap_count = 0;
72
+
73
+ constructor() {
74
+ super();
75
+ }
76
+
77
+ enter(domNode, element) {
78
+ super.enter.apply(this, arguments);
79
+ this.resize(this._size);
80
+ }
81
+
82
+ update(domNode, element) {
83
+ super.update.apply(this, arguments);
84
+ this._ctx = this.element().node().getContext("2d");
85
+ const needs_data_skew = this.topLeftX_exists() && this.topLeftY_exists() && this.bottomRightX_exists() && this.bottomRightY_exists();
86
+ const _data = (needs_data_skew ? this.skewedData() : this.data())
87
+ .map(row => [...row])
88
+ .map(row => {
89
+ row.is_cluster = false;
90
+ row.already_flagged = false;
91
+ row.weight = row[2];
92
+ row.fillStyle = "#FFFFFF";
93
+ row.strokeStyle = "#000000";
94
+ row.overlap_arr = [];
95
+ return row;
96
+ });
97
+ this._ctx.clearRect(0, 0, this.width(), this.height());
98
+
99
+ this._drawData = this.enableClustering() ? this.applyClustering(_data) : _data;
100
+
101
+ this.draw(this._drawData);
102
+ }
103
+
104
+ applyClustering(_data: Readonly<CanvasPinRow[]>): Readonly<CanvasPinRow[]> {
105
+ const context = this;
106
+ this._overlap_count = 0;
107
+ const arrow_height = 8;
108
+ const pin_h = this.pinHeight();
109
+ const pin_w = this.pinWidth();
110
+ const half_w = pin_w / 2;
111
+ const half_h = pin_h / 2;
112
+ const qt = new Quadtree([[0, 0], [this.size().width, this.size().height]], _data, pin_h, pin_w);
113
+ this._quadtree_rect_arr = qt.getTreeRects();
114
+ switch (this.clusterMode()) {
115
+ case "default":
116
+ const defData = _data.map(row => {
117
+ if (!row.already_flagged) {
118
+ const mult = this.searchRectMult();
119
+ const left = row[0] - half_w * mult;
120
+ const top = row[1] - half_h * mult - arrow_height;
121
+ const right = row[0] + half_w * mult;
122
+ const bottom = row[1] + half_h * mult - arrow_height;
123
+ row.overlap_arr = qt.searchRect(left, top, right, bottom).filter(n => n !== row);
124
+ if (row.overlap_arr.length === 0) {
125
+ row.already_flagged = false;
126
+ }
127
+ }
128
+ return row;
129
+ });
130
+ defData.forEach(data_row => {
131
+ if (data_row.already_flagged && data_row.overlap_arr.length) {
132
+ defData.push(cluster_arr([data_row, ...data_row.overlap_arr]));
133
+ }
134
+ });
135
+ return defData;
136
+ case "grid":
137
+ const gridData = [..._data];
138
+ const grid_cell_w = this.gridCellSize();
139
+ const grid_cell_h = this.gridCellSize();
140
+ const grid_row_count = Math.ceil(this.size().width / grid_cell_w);
141
+ const grid_col_count = Math.ceil(this.size().height / grid_cell_h);
142
+ for (let _col = 0; _col < grid_col_count; _col++) {
143
+ for (let _row = 0; _row < grid_row_count; _row++) {
144
+ const left = grid_cell_w * _row;
145
+ const top = grid_cell_h * _col;
146
+ const right = grid_cell_w * (_row + 1);
147
+ const bottom = grid_cell_h * (_col + 1);
148
+ const overlap_arr = qt.searchRect(left, top, right, bottom);
149
+ if (overlap_arr.length > 1) {
150
+ const x = left + (grid_cell_w / 2);
151
+ const y = top + (grid_cell_h / 2);
152
+ gridData.push(cluster_arr(overlap_arr, x, y));
153
+ }
154
+ }
155
+ }
156
+ return gridData;
157
+ }
158
+ return _data;
159
+
160
+ function cluster_arr(arr: CanvasPinRow[], x?: number, y?: number): CanvasPinRow {
161
+ const arr_weight = arr.reduce((a, b) => {
162
+ b.already_flagged = true;
163
+ return a + b[2];
164
+ }, 0);
165
+ const _x = typeof x !== "undefined" ? x : context.useAveragePos() ? arr.reduce((a, b) => a + b[0] * b[2], 0) / arr_weight : arr[0][0];
166
+ const _y = typeof y !== "undefined" ? y : context.useAveragePos() ? arr.reduce((a, b) => a + b[1] * b[2], 0) / arr_weight : arr[0][1];
167
+ return {
168
+ 0: _x,
169
+ 1: _y,
170
+ weight: arr.reduce((a, b) => a + b[2], 0),
171
+ is_cluster: true,
172
+ already_flagged: false,
173
+ fillStyle: "#FFFFFF",
174
+ strokeStyle: "#000000",
175
+ overlap_arr: []
176
+ };
177
+ }
178
+ }
179
+
180
+ drawQuadtree() {
181
+ if (!this._quadtree_rect_arr) return;
182
+ this._ctx.strokeStyle = "#000000";
183
+ this._quadtree_rect_arr.forEach(n => {
184
+ this._ctx.strokeRect(n[0], n[1], n[2], n[3]);
185
+ });
186
+ }
187
+ draw(data_arr) {
188
+ const context = this;
189
+ const ctx = this._ctx;
190
+ if (this.showQuadtree()) this.drawQuadtree();
191
+ const arrow_height = this.arrowHeight();
192
+ const arrow_width = this.arrowWidth();
193
+ const pin_h = this.pinHeight();
194
+ const pin_w = this.pinWidth();
195
+ const weight_bonus: number = this.radiusWeightMult();
196
+ let heaviest_cluster = 0;
197
+ if (this.useWeightedRadius()) {
198
+ data_arr.filter(n => n.is_cluster).forEach(n => {
199
+ if (heaviest_cluster < n.weight) {
200
+ heaviest_cluster = n.weight;
201
+ }
202
+ });
203
+ }
204
+ data_arr
205
+ .filter(n => !n.already_flagged)
206
+ .sort((a, b) => a[1] > b[1] ? 1 : -1)
207
+ .forEach(d => {
208
+ if (d.is_cluster || this.allCircles()) {
209
+ let _radius = pin_w / 2;
210
+ if (this.useWeightedRadius()) {
211
+ _radius += pin_w * (weight_bonus * d.weight / heaviest_cluster);
212
+ }
213
+ drawCirclePin({
214
+ icon: d.weight,
215
+ left: Math.floor(d[0] - _radius) + 0.5,
216
+ top: Math.floor(d[1] - _radius - arrow_height) + 0.5,
217
+ radius: _radius,
218
+ arrow_height,
219
+ arrow_width
220
+ });
221
+ } else {
222
+ drawSquarePin({
223
+ fillStyle: d.fillStyle,
224
+ strokeStyle: d.strokeStyle,
225
+ icon: d.weight,
226
+ left: Math.floor(d[0] - (pin_w / 2)) + 0.5,
227
+ top: Math.floor(d[1] - pin_h - arrow_height) + 0.5,
228
+ width: pin_w,
229
+ height: pin_h,
230
+ arrow_height,
231
+ arrow_width
232
+ });
233
+ }
234
+ });
235
+
236
+ function drawCirclePin(p: any) {
237
+ p.width = p.radius * 2;
238
+ p.height = p.width;
239
+ ctx.fillStyle = "#FFFFFF";
240
+ ctx.strokeStyle = "#000000";
241
+ ctx.beginPath();
242
+ ctx.arc(p.left + (p.width / 2), p.top + (p.height / 2), p.radius, 0, 2 * Math.PI);
243
+ ctx.closePath();
244
+ ctx.fill();
245
+ ctx.stroke();
246
+ drawPinText(p);
247
+ }
248
+ function drawSquarePin(p: any) {
249
+ ctx.fillStyle = p.fillStyle;
250
+ ctx.strokeStyle = "#000000";
251
+ ctx.fillRect(p.left, p.top, p.width, p.height);
252
+ ctx.strokeRect(p.left, p.top, p.width, p.height);
253
+ drawArrow(p);
254
+ drawPinText(p);
255
+ }
256
+ function drawArrow(p: any, is_offset?: boolean) {
257
+ const a_x0 = p.left + (p.width / 2) - (p.arrow_width / 2);
258
+ const a_x1 = p.left + (p.width / 2);
259
+ const a_x2 = p.left + (p.width / 2) + (p.arrow_width / 2);
260
+ let a_y0 = p.top + p.height;
261
+ let a_y1 = a_y0 + p.arrow_height;
262
+ let a_y2 = a_y0;
263
+ if (!is_offset) {
264
+ ctx.fillStyle = "#FFFFFF";
265
+ ctx.strokeStyle = "#000000";
266
+ } else {
267
+ ctx.fillStyle = "#FFFFFF";
268
+ ctx.strokeStyle = "#FFFFFF";
269
+ a_y0 -= 2;
270
+ a_y1 -= 2;
271
+ a_y2 -= 2;
272
+ }
273
+ ctx.beginPath();
274
+ ctx.moveTo(a_x0, a_y0);
275
+ ctx.lineTo(a_x1, a_y1);
276
+ ctx.lineTo(a_x2, a_y2);
277
+ ctx.lineTo(a_x0, a_y0);
278
+ ctx.closePath();
279
+ ctx.stroke();
280
+ ctx.fill();
281
+ if (!is_offset) drawArrow(p, true);
282
+ }
283
+ function drawPinText(p: any) {
284
+ ctx.font = `${context.pinFontSize()}px '${context.pinFontFamily()}'`;
285
+ const x = p.left + (p.width / 2);
286
+ const y = p.top + (p.height / 2);
287
+ ctx.textAlign = "center";
288
+ ctx.textBaseline = "middle";
289
+ ctx.fillStyle = "#000000";
290
+ let size_dec = 0;
291
+ let txt_w = ctx.measureText(p.icon).width;
292
+ while (txt_w > p.width || !context.shrinkFontToPin()) {
293
+ size_dec++;
294
+ ctx.font = `${context.pinFontSize() - size_dec}px '${context.pinFontFamily()}'`;
295
+ txt_w = ctx.measureText(p.icon).width;
296
+ }
297
+ ctx.fillText(p.icon, x, y);
298
+ }
299
+ }
300
+
301
+ skewedData() {
302
+ const context = this;
303
+ const retArr = [];
304
+ const arr = this.data();
305
+ const box = this.size();
306
+
307
+ const coordsWidth = this.bottomRightX() - this.topLeftX();
308
+ const coordsHeight = this.bottomRightY() - this.topLeftY();
309
+
310
+ const pixelValueX = coordsWidth / box.width;
311
+ const pixelValueY = coordsHeight / box.height;
312
+
313
+ arr.forEach(function (n) {
314
+ const left = Math.abs(n[0] - context.topLeftX());
315
+ const top = Math.abs(n[1] - context.topLeftY());
316
+
317
+ const newX = left / pixelValueX;
318
+ const newY = top / pixelValueY;
319
+
320
+ retArr.push([newX, newY, n[2]]);
321
+ });
322
+
323
+ return retArr;
324
+ }
325
+ }
326
+ CanvasPins.prototype._class += " map_CanvasPins";
327
+
328
+ export interface CanvasPins {
329
+ clusterMode(): any;
330
+ clusterMode(_: any): CanvasPins;
331
+ allCircles(): boolean;
332
+ allCircles(_: boolean): CanvasPins;
333
+ showQuadtree(): boolean;
334
+ showQuadtree(_: boolean): CanvasPins;
335
+ useAveragePos(): boolean;
336
+ useAveragePos(_: boolean): CanvasPins;
337
+ useWeightedRadius(): boolean;
338
+ useWeightedRadius(_: boolean): CanvasPins;
339
+ radiusWeightMult(): number;
340
+ radiusWeightMult(_: number): CanvasPins;
341
+ shrinkFontToPin(): boolean;
342
+ shrinkFontToPin(_: boolean): CanvasPins;
343
+ enableClustering(): boolean;
344
+ enableClustering(_: boolean): CanvasPins;
345
+ searchRectMult(): number;
346
+ searchRectMult(_: number): CanvasPins;
347
+ gridCellSize(): number;
348
+ gridCellSize(_: number): CanvasPins;
349
+ arrowHeight(): number;
350
+ arrowHeight(_: number): CanvasPins;
351
+ arrowWidth(): number;
352
+ arrowWidth(_: number): CanvasPins;
353
+ bottomRightX(): number;
354
+ bottomRightX(_: number): CanvasPins;
355
+ bottomRightY(): number;
356
+ bottomRightY(_: number): CanvasPins;
357
+ topLeftX(): number;
358
+ topLeftX(_: number): CanvasPins;
359
+ topLeftY(): number;
360
+ topLeftY(_: number): CanvasPins;
361
+ pinHeight(): number;
362
+ pinHeight(_: number): CanvasPins;
363
+ pinWidth(): number;
364
+ pinWidth(_: number): CanvasPins;
365
+ pinFontFamily(): string;
366
+ pinFontFamily(_: string): CanvasPins;
367
+ pinFontSize(): number;
368
+ pinFontSize(_: number): CanvasPins;
369
+ bottomRightX_exists(): boolean;
370
+ bottomRightY_exists(): boolean;
371
+ topLeftX_exists(): boolean;
372
+ topLeftY_exists(): boolean;
373
+ }
374
+
375
+ CanvasPins.prototype.publish("clusterMode", "default", "set", "clusterMode", ["defualt", "grid"], { tags: ["Basic"], optional: true });
376
+ CanvasPins.prototype.publish("gridCellSize", 80, "number", "gridCellSize", null, { tags: ["Basic"], optional: true });
377
+
378
+ CanvasPins.prototype.publish("allCircles", false, "boolean", "allCircles", null, { tags: ["Basic"], optional: true });
379
+ CanvasPins.prototype.publish("showQuadtree", false, "boolean", "showQuadtree", null, { tags: ["Basic"], optional: true });
380
+ CanvasPins.prototype.publish("useAveragePos", false, "boolean", "useAveragePos", null, { tags: ["Basic"], optional: true });
381
+ CanvasPins.prototype.publish("shrinkFontToPin", true, "boolean", "shrinkFontToPin", null, { tags: ["Basic"], optional: true });
382
+ CanvasPins.prototype.publish("enableClustering", true, "boolean", "enableClustering", null, { tags: ["Basic"], optional: true });
383
+ CanvasPins.prototype.publish("useWeightedRadius", false, "boolean", "useWeightedRadius", null, { tags: ["Basic"], optional: true });
384
+ CanvasPins.prototype.publish("radiusWeightMult", 0.5, "number", "radiusWeightMult", null, { tags: ["Basic"], optional: true });
385
+ CanvasPins.prototype.publish("searchRectMult", 3, "number", "searchRectMult", null, { tags: ["Basic"], optional: true });
386
+ CanvasPins.prototype.publish("bottomRightX", null, "number", "Bottom right x-value", null, { tags: ["Basic"], optional: true });
387
+ CanvasPins.prototype.publish("bottomRightY", null, "number", "Bottom right y-value", null, { tags: ["Basic"], optional: true });
388
+ CanvasPins.prototype.publish("topLeftX", null, "number", "Top left x-value", null, { tags: ["Basic"], optional: true });
389
+ CanvasPins.prototype.publish("topLeftY", null, "number", "Top left y-value", null, { tags: ["Basic"], optional: true });
390
+
391
+ CanvasPins.prototype.publish("pinHeight", 20, "number", "pinHeight", null, { tags: ["Basic"], optional: true });
392
+ CanvasPins.prototype.publish("pinWidth", 20, "number", "pinWidth", null, { tags: ["Basic"], optional: true });
393
+ CanvasPins.prototype.publish("pinFontFamily", "Arial", "string", "pinFontFamily", null, { tags: ["Basic"], optional: true });
394
+ CanvasPins.prototype.publish("pinFontSize", 14, "number", "pinFontSize", null, { tags: ["Basic"], optional: true });
395
+ CanvasPins.prototype.publish("arrowHeight", 8, "number", "arrowHeight", null, { tags: ["Basic"], optional: true });
396
+ CanvasPins.prototype.publish("arrowWidth", 8, "number", "arrowWidth", null, { tags: ["Basic"], optional: true });