Comments (7)
Hi @FabianSchmick, in here you should actually see some gap between in chart area (ref: https://jsfiddle.net/erjqhgc1/1/).
BTW, I recommend you start by doing some testing by implementing a your own custom handler.
In particular, you can start investigating the following:
leaflet-elevation/src/handlers/distance.js
Lines 1 to 46 in ea15556
Related resources
Currently all additional chart area definitions (altitude, speed, acceleration, ...) are lazy-loaded from the
src/handlers
.So I think a good way to achieve this could be to create a custom definition for this type of graph as well.
It sure won't work out of the box, but maybe with a little tweaking of the "loading" logic it might even turn out to be something more useful.
Below some notable functions:
• L.Control.Elevation::options.handlers
leaflet-elevation/examples/leaflet-elevation_extended-ui.html
Lines 255 to 271 in b0006d6
• L.Control.Elevation::_registerHandler
leaflet-elevation/src/control.js
Lines 1080 to 1147 in b0006d6
• L.Control.Elevation::_registerDataAttribute
leaflet-elevation/src/control.js
Lines 1003 to 1078 in b0006d6
👋 Raruto
from leaflet-elevation.
I think the pointToAttr code is executed before my flag is added.
Listening to both events (in sequence) you should somehow achieve that goal:
leaflet-elevation/src/control.js
Line 363 in ea15556
leaflet-elevation/src/control.js
Line 368 in ea15556
I didn't elaborate, maybe you need some other support flag..
👋 Raruto
from leaflet-elevation.
@Raruto thank you for your reply. The hint with investigating the src/handlers/distance.js handler helped a lot. I understand the project more from day to day.
I also could fix my issue: 🎉
But I am not 100% happy with my solution:
- Override
_addGeoJSONData
and_addPoint
:
L.Control.Elevation.include({
_addPoint: function (x, y, z, isFirstPoint = false) {
...
this._data.push({
x: x,
y: y,
z: z,
latlng: L.latLng(x, y, z),
isFirstPoint: isFirstPoint
});
...
},
_addGeoJSONData: function (coords, properties, nestingLevel) {
...
coords.forEach((point, i) => {
...
this._addPoint(
point.lat ?? point[1],
point.lng ?? point[0],
point.alt ?? point.meta.ele ?? point[2],
i === 0
);
...
});
...
}
})
- Create my own handler:
export function Distance() {
... // I have copied the full file only to add a few lines of code :/
return {
...
pointToAttr: (point, i) => {
let currentPointDistance = (i > 0 ? this._data[i - 1].dist : 0);
if (this._data[i].isFirstPoint) {
return currentPointDistance;
}
return currentPointDistance + (this._data[i].latlng.distanceTo(this._data[i > 0 ? i - 1 : i].latlng) * opts.distanceFactor) / 1000;
},
...
};
}
As you can see, everytime I write ...
I copied the lines from the library. I have to admit, that I am not an experienced JS-Developer, but I don't like to copy so much code. Do you think the new information isFirstPoint
on the this._data
object should live in the source code, maybe this can help others, too? Also, I would like to ask if you know a way to extend the existing handler
and override only the pointToAttr
function-body of it (so I don't have to copy the full content of the src/handlers/distance.js
).
from leaflet-elevation.
maybe this can help others, too?
It's definitely something that others might be interested in as well, perhaps through a dedicated option.
To be carefully evaluated through a reasonable PR.
I don't like to copy so much code
Handlers files are there to allow a certain degree of flexibility (custom extensions or hotfixes) to other developers looking forward to an official release.
Not much has changed in here, but if you don't override the whole handler file in the future it could be a little more difficult to keep your local changes aligned (monkey patching is leaner, but it's also a dangerous technique).
Do you think the new information isFirstPoint on the this._dataobject should live in the source code?
Alternatively, why not consider the following listener?
leaflet-elevation/src/control.js
Line 368 in ea15556
If I'm not mistaken it's used for a similar purpose:
leaflet-elevation/src/components/chart.js
Lines 67 to 72 in ea15556
👋 Raruto
from leaflet-elevation.
I assume the eletrack_added
event is unfortunately too late.
I tried the following:
controlElevation.on('eletrack_added', ({index}) => {
controlElevation._data[index]['isLastPoint'] = true;
});
In the handler
pointToAttr: (point, i) => {
...
if (this._data[i].isLastPoint === true) {
console.log("test");
return currentPointDistance;
}
...
},
I think the pointToAttr
code is executed before my flag is added. So the if
-statement never becomes true
in the first run.
But if it would work, I could omit the monkey patching.
from leaflet-elevation.
However, you probably need to try adding all of your event listeners before all the various handlers are registered:
leaflet-elevation/src/control.js
Lines 243 to 263 in ea15556
When monkey pathching, in order to avoid duplicating too much code, if you just want to run some code (before or after) that function, you can invoke oldProto.onAdd.call()
on the old function reference.
Here it is a similar situation: https://github.com/Raruto/leaflet-ui/blob/31b300a69f0ae70b1d0c767686ca1c25c541f03a/src/leaflet-ui.js#L572-L592
from leaflet-elevation.
Ahh, great! I couldn't think straight at the moment
This:
controlElevation
.on('eletrack_added', ({index}) => {
controlElevation._data[index]['isLastPoint'] = true;
})
.on('elepoint_added', ({index}) => {
if (controlElevation._data[index ? index -1 : 0]['isLastPoint']) {
controlElevation._data[index]['isFirstPoint'] = true;
}
})
;
And the custom handler from my comment are the solution:
export function Distance() {
... // I have copied the full file only to add a few lines of code :/
return {
...
pointToAttr: (point, i) => {
let currentPointDistance = (i > 0 ? this._data[i - 1].dist : 0);
if (this._data[i].isFirstPoint) {
return currentPointDistance;
}
return currentPointDistance + (this._data[i].latlng.distanceTo(this._data[i > 0 ? i - 1 : i].latlng) * opts.distanceFactor) / 1000;
},
...
};
}
Thanks again for the useful help @Raruto
from leaflet-elevation.
Related Issues (20)
- Setting `marker: 'position-marker'` throws Uncaught TypeError: t.icon.createIcon is not a function.
- Label rotation randomly not working HOT 1
- Large parts of leaflet-holtine are black HOT 13
- Integration with Bootstrap framework (v3.3.7) HOT 2
- Omitting the `<ele>` tag in gpx file throws Uncaught DOMException: CanvasGradient.addColorStop: Invalid color HOT 2
- GDPR compliance - usage without unpkg.com HOT 8
- Hide file url and encrypt gpx data HOT 9
- How to add the Download button to a gpxGroup map HOT 2
- Android: Pointer on elevation graph HOT 2
- Change decimal and thousands separator HOT 4
- Polyline layer turns black after turning off all legend filters (`legend: true` + `preferCanvas: false`) HOT 6
- Updating a track continuously (live tracking) HOT 3
- Unsopperted GeoJSON feature geometry type:Point HOT 1
- Download link HOT 1
- How to show Elevation in decimal? HOT 3
- Marker tooltip with negative elevation HOT 1
- Polyline layer turns black after turning off all legend filters on custom colors HOT 4
- Disable built-in lazy loader (dynamic imports) and load all dependencies statically HOT 8
- Leaflet elevation get track properties (Length, Elevation, time, etc) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leaflet-elevation.