Skip to content

Commit a5eb8db

Browse files
authored
Merge pull request #56 from UTSC-CSCC01-Software-Engineering-I/fix/bugs_features
fixed the color points bug with unit toggle
2 parents 9263e87 + b67b8ba commit a5eb8db

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

frontend/src/components/MapComponent.jsx

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -610,44 +610,45 @@ export default function MapComponent() {
610610
const removeUnitListener = UnitManager.addUnitChangeListener((newUnit) => {
611611
setUnit(newUnit); // Update local state for legend
612612

613-
markersRef.current.forEach(({ marker, tempC, name }) => {
614-
// Use formatTemperature utility for consistent formatting
613+
markersRef.current.forEach(({ marker, tempC }) => {
614+
// 1) compute the new label
615615
const formattedTemp = formatTemperature(tempC, newUnit);
616-
const tempColor = getTemperatureColor(tempC, 'C'); // Always calculate color from Celsius
617-
618-
// Update marker icon
619-
marker.setIcon(window.L.divIcon({
620-
className: 'custom-temp-marker',
621-
html: `<div class="temp-label" style="background-color: ${tempColor};">${formattedTemp}</div>`,
622-
iconSize: [40, 40],
623-
iconAnchor: [20, 20],
624-
}));
625616

626-
// Update the graph if it exists
627-
if (marker.chartInstance && marker.chartData) {
628-
const chart = marker.chartInstance;
629-
630-
// Convert original data to new unit with time structure
631-
const newTimeBasedData = marker.chartData.map((d) => ({
632-
x: new Date(d.timestamp),
633-
y: newUnit === 'F' ? (d.temp * 9) / 5 + 32 : d.temp
634-
}));
617+
// 2) find the existing <span class="temp-value"> and update it
618+
const el = marker.getElement();
619+
if (el) {
620+
const valueSpan = el.querySelector('.temp-value');
621+
if (valueSpan) valueSpan.textContent = formattedTemp;
622+
}
635623

636-
// Update dataset with time-based data
637-
chart.data.datasets[0].data = newTimeBasedData;
624+
// 3) leave the background‐color alone so it never “snaps back” after a refresh
625+
// (skip marker.setIcon entirely)
626+
});
638627

639-
// Update the y-axis title
640-
chart.options.scales.y.title.text = `Temperature (°${newUnit})`;
641-
642-
// Update y-axis tick callback
643-
chart.options.scales.y.ticks.callback = function(value) {
644-
return `${value}°${newUnit}`;
645-
};
628+
// Update the graph if it exists
629+
if (marker.chartInstance && marker.chartData) {
630+
const chart = marker.chartInstance;
631+
632+
// Convert original data to new unit with time structure
633+
const newTimeBasedData = marker.chartData.map((d) => ({
634+
x: new Date(d.timestamp),
635+
y: newUnit === 'F' ? (d.temp * 9) / 5 + 32 : d.temp
636+
}));
646637

647-
// Apply the updates
648-
chart.update();
649-
}
650-
});
638+
// Update dataset with time-based data
639+
chart.data.datasets[0].data = newTimeBasedData;
640+
641+
// Update the y-axis title
642+
chart.options.scales.y.title.text = `Temperature (°${newUnit})`;
643+
644+
// Update y-axis tick callback
645+
chart.options.scales.y.ticks.callback = function(value) {
646+
return `${value}°${newUnit}`;
647+
};
648+
649+
// Apply the updates
650+
chart.update();
651+
}
651652
});
652653

653654
// Listen for theme changes

0 commit comments

Comments
 (0)