Merge pull request #9 from dominik-korsa/elevation-map

Start elevation legend at 0
This commit is contained in:
Igor Zawartka 2025-07-23 00:18:21 +02:00 committed by GitHub
commit 7a2c969489
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 9 deletions

View File

@ -7,11 +7,12 @@ function getMinMax(trackColorMode, scenery) {
if (!scenery) return null; if (!scenery) return null;
if (scenery.trackElevationBounds.minY > scenery.trackElevationBounds.maxY) return null; if (scenery.trackElevationBounds.minY > scenery.trackElevationBounds.maxY) return null;
// Ensure min and max values are even so that the min, max and middle values are integers const min = scenery.trackElevationBounds.minY;
const min = Math.floor(scenery.trackElevationBounds.minY/2)*2; // Make sure diff > 0 even on flat sceneries
let max = Math.ceil((scenery.trackElevationBounds.maxY)/2)*2; let diff = Math.max(scenery.trackElevationBounds.maxY - scenery.trackElevationBounds.minY, 1);
// Make sure max > min even on flat sceneries, otherwise `diff` calculation would result in division by 0 // Ensure `diff` is even so that the min, max and middle legend values are integers
if (max <= min) max = min + 2; diff = Math.ceil(diff / 2)*2;
const max = min + diff;
return [min, max]; return [min, max];
} }
@ -38,6 +39,7 @@ function createGradientSettings(trackColorMode, scenery, def) {
legendMin: minValue, legendMin: minValue,
legendMax: maxValue, legendMax: maxValue,
unit: gradient.unit, unit: gradient.unit,
startLegendAt0: gradient.startLegendAt0 ?? false,
}; };
} }
return null; return null;

View File

@ -50,15 +50,21 @@ function LegendGradient(props) {
const to = MiscHelper.getTrackGradientColor(gradientDef, gradientDef.legendMax); const to = MiscHelper.getTrackGradientColor(gradientDef, gradientDef.legendMax);
const gradientStyle = `linear-gradient(to right, ${from}, ${to})`; const gradientStyle = `linear-gradient(to right, ${from}, ${to})`;
const midVal = (gradientDef.legendMin + gradientDef.legendMax) / 2; let { legendMin, legendMax } = gradientDef;
if (gradientDef.startLegendAt0) {
legendMax -= legendMin;
legendMin = 0;
}
const midVal = (legendMin + legendMax) / 2;
return ( return (
<div className="gradient"> <div className="gradient">
<div className="gradient-bar" style={{ background: gradientStyle}}></div> <div className="gradient-bar" style={{ background: gradientStyle}}></div>
<div className="gradient-labels"> <div className="gradient-labels">
<span className="gradient-label">{gradientDef.legendMin} {gradientDef.unit}</span> <span className="gradient-label">{legendMin} {gradientDef.unit}</span>
<span className="gradient-label">{midVal} {gradientDef.unit}</span> <span className="gradient-label">{midVal} {gradientDef.unit}</span>
<span className="gradient-label">{gradientDef.legendMax} {gradientDef.unit}</span> <span className="gradient-label">{legendMax} {gradientDef.unit}</span>
</div> </div>
</div> </div>
); );

View File

@ -166,6 +166,7 @@ const Constants = {
legendMin: 0, legendMin: 0,
legendMax: 10, legendMax: 10,
unit: '‰', unit: '‰',
startLegendAt0: false,
} }
}, },
'max-speed': { 'max-speed': {
@ -177,6 +178,7 @@ const Constants = {
legendMin: 0, legendMin: 0,
legendMax: 170, legendMax: 170,
unit: 'km/h', unit: 'km/h',
startLegendAt0: false,
}, },
options: { options: {
'derail': ['#f22', 'Derail track'], 'derail': ['#f22', 'Derail track'],
@ -190,8 +192,9 @@ const Constants = {
min: [0, 190, 0], min: [0, 190, 0],
max: [255, 22, 22], max: [255, 22, 22],
defaultMin: 0, defaultMin: 0,
defaultMax: 1, defaultMax: 2,
unit: 'm', unit: 'm',
startLegendAt0: true,
}, },
}, },
} }