Skip to content

Commit b13a083

Browse files
committed
Fix: Use chart.update() instead of destroy/recreate for better interactivity
When waypoints are dragged, the elevation chart should update smoothly. The previous approach of destroying and recreating the chart on every update was causing the chart to not refresh when waypoints were moved. Chart.js update() method is designed for this use case: - Preserves the chart instance - Updates data and options - Triggers smooth re-render with animations - More efficient than destroy/recreate This fixes the issue where dragging a waypoint didn't update the elevation profile.
1 parent 0c5229e commit b13a083

1 file changed

Lines changed: 63 additions & 54 deletions

File tree

tabs/mission_control.js

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4330,68 +4330,77 @@ function iconKey(filename) {
43304330
return;
43314331
}
43324332

4333-
// Destroy existing chart if it exists
4334-
if (window.elevationChartInstance) {
4335-
window.elevationChartInstance.destroy();
4336-
}
4337-
4338-
window.elevationChartInstance = new Chart(ctx, {
4339-
type: 'line',
4340-
data: {
4341-
labels: x_elevation,
4342-
datasets: [
4343-
{
4344-
label: 'WGS84 elevation',
4345-
data: elevation.map((y, i) => ({x: x_elevation[i], y: y})),
4346-
borderColor: '#ff7f0e',
4347-
backgroundColor: 'rgba(255, 127, 14, 0.2)',
4348-
borderWidth: 2,
4349-
fill: true,
4350-
pointRadius: 0,
4351-
},
4352-
{
4353-
label: 'Mission altitude',
4354-
data: lengthMission.map((x, i) => ({x: x, y: y_missionElevation[i]})),
4355-
borderColor: '#1497f1',
4356-
backgroundColor: 'rgba(20, 151, 241, 0)',
4357-
borderWidth: 2,
4358-
pointRadius: 5,
4359-
pointBackgroundColor: '#1f77b4',
4360-
}
4361-
]
4362-
},
4363-
options: {
4364-
responsive: true,
4365-
maintainAspectRatio: false,
4366-
plugins: {
4367-
title: {
4368-
display: true,
4369-
text: chartTitle
4370-
},
4371-
legend: {
4372-
display: true,
4373-
position: 'top',
4374-
}
4333+
const newData = {
4334+
labels: x_elevation,
4335+
datasets: [
4336+
{
4337+
label: 'WGS84 elevation',
4338+
data: elevation.map((y, i) => ({x: x_elevation[i], y: y})),
4339+
borderColor: '#ff7f0e',
4340+
backgroundColor: 'rgba(255, 127, 14, 0.2)',
4341+
borderWidth: 2,
4342+
fill: true,
4343+
pointRadius: 0,
43754344
},
4376-
scales: {
4377-
x: {
4378-
type: 'linear',
4345+
{
4346+
label: 'Mission altitude',
4347+
data: lengthMission.map((x, i) => ({x: x, y: y_missionElevation[i]})),
4348+
borderColor: '#1497f1',
4349+
backgroundColor: 'rgba(20, 151, 241, 0)',
4350+
borderWidth: 2,
4351+
pointRadius: 5,
4352+
pointBackgroundColor: '#1f77b4',
4353+
}
4354+
]
4355+
};
4356+
4357+
// Update existing chart if it exists, otherwise create new one
4358+
if (window.elevationChartInstance) {
4359+
// Update data
4360+
window.elevationChartInstance.data = newData;
4361+
window.elevationChartInstance.options.plugins.title.text = chartTitle;
4362+
window.elevationChartInstance.options.scales.y.min = Math.floor(-10 + Math.min(minMission, minElevation));
4363+
window.elevationChartInstance.options.scales.y.max = Math.ceil(10 + Math.max(maxMission, maxElevation));
4364+
// Trigger re-render
4365+
window.elevationChartInstance.update();
4366+
} else {
4367+
// Create new chart
4368+
window.elevationChartInstance = new Chart(ctx, {
4369+
type: 'line',
4370+
data: newData,
4371+
options: {
4372+
responsive: true,
4373+
maintainAspectRatio: false,
4374+
plugins: {
43794375
title: {
43804376
display: true,
4381-
text: 'Distance (m)'
4377+
text: chartTitle
4378+
},
4379+
legend: {
4380+
display: true,
4381+
position: 'top',
43824382
}
43834383
},
4384-
y: {
4385-
title: {
4386-
display: true,
4387-
text: 'Elevation (m)'
4384+
scales: {
4385+
x: {
4386+
type: 'linear',
4387+
title: {
4388+
display: true,
4389+
text: 'Distance (m)'
4390+
}
43884391
},
4389-
min: Math.floor(-10 + Math.min(minMission, minElevation)),
4390-
max: Math.ceil(10 + Math.max(maxMission, maxElevation))
4392+
y: {
4393+
title: {
4394+
display: true,
4395+
text: 'Elevation (m)'
4396+
},
4397+
min: Math.floor(-10 + Math.min(minMission, minElevation)),
4398+
max: Math.ceil(10 + Math.max(maxMission, maxElevation))
4399+
}
43914400
}
43924401
}
4393-
}
4394-
});
4402+
});
4403+
}
43954404
} catch (error) {
43964405
console.error('Failed to plot elevation:', error);
43974406
}

0 commit comments

Comments
 (0)