Skip to content

Commit fb721dc

Browse files
committed
show steepness in popup and graph color
1 parent f7175fe commit fb721dc

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

app/javascript/maplibre/feature/elevation.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ export async function showElevationChart (feature) {
6868
borderColor: chartLineColor,
6969
borderWidth: 2,
7070
backgroundColor: chartLineColor + '50',
71+
segment: {
72+
backgroundColor: (ctx) => {
73+
const i0 = ctx.p0DataIndex
74+
const i1 = ctx.p1DataIndex
75+
const distDiff = labels[i1] - labels[i0]
76+
if (distDiff === 0) return chartLineColor + '50'
77+
const grade = Math.abs((values[i1] - values[i0]) / distDiff * 100)
78+
if (grade >= 15) return 'rgba(139, 0, 0, 0.5)'
79+
if (grade >= 10) return 'rgba(255, 0, 0, 0.5)'
80+
if (grade >= 5) return 'rgba(255, 165, 0, 0.5)'
81+
return chartLineColor + '50'
82+
}
83+
},
7184
pointRadius: 4,
7285
pointHoverRadius: 6,
7386
pointBackgroundColor: 'white',
@@ -109,6 +122,15 @@ export async function showElevationChart (feature) {
109122
label: (tooltipItem) => {
110123
return "Elevation: " + tooltipItem.raw.toFixed(0) + 'm'
111124
},
125+
afterLabel: (tooltipItem) => {
126+
const i = tooltipItem.dataIndex
127+
if (i === 0) return 'Steepness: 0%'
128+
const elevDiff = values[i] - values[i - 1]
129+
const distDiff = labels[i] - labels[i - 1]
130+
if (distDiff === 0) return 'Steepness: 0%'
131+
const grade = (elevDiff / distDiff) * 100
132+
return 'Steepness: ' + grade.toFixed(1) + '%'
133+
},
112134
afterBody: function(context) {
113135
let coord = feature.geometry.coordinates[context[0]['dataIndex']]
114136
marker = getMarker(feature)

0 commit comments

Comments
 (0)