@geoblocks/elevation-profile 0.0.2 → 0.0.3

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.
@@ -0,0 +1,564 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html>
4
+ <head>
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
6
+ <script src="/index.600fa9d1.js" defer=""></script>
7
+ <style>body {
8
+ margin: 0;
9
+ }
10
+
11
+ .container {
12
+ margin: 50px;
13
+ position: relative;
14
+ }
15
+
16
+ #tooltip {
17
+ color: #fff;
18
+ background-color: #707070;
19
+ padding: 10px;
20
+ display: none;
21
+ position: absolute;
22
+ transform: translate(-50%, -100%);
23
+ }
24
+
25
+ elevation-profile {
26
+ width: 80%;
27
+ height: 300px;
28
+ display: block;
29
+ }
30
+
31
+ elevation-profile .grid.x {
32
+ display: none;
33
+ }
34
+
35
+ elevation-profile line {
36
+ shape-rendering: crispedges;
37
+ }
38
+
39
+ elevation-profile .axis .domain, .grid .domain {
40
+ display: none;
41
+ }
42
+
43
+ elevation-profile .axis line, .grid line {
44
+ stroke: #707070;
45
+ }
46
+
47
+ elevation-profile .pointer-line {
48
+ stroke: #707070;
49
+ stroke-opacity: .3;
50
+ stroke-width: 2px;
51
+ stroke-dasharray: 3 3;
52
+ }
53
+
54
+ elevation-profile .pointer-circle {
55
+ r: 6;
56
+ fill: #3761a4;
57
+ stroke: #fff;
58
+ stroke-width: 3px;
59
+ }
60
+
61
+ elevation-profile .elevation, elevation-profile .elevation.highlight {
62
+ stroke: #3761a4;
63
+ stroke-width: 2px;
64
+ stroke-linejoin: round;
65
+ }
66
+
67
+ elevation-profile .elevation.highlight {
68
+ stroke-width: 3px;
69
+ }
70
+
71
+ elevation-profile .area {
72
+ fill: #e6e6e6;
73
+ fill-opacity: .85;
74
+ }
75
+
76
+ </style>
77
+ </head>
78
+ <body>
79
+ <div class="container">
80
+ <div id="tooltip"></div>
81
+
82
+ <elevation-profile></elevation-profile>
83
+ </div>
84
+ <button id="set-lines">set lines</button>
85
+ <button id="reset-lines">reset lines</button>
86
+
87
+ <pre id="logs"></pre>
88
+
89
+ <script>const profile = document.querySelector("elevation-profile");
90
+ const tooltip = document.querySelector("#tooltip");
91
+ const logs = document.querySelector("#logs");
92
+ const setLines = document.querySelector("#set-lines");
93
+ const resetLines = document.querySelector("#reset-lines");
94
+ profile.margin = {
95
+ top: 20,
96
+ right: 20,
97
+ bottom: 30,
98
+ left: 100
99
+ };
100
+ setLines.addEventListener("click", ()=>{
101
+ profile.lines = [
102
+ [
103
+ 746545,
104
+ 5900651,
105
+ 486,
106
+ 0
107
+ ],
108
+ [
109
+ 746546,
110
+ 5900647,
111
+ 486,
112
+ 2
113
+ ],
114
+ [
115
+ 746564,
116
+ 5900623,
117
+ 486,
118
+ 23
119
+ ],
120
+ [
121
+ 746575,
122
+ 5900607,
123
+ 486,
124
+ 36
125
+ ],
126
+ [
127
+ 746597,
128
+ 5900576,
129
+ 486,
130
+ 62
131
+ ],
132
+ [
133
+ 746608,
134
+ 5900563,
135
+ 487,
136
+ 74
137
+ ],
138
+ [
139
+ 746615,
140
+ 5900565,
141
+ 487,
142
+ 78
143
+ ],
144
+ [
145
+ 746618,
146
+ 5900565,
147
+ 487,
148
+ 81
149
+ ],
150
+ [
151
+ 746624,
152
+ 5900561,
153
+ 486,
154
+ 85
155
+ ],
156
+ [
157
+ 746630,
158
+ 5900560,
159
+ 485,
160
+ 90
161
+ ],
162
+ [
163
+ 746636,
164
+ 5900560,
165
+ 485,
166
+ 94
167
+ ],
168
+ [
169
+ 746640,
170
+ 5900561,
171
+ 485,
172
+ 97
173
+ ],
174
+ [
175
+ 746647,
176
+ 5900566,
177
+ 484,
178
+ 103
179
+ ],
180
+ [
181
+ 746653,
182
+ 5900573,
183
+ 484,
184
+ 108
185
+ ],
186
+ [
187
+ 746661,
188
+ 5900584,
189
+ 484,
190
+ 118
191
+ ],
192
+ [
193
+ 746676,
194
+ 5900573,
195
+ 485,
196
+ 131
197
+ ],
198
+ [
199
+ 746695,
200
+ 5900558,
201
+ 487,
202
+ 147
203
+ ],
204
+ [
205
+ 746707,
206
+ 5900548,
207
+ 490,
208
+ 158
209
+ ],
210
+ [
211
+ 746711,
212
+ 5900545,
213
+ 490,
214
+ 161
215
+ ],
216
+ [
217
+ 746713,
218
+ 5900539,
219
+ 490,
220
+ 166
221
+ ],
222
+ [
223
+ 746718,
224
+ 5900540,
225
+ 491,
226
+ 170
227
+ ],
228
+ [
229
+ 746730,
230
+ 5900543,
231
+ 491,
232
+ 178
233
+ ],
234
+ [
235
+ 746738,
236
+ 5900547,
237
+ 491,
238
+ 184
239
+ ],
240
+ [
241
+ 746750,
242
+ 5900553,
243
+ 492,
244
+ 193
245
+ ],
246
+ [
247
+ 746763,
248
+ 5900561,
249
+ 492,
250
+ 204
251
+ ],
252
+ [
253
+ 746768,
254
+ 5900566,
255
+ 492,
256
+ 209
257
+ ],
258
+ [
259
+ 746773,
260
+ 5900563,
261
+ 493,
262
+ 213
263
+ ],
264
+ [
265
+ 746776,
266
+ 5900563,
267
+ 493,
268
+ 215
269
+ ],
270
+ [
271
+ 746783,
272
+ 5900565,
273
+ 494,
274
+ 220
275
+ ],
276
+ [
277
+ 746787,
278
+ 5900568,
279
+ 494,
280
+ 224
281
+ ],
282
+ [
283
+ 746795,
284
+ 5900573,
285
+ 495,
286
+ 230
287
+ ],
288
+ [
289
+ 746800,
290
+ 5900576,
291
+ 496,
292
+ 234
293
+ ],
294
+ [
295
+ 746803,
296
+ 5900576,
297
+ 496,
298
+ 236
299
+ ],
300
+ [
301
+ 746810,
302
+ 5900578,
303
+ 498,
304
+ 241
305
+ ],
306
+ [
307
+ 746820,
308
+ 5900579,
309
+ 499,
310
+ 248
311
+ ],
312
+ [
313
+ 746831,
314
+ 5900579,
315
+ 500,
316
+ 255
317
+ ],
318
+ [
319
+ 746840,
320
+ 5900578,
321
+ 501,
322
+ 261
323
+ ],
324
+ [
325
+ 746846,
326
+ 5900576,
327
+ 502,
328
+ 266
329
+ ],
330
+ [
331
+ 746851,
332
+ 5900574,
333
+ 502,
334
+ 269
335
+ ],
336
+ [
337
+ 746854,
338
+ 5900573,
339
+ 502,
340
+ 272
341
+ ],
342
+ [
343
+ 746858,
344
+ 5900568,
345
+ 503,
346
+ 276
347
+ ],
348
+ [
349
+ 746860,
350
+ 5900565,
351
+ 503,
352
+ 279
353
+ ],
354
+ [
355
+ 746862,
356
+ 5900560,
357
+ 504,
358
+ 282
359
+ ],
360
+ [
361
+ 746863,
362
+ 5900545,
363
+ 505,
364
+ 292
365
+ ],
366
+ [
367
+ 746865,
368
+ 5900530,
369
+ 506,
370
+ 302
371
+ ],
372
+ [
373
+ 746868,
374
+ 5900509,
375
+ 508,
376
+ 317
377
+ ],
378
+ [
379
+ 746869,
380
+ 5900498,
381
+ 509,
382
+ 325
383
+ ],
384
+ [
385
+ 746872,
386
+ 5900488,
387
+ 509,
388
+ 332
389
+ ],
390
+ [
391
+ 746878,
392
+ 5900477,
393
+ 510,
394
+ 341
395
+ ],
396
+ [
397
+ 746882,
398
+ 5900469,
399
+ 511,
400
+ 347
401
+ ],
402
+ [
403
+ 746891,
404
+ 5900456,
405
+ 512,
406
+ 358
407
+ ],
408
+ [
409
+ 746899,
410
+ 5900446,
411
+ 513,
412
+ 366
413
+ ],
414
+ [
415
+ 746910,
416
+ 5900430,
417
+ 515,
418
+ 380
419
+ ],
420
+ [
421
+ 746931,
422
+ 5900402,
423
+ 517,
424
+ 403
425
+ ],
426
+ [
427
+ 746935,
428
+ 5900394,
429
+ 518,
430
+ 410
431
+ ],
432
+ [
433
+ 746947,
434
+ 5900368,
435
+ 520,
436
+ 429
437
+ ],
438
+ [
439
+ 746953,
440
+ 5900352,
441
+ 522,
442
+ 441
443
+ ],
444
+ [
445
+ 746956,
446
+ 5900350,
447
+ 522,
448
+ 443
449
+ ],
450
+ [
451
+ 746958,
452
+ 5900350,
453
+ 522,
454
+ 445
455
+ ],
456
+ [
457
+ 746963,
458
+ 5900352,
459
+ 522,
460
+ 449
461
+ ],
462
+ [
463
+ 746942,
464
+ 5900413,
465
+ 520,
466
+ 493
467
+ ],
468
+ [
469
+ 746933,
470
+ 5900436,
471
+ 519,
472
+ 510
473
+ ],
474
+ [
475
+ 746920,
476
+ 5900474,
477
+ 517,
478
+ 537
479
+ ],
480
+ [
481
+ 746912,
482
+ 5900496,
483
+ 517,
484
+ 554
485
+ ],
486
+ [
487
+ 746908,
488
+ 5900513,
489
+ 516,
490
+ 565
491
+ ],
492
+ [
493
+ 746905,
494
+ 5900524,
495
+ 515,
496
+ 573
497
+ ],
498
+ [
499
+ 746904,
500
+ 5900535,
501
+ 515,
502
+ 581
503
+ ],
504
+ [
505
+ 746903,
506
+ 5900547,
507
+ 514,
508
+ 589
509
+ ],
510
+ [
511
+ 746903,
512
+ 5900556,
513
+ 514,
514
+ 595
515
+ ],
516
+ [
517
+ 746903,
518
+ 5900568,
519
+ 514,
520
+ 603
521
+ ],
522
+ [
523
+ 746904,
524
+ 5900579,
525
+ 513,
526
+ 611
527
+ ],
528
+ [
529
+ 746907,
530
+ 5900591,
531
+ 513,
532
+ 619
533
+ ],
534
+ [
535
+ 746908,
536
+ 5900599,
537
+ 512,
538
+ 624
539
+ ],
540
+ [
541
+ 746909,
542
+ 5900604,
543
+ 512,
544
+ 628
545
+ ]
546
+ ];
547
+ });
548
+ resetLines.addEventListener("click", ()=>{
549
+ profile.lines = [];
550
+ });
551
+ profile.addEventListener("over", (event)=>{
552
+ logs.textContent = JSON.stringify(event.detail, null, 2);
553
+ tooltip.style.display = "block";
554
+ tooltip.textContent = `${event.detail.coordinate[2]} m`;
555
+ tooltip.style.left = `${event.detail.position.x}px`;
556
+ });
557
+ profile.addEventListener("out", (event)=>{
558
+ logs.textContent = "";
559
+ tooltip.style.display = "none";
560
+ });
561
+
562
+ </script>
563
+ </body>
564
+ </html>
@@ -2,6 +2,7 @@ import {LitElement, svg} from 'lit';
2
2
  import {customElement, state, property} from 'lit/decorators.js';
3
3
  import {ResizeController} from '@lit-labs/observers/resize-controller.js';
4
4
  import {createRef, ref} from 'lit/directives/ref.js';
5
+ import type {PropertyValues} from 'lit';
5
6
 
6
7
  import {extent, bisector} from 'd3-array';
7
8
  import {scaleLinear} from 'd3-scale';
@@ -9,6 +10,7 @@ import {line, area} from 'd3-shape';
9
10
  import {axisBottom, axisLeft} from 'd3-axis';
10
11
  import {select, pointer} from 'd3-selection';
11
12
 
13
+ type PlotPoint = number[];
12
14
 
13
15
  @customElement('elevation-profile')
14
16
  export class ElevationProfile extends LitElement {
@@ -19,22 +21,22 @@ export class ElevationProfile extends LitElement {
19
21
  @state() pointer = {x: 0, y: 0};
20
22
  private resizeController = new ResizeController(this, {});
21
23
 
22
- private plotData: number[][] = [];
24
+ private plotData: PlotPoint[] = [];
23
25
  private scaleX = scaleLinear();
24
26
  private scaleY = scaleLinear();
25
27
 
26
- private bisectDistance = bisector((data) => data[0]).left;
28
+ private bisectDistance = bisector((point: PlotPoint) => point[0]);
27
29
 
28
30
  private line = line()
29
- .x((point) => this.scaleX(point[0]))
30
- .y((point) => this.scaleY(point[1]));
31
+ .x((point: PlotPoint) => this.scaleX(point[0]))
32
+ .y((point: PlotPoint) => this.scaleY(point[1]));
31
33
  private area = area()
32
- .x((point) => this.scaleX(point[0]))
33
- .y1((point) => this.scaleY(point[1]));
34
+ .x((point: PlotPoint) => this.scaleX(point[0]))
35
+ .y1((point: PlotPoint) => this.scaleY(point[1]));
34
36
  private xAxis = axisBottom(this.scaleX)
35
- .tickFormat((i) => i + ' m');
37
+ .tickFormat((val: number) => val + ' m');
36
38
  private yAxis = axisLeft(this.scaleY)
37
- .tickFormat((i) => i + ' m');
39
+ .tickFormat((val: number) => val + ' m');
38
40
  private xGrid = axisBottom(this.scaleX).tickFormat(() => '');
39
41
  private yGrid = axisLeft(this.scaleY).tickFormat(() => '');
40
42
 
@@ -43,16 +45,20 @@ export class ElevationProfile extends LitElement {
43
45
  private yGridRef = createRef();
44
46
  private xGridRef = createRef();
45
47
 
46
- willUpdate(changedProperties) {
48
+ override willUpdate(changedProperties: PropertyValues) {
47
49
  if (changedProperties.has('lines')) {
48
50
  this.plotData = this.lines.map((coordinate) => [coordinate[3], coordinate[2]]);
49
51
 
50
- this.scaleX.domain(extent(this.plotData, (data) => data[0]));
51
- this.scaleY.domain(extent(this.plotData, (data) => data[1])).nice();
52
+ this.scaleX.domain(extent(this.plotData, (data: PlotPoint) => data[0]));
53
+ this.scaleY.domain(extent(this.plotData, (data: PlotPoint) => data[1])).nice();
52
54
  }
53
55
  }
54
56
 
55
- render() {
57
+ override shouldUpdate(): boolean {
58
+ return this.lines.length > 0;
59
+ }
60
+
61
+ override render() {
56
62
  const width = this.offsetWidth;
57
63
  const height = this.offsetHeight;
58
64
 
@@ -86,7 +92,7 @@ export class ElevationProfile extends LitElement {
86
92
  <path class="elevation" d="${this.line(this.plotData)}" fill="none" />
87
93
  <g style="visibility: ${this.pointer.x > 0 ? 'visible' : 'hidden'}">
88
94
  <path class="elevation highlight" d="${this.line(this.plotData)}" fill="none"
89
- clip-path="polygon(0 0, ${this.pointer.x - 40} 0, ${this.pointer.x - 40} 100%, 0 100%)"
95
+ clip-path="polygon(0 0, ${this.pointer.x - this.margin.left} 0, ${this.pointer.x - this.margin.left} 100%, 0 100%)"
90
96
  />
91
97
  <line
92
98
  class="pointer-line y"
@@ -109,9 +115,14 @@ export class ElevationProfile extends LitElement {
109
115
  `;
110
116
  }
111
117
 
118
+ override firstUpdated() {
119
+ // FIXME: because the ref element are used before render is done, we need to force an update
120
+ this.requestUpdate();
121
+ }
122
+
112
123
  private pointerMove(event: PointerEvent) {
113
124
  const pointerDistance = this.scaleX.invert(pointer(event)[0]);
114
- const index = Math.min(this.bisectDistance(this.plotData, pointerDistance), this.plotData.length - 1);
125
+ const index = Math.min(this.bisectDistance.left(this.plotData, pointerDistance), this.plotData.length - 1);
115
126
  // FIXME:
116
127
  // var d0 = data[i - 1]
117
128
  // var d1 = data[i];
@@ -143,7 +154,7 @@ export class ElevationProfile extends LitElement {
143
154
  this.dispatchEvent(new CustomEvent('out'));
144
155
  }
145
156
 
146
- createRenderRoot() {
157
+ override createRenderRoot() {
147
158
  return this;
148
159
  }
149
160
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geoblocks/elevation-profile",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "license": "BSD-3-Clause",
5
5
  "repository": "github:geoblocks/elevation-profile",
6
6
  "type": "module",