Skip to content

Commit e683db1

Browse files
committed
Allow user to slide on chart
1 parent f88ca73 commit e683db1

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

webapp/script.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,8 @@ loadDataBtn.addEventListener('click', async function() {
918918
}
919919

920920
function initChart() {
921-
const ctx = document.getElementById('densityChart').getContext('2d');
921+
const canvas = document.getElementById('densityChart');
922+
const ctx = canvas.getContext('2d');
922923
if (chart) chart.destroy();
923924

924925
chart = new Chart(ctx, {
@@ -978,6 +979,48 @@ loadDataBtn.addEventListener('click', async function() {
978979
}
979980
}
980981
});
982+
983+
// Add drag behavior
984+
let isDragging = false;
985+
986+
const updateTimeFromEvent = (e) => {
987+
const points = chart.getElementsAtEventForMode(e, 'index', { intersect: false }, true);
988+
989+
if (points.length) {
990+
const firstPoint = points[0];
991+
const index = firstPoint.index;
992+
993+
// Update slider
994+
const timeSlider = document.getElementById('timeSlider');
995+
let currentDt = 300;
996+
if (densities.length > 1) {
997+
currentDt = Math.round((densities[1].datetime - densities[0].datetime) / 1000);
998+
if (currentDt <= 0) currentDt = 300;
999+
}
1000+
1001+
timeSlider.value = index * currentDt;
1002+
timeSlider.dispatchEvent(new Event('input'));
1003+
}
1004+
};
1005+
1006+
canvas.onmousedown = (e) => {
1007+
isDragging = true;
1008+
updateTimeFromEvent(e);
1009+
};
1010+
1011+
canvas.onmousemove = (e) => {
1012+
if (isDragging) {
1013+
updateTimeFromEvent(e);
1014+
}
1015+
};
1016+
1017+
canvas.onmouseup = () => {
1018+
isDragging = false;
1019+
};
1020+
1021+
canvas.onmouseleave = () => {
1022+
isDragging = false;
1023+
};
9811024
}
9821025

9831026
function updateChart() {

0 commit comments

Comments
 (0)