Skip to content

Commit 0a0497d

Browse files
committed
refactor: progress bar uses ticks/index instead of years
1 parent bc97fb1 commit 0a0497d

File tree

2 files changed

+26
-41
lines changed

2 files changed

+26
-41
lines changed

src/Components/Infographics/BarChartRace/barChartRace.jsx

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'font-awesome/css/font-awesome.min.css';
88
import api from '../../../api/axios';
99

1010
/**
11-
* BarChartRace component visualizes a bar chart race with play/pause and year selection controls.
11+
* BarChartRace component visualizes a bar chart race with play/pause and selection controls.
1212
*
1313
* @param {Object} props - Component props
1414
* @param {string} props.title - Title of the chart
@@ -22,31 +22,27 @@ const BarChartRace = ({ title, speed, colorPalette, barRaceData, isDownloadingVi
2222
const svgRef = useRef(null); // Reference to the SVG element
2323
const currentKeyframeRef = useRef(0); // Tracks the current keyframe
2424
const [isPlaying, setIsPlaying] = useState(false); // Play/pause state
25-
const [startYear, setStartYear] = useState(0); // Minimum year in data
26-
const [endYear, setEndYear] = useState(0); // Maximum year in data
27-
const [year, setYear] = useState(startYear); // Current selected year
25+
const [progressBarTick, setProgressBarTick] = useState(0);
26+
const [progressBarMaxTick, setProgressBarMaxTick] = useState(0);
2827
const [dataset, setDataset] = useState(null); // Processed data
2928
const [currentKeyframeState, setCurrentKeyFrameState] = useState(0); // Current keyframe state
3029
const [downloadTimeLeft, setDownloadTimeLeft] = useState(null);
3130
const keyframesRef = useRef([]); // Stores all keyframes
3231
const timeoutRef = useRef(null); // Handles animation timing
33-
const inputRef = useRef(null); // Reference to range input
3432
const abortControllerRef = useRef(null);
3533
const videoIdRef = useRef(null);
3634

3735
useEffect(() => {
3836
const fetchDataAsync = () => {
3937
if (barRaceData) {
38+
var keyframes = barRaceData.values_by_date.map(d => [new Date(d.date), d.values]);
39+
4040
const dataset = {
4141
"elements": barRaceData.elements,
42-
"keyframes": barRaceData.values_by_date.map(d => [new Date(d.date), d.values])
43-
}
42+
"keyframes": keyframes
43+
};
4444
setDataset(dataset);
45-
46-
// Find and set the minimum and maximum years
47-
const years = dataset.keyframes.map(d => d[0].getFullYear());
48-
setStartYear(Math.min(...years));
49-
setEndYear(Math.max(...years));
45+
setProgressBarMaxTick(keyframes.length - 1);
5046
}
5147
};
5248
fetchDataAsync();
@@ -73,9 +69,9 @@ const BarChartRace = ({ title, speed, colorPalette, barRaceData, isDownloadingVi
7369

7470
// Initialize chart with the first keyframe.
7571
clearTimeout(timeoutRef.current);
76-
setYear(startYear);
72+
setProgressBarTick(0);
7773
currentKeyframeRef.current = 0;
78-
updateChart(keyframes[0], svgRef.current.transition().duration(0), inputRef, null);
74+
updateChart(keyframes[0], svgRef.current.transition().duration(0));
7975
setIsDownloadingVideo(false);
8076
}
8177
const currentSvg = svgRef.current;
@@ -110,13 +106,12 @@ const BarChartRace = ({ title, speed, colorPalette, barRaceData, isDownloadingVi
110106
if (canIncreaseAnimationTick()) {
111107
const keyframe = keyframesRef.current[currentKeyframeRef.current];
112108

113-
updateChart(keyframe, transition, inputRef, null);
114-
currentKeyframeRef.current += 1;
109+
updateChart(keyframe, transition);
115110
setCurrentKeyFrameState(keyframe);
116111

117112
// Sync range input with keyframe
118-
inputRef.current.value = keyframe[0].getFullYear();
119-
setYear(keyframe[0].getFullYear());
113+
setProgressBarTick(currentKeyframeRef.current);
114+
currentKeyframeRef.current += 1;
120115
return true;
121116
} else {
122117
return false;
@@ -131,30 +126,30 @@ const BarChartRace = ({ title, speed, colorPalette, barRaceData, isDownloadingVi
131126
if (isPlaying) {
132127
clearTimeout(timeoutRef.current);
133128
const transition = getTransition(DEFAULT_TRANSITION_DELAY);
134-
updateChart(currentKeyframeState, transition, inputRef, null);
129+
updateChart(currentKeyframeState, transition);
135130
} else {
136131
startAnimation();
137132
}
138133

139-
if (!(year >= endYear)) {
134+
if (!(progressBarTick >= progressBarMaxTick)) {
140135
setIsPlaying(!isPlaying);
141136
}
142137
};
143138

144139
const onRangeChange = (event) => {
145-
const selectedYear = parseInt(event.target.value, 10);
146-
setAnimationToYear(selectedYear);
140+
const selectedTick = parseInt(event.target.value, 10);
141+
setAnimationToTick(selectedTick);
147142
};
148143

149-
const setAnimationToYear = (year) => {
150-
const frameIndex = keyframesRef.current.findIndex(frame => frame[0].getFullYear() === year);
144+
const setAnimationToTick = (tick) => {
145+
const frameIndex = tick;
151146
if (frameIndex !== -1) {
152147
clearTimeout(timeoutRef.current);
153148
setIsPlaying(false);
154-
setYear(year);
149+
setProgressBarTick(tick);
155150
const transition = getTransition(DEFAULT_TRANSITION_DELAY);
156151
currentKeyframeRef.current = frameIndex; // Set current keyframe
157-
updateChart(keyframesRef.current[frameIndex], transition, inputRef, null);
152+
updateChart(keyframesRef.current[frameIndex], transition);
158153
}
159154
}
160155

@@ -167,7 +162,7 @@ const BarChartRace = ({ title, speed, colorPalette, barRaceData, isDownloadingVi
167162
api.post('/video/create/').then((response) => {
168163
if (response.status == 201) {
169164
videoIdRef.current = response.data.id;
170-
setAnimationToYear(startYear);
165+
setAnimationToTick(0);
171166
startTimeLeft();
172167
timeoutRef.current = setTimeout(startDownloadAnimation, animationDelay() * 2);
173168
} else {
@@ -278,12 +273,11 @@ const BarChartRace = ({ title, speed, colorPalette, barRaceData, isDownloadingVi
278273
<input
279274
id="play-range"
280275
type="range"
281-
value={year}
282-
min={startYear}
283-
max={endYear}
276+
min="0"
277+
value={progressBarTick}
278+
max={progressBarMaxTick}
284279
className="ml-1"
285280
onChange={onRangeChange}
286-
ref={inputRef}
287281
/>
288282
</div>
289283
<div id="container"></div>

src/Components/Infographics/BarChartRace/barChartRaceUtils.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,14 @@ export const initializeChart = (svgRef, dataset, width, title, colorPaletteArray
7171
}
7272

7373
// Function to update the chart
74-
export const updateChart = (keyframe, transition, inputRef, increment) => {
74+
export const updateChart = (keyframe, transition) => {
7575

7676
// Update based on keyframe
7777
x.domain([0, keyframe[1][0].value]);
7878
updateAxis(keyframe, transition);
7979
updateBars(keyframe, transition);
8080
updateLabels(keyframe, transition);
8181
updateTicker(keyframe, transition);
82-
83-
// Update range input value if increment is true and within bounds
84-
if (increment) {
85-
const newValue = parseInt(inputRef.current.value, 10) + increment;
86-
if (newValue <= parseInt(inputRef.current.max, 10)) {
87-
inputRef.current.value = newValue;
88-
}
89-
}
90-
9182
};
9283

9384

0 commit comments

Comments
 (0)