-
Notifications
You must be signed in to change notification settings - Fork 80
/
slope.js
185 lines (152 loc) · 5.12 KB
/
slope.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
import 'leaflet-i18n';
import * as _ from './utils';
import * as D3 from './components';
import { Elevation } from './control';
Elevation.addInitHook(function() {
if (!this.options.slope) return;
let opts = this.options;
let slope = {};
opts.margins.right = 50;
this.on("elechart_init", function() {
slope.path = this._area.append('path')
.style("pointer-events", "none")
// TODO: add a class here.
.attr("fill", "#F00")
.attr("stroke", "#000")
.attr("stroke-opacity", "0.5")
.attr("fill-opacity", "0.25");
});
this.on("elechart_axis", function() {
slope.x = this._x;
slope.y = D3.Scale({
data: this._data,
range: [this._height(), 0],
attr: "slope",
min: -1,
max: +1,
forceBounds: opts.forceAxisBounds,
});
slope.axis = D3.Axis({
axis: "y",
position: "right",
width: this._width(),
height: this._height(),
scale: slope.y,
ticks: this.options.yTicks,
tickPadding: 16,
label: "%",
labelX: 25,
labelY: 3,
});
this._axis.call(slope.axis);
});
this.on("elechart_updated", function() {
slope.area = D3.Area({
interpolation: "curveStepAfter",
data: this._data,
name: 'Slope',
xAttr: opts.xAttr,
yAttr: "slope",
width: this._width(),
height: this._height(),
scaleX: slope.x,
scaleY: slope.y,
});
slope.path.call(slope.area);
});
this.on("elechart_legend", function() {
slope.legend = this._legend.append("g")
.call(
D3.LegendItem({
name: 'Slope',
width: this._width(),
height: this._height(),
margins: this.options.margins,
})
);
this._altitudeLegend
.attr("transform", "translate(-50, 0)");
slope.legend
.attr("transform", "translate(50, 0)");
slope.legend.select("rect")
.classed("area", false)
// TODO: add a class here.
.attr("fill", "#F00")
.attr("stroke", "#000")
.attr("stroke-opacity", "0.5")
.attr("fill-opacity", "0.25");
});
this.on("eledata_updated", function(e) {
let data = this._data;
let i = e.index;
let z = data[i].z;
let curr = data[i].latlng;
let prev = i > 0 ? data[i - 1].latlng : curr;
let delta = curr.distanceTo(prev) * this._distanceFactor;
// Slope / Gain
let tAsc = this._tAsc || 0; // Total Ascent
let tDes = this._tDes || 0; // Total Descent
let sMax = this._sMax || 0; // Slope Max
let sMin = this._sMin || 0; // Slope Min
let diff = 0;
let slope = 0;
if (!isNaN(z)) {
// diff height between actual and previous point
diff = i > 0 ? z - data[i - 1].z : 0;
if (diff > 0) tAsc += diff;
if (diff < 0) tDes -= diff;
// slope in % = ( height / length ) * 100
slope = delta !== 0 ? Math.round((diff / delta) * 10000) / 100 : 0;
// apply slope to the previous point because we will
// ascent or desent, so the slope is in the fist point
if (i > 0) data[i - 1].slope = slope;
sMax = slope > sMax ? slope : sMax;
sMin = slope < sMin ? slope : sMin;
}
data[i].slope = slope;
this.track_info = this.track_info || {};
this.track_info.ascent = this._tAsc = tAsc;
this.track_info.descent = this._tDes = tDes;
this.track_info.slope_max = this._sMax = sMax;
this.track_info.slope_min = this._sMin = sMin;
});
this.on("elechart_change", function(e) {
let item = e.data;
let xCoordinate = e.xCoord;
if (!this._focuslabelSlope || !this._focuslabelSlope.property('isConnected')) {
this._focuslabelSlope = this._focuslabel.select('text').insert("svg:tspan", ".mouse-focus-label-x")
.attr("class", "mouse-focus-label-slope")
.attr("dy", "1.5em");
}
this._focuslabelSlope.text(item.slope + "%");
if (!this._mouseSlopeFocusLabel) {
this._mouseSlopeFocusLabel = this._mouseHeightFocusLabel.append("svg:tspan")
.attr("class", "height-focus-slope ");
}
this._mouseHeightFocusLabel.select('.height-focus-y')
.attr("dy", "-1.5em");
this._focuslabel.select('.mouse-focus-label-x')
.attr("dy", "1.5em");
this._mouseSlopeFocusLabel
.attr("dy", "1.5em")
.text(Math.round(item.slope) + "%");
});
this.on("elechart_summary", function() {
this.track_info.ascent = this._tAsc || 0;
this.track_info.descent = this._tDes || 0;
this.track_info.slope_max = this._sMax || 0;
this.track_info.slope_min = this._sMin || 0;
this.summaryDiv.querySelector('.minele').insertAdjacentHTML('afterend', '<span class="ascent"><span class="summarylabel">' + L._("Total Ascent: ") + '</span><span class="summaryvalue">' + Math.round(this.track_info.ascent) + ' ' +
this._yLabel +
'</span></span>' + '<span class="descent"><span class="summarylabel">' + L._("Total Descent: ") + '</span><span class="summaryvalue">' + Math.round(this.track_info.descent) + ' ' + this._yLabel +
'</span></span>' + '<span class="minslope"><span class="summarylabel">' + L._("Min Slope: ") + '</span><span class="summaryvalue">' + this.track_info.slope_min + ' ' + '%' +
'</span></span>' + '<span class="maxslope"><span class="summarylabel">' + L._("Max Slope: ") + '</span><span class="summaryvalue">' + this.track_info.slope_max + ' ' + '%' +
'</span></span>');
});
this.on("eledata_clear", function() {
this._sMax = null;
this._sMin = null;
this._tAsc = null;
this._tDes = null;
});
});