@raruto/leaflet-elevation 1.9.2 → 1.9.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.
@@ -13,6 +13,48 @@ Elevation.addInitHook(function() {
13
13
  acceleration.label = opts.accelerationLabel || L._(opts.imperial ? 'ft/s²' : 'm/s²');
14
14
  opts.accelerationFactor = opts.accelerationFactor || 1;
15
15
 
16
+ this._registerDataAttribute({
17
+ name: 'acceleration',
18
+ init: () => {
19
+ // this.track_info.acceleration_max = -Infinity; // Acceleration Max
20
+ // this.track_info.acceleration_min = +Infinity; // Acceleration Min
21
+ // this.track_info.acceleration_avg = 0; // Acceleration Avg
22
+ },
23
+ fetch: (i) => {
24
+ let data = this._data;
25
+ let dv = (data[i].speed - data[i > 0 ? i - 1 : i].speed) * (1000 / opts.timeFactor);
26
+ let dt = (data[i].time - data[i > 0 ? i - 1 : i].time) / 1000;
27
+ return dt > 0 ? Math.abs((dv / dt)) * opts.accelerationFactor : 0;
28
+ },
29
+ update: (acceleration, i) => {
30
+ this.track_info.acceleration_max = this.track_info.acceleration_max || -Infinity; // Acceleration Max
31
+ this.track_info.acceleration_min = this.track_info.acceleration_min || +Infinity; // Acceleration Min
32
+ this.track_info.acceleration_avg = this.track_info.acceleration_avg || 0; // Acceleration Avg
33
+
34
+ let data = this._data;
35
+ let track = this.track_info;
36
+
37
+ // Try to smooth "crazy" acceleration values.
38
+ if (this.options.accelerationDeltaMax) {
39
+ let delta = acceleration - data[i > 0 ? i - 1 : i].acceleration;
40
+ let deltaMax = this.options.accelerationDeltaMax;
41
+ if (Math.abs(delta) > deltaMax) {
42
+ acceleration = data[i - 1].acceleration + deltaMax * Math.sign(delta);
43
+ }
44
+ }
45
+
46
+ // Range of acceptable acceleration values.
47
+ acceleration = _.clamp(acceleration, this.options.accelerationRange);
48
+
49
+ if (acceleration > track.acceleration_max) track.acceleration_max = acceleration;
50
+ if (acceleration < track.acceleration_min) track.acceleration_min = acceleration;
51
+
52
+ track.acceleration_avg = (acceleration + track.acceleration_avg) / 2.0;
53
+
54
+ return L.Util.formatNum(acceleration, 2);
55
+ },
56
+ });
57
+
16
58
  if (this.options.acceleration != "summary") {
17
59
 
18
60
  this._registerAxisScale({
@@ -26,16 +68,16 @@ Elevation.addInitHook(function() {
26
68
  label : acceleration.label,
27
69
  labelX : 25,
28
70
  labelY : -8,
29
- name : 'acceleration'
71
+ name : 'acceleration',
30
72
  });
31
73
 
32
74
  this._registerAreaPath({
33
- name : 'acceleration',
34
- label : 'Acceleration',
35
- yAttr : 'acceleration',
36
- scaleX : 'distance',
37
- scaleY : 'acceleration',
38
- color : '#050402',
75
+ name : 'acceleration',
76
+ label : 'Acceleration',
77
+ yAttr : 'acceleration',
78
+ scaleX : 'distance',
79
+ scaleY : 'acceleration',
80
+ color : '#050402',
39
81
  strokeColor : '#000',
40
82
  strokeOpacity: "0.5",
41
83
  fillOpacity : "0.25",
@@ -43,65 +85,28 @@ Elevation.addInitHook(function() {
43
85
 
44
86
  }
45
87
 
46
- this.on('elepoint_added', function(e) {
47
- let data = this._data;
48
- let i = e.index;
49
-
50
- let currT = data[i].time;
51
- let prevT = i > 0 ? data[i - 1].time : currT;
52
-
53
- let deltaT = (currT - prevT) / 1000;
54
-
55
- let track = this.track_info;
56
- let sMax = track.acceleration_max || -Infinity; // Acceleration Max
57
- let sMin = track.acceleration_min || +Infinity; // Acceleration Min
58
- let sAvg = track.acceleration_avg || 0; // Acceleration Avg
59
- let acceleration = 0;
60
-
61
- if (deltaT > 0) {
62
- let curr = data[i].speed;
63
- let prev = i > 0 ? data[i - 1].speed : curr;
64
- let delta = (curr - prev) * (1000 / opts.timeFactor);
65
- acceleration = Math.abs((delta / deltaT)) * opts.accelerationFactor;
66
- }
67
-
68
- // Try to smooth "crazy" acceleration values.
69
- if (this.options.accelerationDeltaMax) {
70
- let deltaA = i > 0 ? acceleration - data[i - 1].acceleration : 0;
71
- let maxDeltaS = this.options.accelerationDeltaMax;
72
- if (Math.abs(deltaA) > maxDeltaS) {
73
- acceleration = data[i - 1].acceleration + maxDeltaS * Math.sign(deltaA);
74
- }
75
- }
76
-
77
- // Range of acceptable acceleration values.
78
- acceleration = _.clamp(acceleration, this.options.accelerationRange);
79
-
80
- track.acceleration_max = acceleration > sMax ? acceleration : sMax;
81
- track.acceleration_min = acceleration < sMin ? acceleration : sMin;
82
- track.acceleration_avg = (acceleration + sAvg) / 2.0;
83
-
84
- data[i].acceleration = L.Util.formatNum(acceleration, 2);
85
- });
86
-
87
88
  this._registerTooltip({
88
89
  name : 'acceleration',
89
90
  chart: (item) => L._("a: ") + item.acceleration + " " + acceleration.label,
90
91
  marker: (item) => Math.round(item.acceleration) + " " + acceleration.label,
92
+ order: 60,
91
93
  });
92
94
 
93
95
  this._registerSummary({
94
96
  "minacceleration" : {
95
97
  label: "Min Acceleration: ",
96
- value: (track) => Math.round(track.acceleration_min || 0) + '&nbsp;' + acceleration.label
98
+ value: (track) => Math.round(track.acceleration_min || 0) + '&nbsp;' + acceleration.label,
99
+ order: 60
97
100
  },
98
101
  "maxacceleration" : {
99
102
  label: "Max Acceleration: ",
100
- value: (track) => Math.round(track.acceleration_max || 0) + '&nbsp;' + acceleration.label
103
+ value: (track) => Math.round(track.acceleration_max || 0) + '&nbsp;' + acceleration.label,
104
+ order: 60
101
105
  },
102
106
  "avgacceleration": {
103
107
  label: "Avg Acceleration: ",
104
- value: (track) => Math.round(track.acceleration_avg || 0) + '&nbsp;' + acceleration.label
108
+ value: (track) => Math.round(track.acceleration_avg || 0) + '&nbsp;' + acceleration.label,
109
+ order: 60
105
110
  }
106
111
  });
107
112
 
package/src/altitude.js CHANGED
@@ -5,8 +5,11 @@ import { Elevation } from './control';
5
5
 
6
6
  Elevation.addInitHook(function() {
7
7
 
8
- let opts = this.options;
9
- let altitude = {};
8
+ let opts = this.options;
9
+ let altitude = {};
10
+
11
+ let theme = opts.theme.replace('-theme', '');
12
+ let color = D3.Colors[theme] || {};
10
13
 
11
14
  if (opts.imperial) {
12
15
  opts.altitudeFactor = this.__footFactor;
@@ -16,105 +19,101 @@ Elevation.addInitHook(function() {
16
19
  altitude.label = opts.yLabel;
17
20
  }
18
21
 
19
- this.on("eledata_updated", function(e) {
20
- let data = this._data;
21
- let i = e.index;
22
- let z = data[i].z * opts.altitudeFactor;
23
-
24
- let track = this.track_info;
25
- let zMax = track.elevation_max || -Infinity;
26
- let zMin = track.elevation_min || +Infinity;
27
-
28
- // check and fix missing elevation data on last added point
29
- if (!this.options.skipNullZCoords && i > 0) {
30
- let prevZ = data[i - 1].z;
31
- if (isNaN(prevZ)) {
32
- let lastZ = this._lastValidZ;
33
- let currZ = z;
34
- if (!isNaN(lastZ) && !isNaN(currZ)) {
35
- prevZ = (lastZ + currZ) / 2;
36
- } else if (!isNaN(lastZ)) {
37
- prevZ = lastZ;
38
- } else if (!isNaN(currZ)) {
39
- prevZ = currZ;
22
+ this._registerDataAttribute({
23
+ name: 'z',
24
+ skipNull: this.options.skipNullZCoords,
25
+ init: ({point, }) => {
26
+ // "alt" property is generated inside "leaflet"
27
+ if ("alt" in point) point.meta.ele = point.alt;
28
+
29
+ // this.track_info.elevation_max = -Infinity;
30
+ // this.track_info.elevation_min = +Infinity;
31
+ // this.track_info.elevation_avg = 0;
32
+ },
33
+ fetch: (i) => {
34
+ return this._data[i].z * opts.altitudeFactor;
35
+ },
36
+ update: (z, i) => {
37
+ this.track_info.elevation_max = this.track_info.elevation_max || -Infinity;
38
+ this.track_info.elevation_min = this.track_info.elevation_max || +Infinity;
39
+ this.track_info.elevation_avg = this.track_info.elevation_avg || 0;
40
+
41
+ let data = this._data;
42
+ let track = this.track_info;
43
+
44
+ // Try to smooth "crazy" elevation values.
45
+ if (this.options.altitudeDeltaMax) {
46
+ let delta = z - data[i > 0 ? i - 1 : i].z;
47
+ let deltaMax = this.options.altitudeDeltaMax;
48
+ if (Math.abs(delta) > deltaMax) {
49
+ z = data[i - 1].z + deltaMax * Math.sign(delta);
40
50
  }
41
- if (!isNaN(prevZ)) return data.splice(i - 1, 1);
42
- data[i - 1].z = prevZ;
43
51
  }
44
- }
45
- // skip point if it has not elevation
46
- if (!isNaN(z)) {
47
- this._lastValidZ = z;
48
- }
49
52
 
50
- track.elevation_max = z > zMax ? z : zMax;
51
- track.elevation_min = z < zMin ? z : zMin;
53
+ // Range of acceptable elevation values.
54
+ z = _.clamp(z, this.options.altitudeRange);
52
55
 
53
- data[i].z = z;
54
- });
55
-
56
- this.on("elechart_axis", function() {
56
+ if (z > track.elevation_max) track.elevation_max = z;
57
+ if (z < track.elevation_min) track.elevation_min = z;
57
58
 
58
- this._chart._registerAxisGrid({
59
- axis : "y",
60
- position : "left",
61
- scale : this._chart._y,
62
- });
59
+ track.elevation_avg = (z + track.elevation_avg) / 2.0;
63
60
 
61
+ return z;
62
+ }
64
63
  });
65
64
 
66
- if (this.options.altitude != "summary") {
67
-
68
- this.on("elechart_axis", function() {
69
-
70
- this._chart._registerAxisScale({
71
- axis : "y",
72
- position: "left",
73
- scale : this._chart._y,
74
- label : altitude.label,
75
- labelX : -3,
76
- labelY : -8,
77
- name : "altitude",
78
- });
79
-
80
- });
81
-
82
- this.on("elechart_init", function() {
83
-
84
- let theme = this.options.theme.replace('-theme', '');
85
- let color = D3.Colors[theme] || {};
86
-
87
- this._chart._registerAreaPath({
88
- name : 'altitude',
89
- label : 'Altitude',
90
- scaleX : 'distance',
91
- scaleY : 'altitude',
92
- color : color.area || theme,
93
- strokeColor : this.options.detached ? color.stroke : '#000',
94
- strokeOpacity: "1",
95
- fillOpacity : this.options.detached ? (color.alpha || '0.8') : 1,
96
- preferCanvas : this.options.preferCanvas,
97
- });
65
+ this._registerAxisScale({
66
+ axis : "y",
67
+ position: "left",
68
+ scale : "y", // this._chart._y,
69
+ label : altitude.label,
70
+ labelX : -3,
71
+ labelY : -8,
72
+ name : "altitude",
73
+ visbile : this.options.altitude != "summary"
74
+ });
98
75
 
99
- });
76
+ this._registerAxisGrid({
77
+ axis : "y",
78
+ position : "left",
79
+ scale : "y" // this._chart._y,
80
+ });
100
81
 
101
- }
82
+ this._registerAreaPath({
83
+ name : 'altitude',
84
+ label : 'Altitude',
85
+ scaleX : 'distance',
86
+ scaleY : 'altitude',
87
+ color : color.area || theme,
88
+ strokeColor : opts.detached ? color.stroke : '#000',
89
+ strokeOpacity: "1",
90
+ fillOpacity : opts.detached ? (color.alpha || '0.8') : 1,
91
+ preferCanvas : opts.preferCanvas,
92
+ visbile : this.options.altitude != "summary"
93
+ });
102
94
 
103
95
  this._registerTooltip({
104
96
  name: 'y',
105
97
  chart: (item) => L._("y: ") + d3.format("." + opts.decimalsY + "f")(item[opts.yAttr]) + " " + altitude.label,
106
98
  marker: (item) => d3.format("." + opts.decimalsY + "f")(item[opts.yAttr]) + " " + altitude.label,
99
+ order: 10,
107
100
  });
108
101
 
109
102
  this._registerSummary({
110
103
  "maxele" : {
111
104
  label: "Max Elevation: ",
112
- value: (track) => (track.elevation_max || 0).toFixed(2) + '&nbsp;' + altitude.label
105
+ value: (track) => (track.elevation_max || 0).toFixed(2) + '&nbsp;' + altitude.label,
106
+ order: 30,
113
107
  },
114
108
  "minele" : {
115
109
  label: "Min Elevation: ",
116
- value: (track) => (track.elevation_min || 0).toFixed(2) + '&nbsp;' + altitude.label
110
+ value: (track) => (track.elevation_min || 0).toFixed(2) + '&nbsp;' + altitude.label,
111
+ order: 30,
112
+ },
113
+ "avgele" : {
114
+ label: "Avg Elevation: ",
115
+ value: (track) => (track.elevation_avg || 0).toFixed(2) + '&nbsp;' + altitude.label,
116
+ order: 30,
117
117
  },
118
118
  });
119
-
120
- });
119
+ });
package/src/chart.js CHANGED
@@ -344,6 +344,9 @@ export var Chart = L.Class.extend({
344
344
  },
345
345
 
346
346
  _registerAreaPath: function(props) {
347
+ if (props.scale == 'y') props.scale = this._y;
348
+ else if (props.scale == 'x') props.scale = this._x;
349
+
347
350
  let opts = this.options;
348
351
 
349
352
  if (!props.xAttr) props.xAttr = opts.xAttr;
@@ -368,10 +371,16 @@ export var Chart = L.Class.extend({
368
371
  },
369
372
 
370
373
  _registerAxisGrid: function(props) {
374
+ if (props.scale == 'y') props.scale = this._y;
375
+ else if(props.scale == 'x') props.scale = this._x;
376
+
371
377
  this._props.grids[props.name || props.axis] = props;
372
378
  },
373
379
 
374
380
  _registerAxisScale: function(props) {
381
+ if (props.scale == 'y') props.scale = this._y;
382
+ else if(props.scale == 'x') props.scale = this._x;
383
+
375
384
  let opts = this.options;
376
385
  let scale = props.scale;
377
386
 
@@ -433,6 +442,7 @@ export var Chart = L.Class.extend({
433
442
  },
434
443
 
435
444
  _registerTooltip: function(props) {
445
+ props.order = props.order ?? 100;
436
446
  this._props.tooltipItems[props.name] = props;
437
447
  },
438
448
 
package/src/components.js CHANGED
@@ -116,9 +116,11 @@ export const PositionMarker = ({
116
116
  }) => {
117
117
  return g => {
118
118
 
119
- let line = g.select('.height-focus.line');
119
+ g.attr("class", "height-focus-group");
120
+
121
+ let line = g.select('.height-focus.line');
120
122
  let circle = g.select('.height-focus.circle-lower');
121
- let text = g.select('.height-focus-label');
123
+ let text = g.select('.height-focus-label');
122
124
 
123
125
  if (!line.node()) line = g.append('svg:line');
124
126
  if (!circle.node()) circle = g.append('svg:circle');
@@ -148,7 +150,10 @@ export const PositionMarker = ({
148
150
 
149
151
  let label;
150
152
 
151
- for (var i in labels) {
153
+ Object
154
+ .keys(labels)
155
+ .sort((a, b) => labels[a].order - labels[b].order) // TODO: any performance issues?
156
+ .forEach((i)=> {
152
157
  label = text.select(".height-focus-" + labels[i].name);
153
158
 
154
159
  if (!label.size()) {
@@ -158,7 +163,8 @@ export const PositionMarker = ({
158
163
  }
159
164
 
160
165
  label.text(typeof labels[i].value !== "function" ? labels[i].value : labels[i].value(item));
161
- }
166
+
167
+ });
162
168
 
163
169
  text.select('tspan').attr("dy", text.selectAll('tspan').size() > 1 ? "-1.5em" : "0em" );
164
170
  text.selectAll('tspan').attr("x", xCoord + 5);
@@ -272,17 +278,20 @@ export const Tooltip = ({
272
278
 
273
279
  let label;
274
280
 
275
- for (let i in labels) {
276
- label = text.select(".mouse-focus-label-" + labels[i].name);
281
+ Object
282
+ .keys(labels)
283
+ .sort((a, b) => labels[a].order - labels[b].order) // TODO: any performance issues?
284
+ .forEach((i)=> {
285
+ label = text.select(".mouse-focus-label-" + labels[i].name);
277
286
 
278
- if (!label.size()) {
279
- label = text.append("svg:tspan", ".mouse-focus-label-x")
280
- .attr("class", "mouse-focus-label-" + labels[i].name)
281
- .attr("dy", "1.5em");
282
- }
287
+ if (!label.size()) {
288
+ label = text.append("svg:tspan", ".mouse-focus-label-x")
289
+ .attr("class", "mouse-focus-label-" + labels[i].name)
290
+ .attr("dy", "1.5em");
291
+ }
283
292
 
284
- label.text(typeof labels[i].value !== "function" ? labels[i].value : labels[i].value(item));
285
- }
293
+ label.text(typeof labels[i].value !== "function" ? labels[i].value : labels[i].value(item));
294
+ });
286
295
 
287
296
  text.select('tspan').attr("dy", "1em");
288
297
  text.selectAll('tspan').attr("x", xAlign);