@geoblocks/elevation-profile 0.0.3 → 0.0.4

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.
package/dist/index.html CHANGED
@@ -83,6 +83,7 @@ elevation-profile .area {
83
83
  </div>
84
84
  <button id="set-lines">set lines</button>
85
85
  <button id="reset-lines">reset lines</button>
86
+ <button id="save">save</button>
86
87
 
87
88
  <pre id="logs"></pre>
88
89
 
@@ -91,12 +92,20 @@ const tooltip = document.querySelector("#tooltip");
91
92
  const logs = document.querySelector("#logs");
92
93
  const setLines = document.querySelector("#set-lines");
93
94
  const resetLines = document.querySelector("#reset-lines");
94
- profile.margin = {
95
- top: 20,
96
- right: 20,
97
- bottom: 30,
98
- left: 100
99
- };
95
+ const save = document.querySelector("#save");
96
+ save.addEventListener("click", ()=>{
97
+ const svg = profile.innerHTML;
98
+ const blob = new Blob([
99
+ svg
100
+ ], {
101
+ type: "image/svg+xml"
102
+ });
103
+ const url = URL.createObjectURL(blob);
104
+ const a = document.createElement("a");
105
+ a.href = url;
106
+ a.download = "elevation.svg";
107
+ a.click();
108
+ });
100
109
  setLines.addEventListener("click", ()=>{
101
110
  profile.lines = [
102
111
  [
@@ -553,6 +562,7 @@ profile.addEventListener("over", (event)=>{
553
562
  tooltip.style.display = "block";
554
563
  tooltip.textContent = `${event.detail.coordinate[2]} m`;
555
564
  tooltip.style.left = `${event.detail.position.x}px`;
565
+ tooltip.style.top = `${event.detail.position.y - 20}px`;
556
566
  });
557
567
  profile.addEventListener("out", (event)=>{
558
568
  logs.textContent = "";
@@ -1,7 +1,6 @@
1
1
  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
- import {createRef, ref} from 'lit/directives/ref.js';
5
4
  import type {PropertyValues} from 'lit';
6
5
 
7
6
  import {extent, bisector} from 'd3-array';
@@ -19,7 +18,7 @@ export class ElevationProfile extends LitElement {
19
18
  @property({type: Object}) tickSize = {x: 100, y: 40};
20
19
 
21
20
  @state() pointer = {x: 0, y: 0};
22
- private resizeController = new ResizeController(this, {});
21
+ private _resizeController = new ResizeController(this, {});
23
22
 
24
23
  private plotData: PlotPoint[] = [];
25
24
  private scaleX = scaleLinear();
@@ -33,17 +32,20 @@ export class ElevationProfile extends LitElement {
33
32
  private area = area()
34
33
  .x((point: PlotPoint) => this.scaleX(point[0]))
35
34
  .y1((point: PlotPoint) => this.scaleY(point[1]));
36
- private xAxis = axisBottom(this.scaleX)
37
- .tickFormat((val: number) => val + ' m');
38
- private yAxis = axisLeft(this.scaleY)
39
- .tickFormat((val: number) => val + ' m');
40
- private xGrid = axisBottom(this.scaleX).tickFormat(() => '');
41
- private yGrid = axisLeft(this.scaleY).tickFormat(() => '');
42
-
43
- private xRef = createRef();
44
- private yRef = createRef();
45
- private yGridRef = createRef();
46
- private xGridRef = createRef();
35
+ private xAxis = axisBottom(this.scaleX).tickFormat((value: number) => this.tickFormat(value));
36
+ private yAxis = axisLeft(this.scaleY).tickFormat((value: number) => this.tickFormat(value));
37
+ private xGrid = axisBottom(this.scaleX).tickFormat(() => '');
38
+ private yGrid = axisLeft(this.scaleY).tickFormat(() => '');
39
+
40
+ private meterFormat = Intl.NumberFormat('de-CH', {
41
+ style: 'unit',
42
+ unit: 'meter',
43
+ });
44
+
45
+ private kilometerFormat = Intl.NumberFormat('de-CH', {
46
+ style: 'unit',
47
+ unit: 'kilometer',
48
+ });
47
49
 
48
50
  override willUpdate(changedProperties: PropertyValues) {
49
51
  if (changedProperties.has('lines')) {
@@ -54,9 +56,9 @@ export class ElevationProfile extends LitElement {
54
56
  }
55
57
  }
56
58
 
57
- override shouldUpdate(): boolean {
58
- return this.lines.length > 0;
59
- }
59
+ // override shouldUpdate(): boolean {
60
+ // return this.lines.length > 0;
61
+ // }
60
62
 
61
63
  override render() {
62
64
  const width = this.offsetWidth;
@@ -77,17 +79,17 @@ export class ElevationProfile extends LitElement {
77
79
  this.yAxis.ticks(yTicks);
78
80
  this.yGrid.ticks(yTicks);
79
81
 
80
- select(this.xRef.value).call(this.xAxis);
81
- select(this.yRef.value).call(this.yAxis);
82
- select(this.xGridRef.value).call(this.xGrid);
83
- select(this.yGridRef.value).call(this.yGrid);
82
+ select(this.querySelector('.axis.x')).call(this.xAxis);
83
+ select(this.querySelector('.axis.y')).call(this.yAxis);
84
+ select(this.querySelector('.grid.x')).call(this.xGrid);
85
+ select(this.querySelector('.grid.y')).call(this.yGrid);
84
86
 
85
87
  return svg`
86
- <svg width="${width}" height="${height}">
87
- <g class="grid y" ${ref(this.yGridRef)} transform="translate(${this.margin.left}, 0)" />
88
- <g class="grid x" ${ref(this.xGridRef)} transform="translate(0, ${this.margin.bottom})" />
89
- <g class="axis x" ${ref(this.xRef)} transform="translate(0, ${height - this.margin.bottom})" />
90
- <g class="axis y" ${ref(this.yRef)} transform="translate(${this.margin.left}, 0)" />
88
+ <svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">
89
+ <g class="grid y" transform="translate(${this.margin.left}, 0)" />
90
+ <g class="grid x" transform="translate(0, ${this.margin.bottom})" />
91
+ <g class="axis x" transform="translate(0, ${height - this.margin.bottom})" />
92
+ <g class="axis y" transform="translate(${this.margin.left}, 0)" />
91
93
  <path class="area" d="${this.area(this.plotData)}" />
92
94
  <path class="elevation" d="${this.line(this.plotData)}" fill="none" />
93
95
  <g style="visibility: ${this.pointer.x > 0 ? 'visible' : 'hidden'}">
@@ -108,6 +110,7 @@ export class ElevationProfile extends LitElement {
108
110
  height="${height}"
109
111
  fill="none"
110
112
  pointer-events="all"
113
+ style="touch-action: none;"
111
114
  @pointermove="${this.pointerMove}"
112
115
  @pointerout="${this.pointerOut}"
113
116
  />
@@ -115,6 +118,14 @@ export class ElevationProfile extends LitElement {
115
118
  `;
116
119
  }
117
120
 
121
+ private tickFormat(value: number) {
122
+ if (value < 1000) {
123
+ return this.meterFormat.format(value);
124
+ } else {
125
+ return this.kilometerFormat.format(value / 1000);
126
+ }
127
+ }
128
+
118
129
  override firstUpdated() {
119
130
  // FIXME: because the ref element are used before render is done, we need to force an update
120
131
  this.requestUpdate();
@@ -123,6 +134,10 @@ export class ElevationProfile extends LitElement {
123
134
  private pointerMove(event: PointerEvent) {
124
135
  const pointerDistance = this.scaleX.invert(pointer(event)[0]);
125
136
  const index = Math.min(this.bisectDistance.left(this.plotData, pointerDistance), this.plotData.length - 1);
137
+
138
+ if (index < 0) {
139
+ return;
140
+ }
126
141
  // FIXME:
127
142
  // var d0 = data[i - 1]
128
143
  // var d1 = data[i];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geoblocks/elevation-profile",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "license": "BSD-3-Clause",
5
5
  "repository": "github:geoblocks/elevation-profile",
6
6
  "type": "module",