Skip to content

Commit da7c0c0

Browse files
committed
refactor: increase bar chart race size
1 parent 41088ad commit da7c0c0

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/Components/Infographics/BarChartRace/barChartRace.jsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const BarChartRace = ({ title, speed, colorPalette, timeUnit, barRaceData, isDow
2828
const [dataset, setDataset] = useState(null); // Processed data
2929
const [currentKeyframeState, setCurrentKeyFrameState] = useState(0); // Current keyframe state
3030
const [downloadPercentage, setDownloadPercentage] = useState(null);
31+
const [graphicsPanelHeight, setGraphicsPanelHeight] = useState(getGraphicsPanelHeight());
3132
const keyframesRef = useRef([]); // Stores all keyframes
3233
const timeoutRef = useRef(null); // Handles animation timing
3334
const abortControllerRef = useRef(null);
@@ -72,7 +73,8 @@ const BarChartRace = ({ title, speed, colorPalette, timeUnit, barRaceData, isDow
7273
};
7374

7475
const width = container.clientWidth;
75-
const keyframes = initializeChart(svgRef, dataset, width, title, colorPaletteArray, timeUnit, locale);
76+
const maxHeight = graphicsPanelHeight;
77+
const keyframes = initializeChart(svgRef, dataset, width, maxHeight, title, colorPaletteArray, timeUnit, locale);
7678
keyframesRef.current = keyframes;
7779

7880
// Initialize chart with the first keyframe.
@@ -89,8 +91,20 @@ const BarChartRace = ({ title, speed, colorPalette, timeUnit, barRaceData, isDow
8991
currentSvg.remove();
9092
}
9193
};
94+
}, [dataset, timeUnit, title, colorPalette, locale, graphicsPanelHeight]);
9295

93-
}, [dataset, timeUnit, title, colorPalette, locale]);
96+
function getGraphicsPanelHeight() {
97+
return document.getElementById("graphicsPanel").clientHeight;
98+
};
99+
100+
useEffect(() => {
101+
function handleResize() {
102+
setGraphicsPanelHeight(getGraphicsPanelHeight());
103+
}
104+
window.addEventListener('resize', handleResize);
105+
// cleanup:
106+
return () => window.removeEventListener('resize', handleResize);
107+
}, []);
94108

95109
const animationDelay = () => {
96110
return 1000 / speed;
@@ -255,7 +269,7 @@ const BarChartRace = ({ title, speed, colorPalette, timeUnit, barRaceData, isDow
255269
}
256270

257271
return (
258-
<div id="parent-container" className="relative p-4">
272+
<div id="parent-container" className="relative p-4" style={{height: "90%"}}>
259273
<DownloadingAlert/>
260274
<div id="play-controls" className="flex items-center">
261275
<button
@@ -274,7 +288,7 @@ const BarChartRace = ({ title, speed, colorPalette, timeUnit, barRaceData, isDow
274288
onChange={onRangeChange}
275289
/>
276290
</div>
277-
<div id="container"></div>
291+
<div id="container" style={{height: "95%"}}></div>
278292
</div>
279293
);
280294
};

src/Components/Infographics/BarChartRace/barChartRaceUtils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as d3 from "d3";
55

66
// Constants
77
export const margin = { top: 16, right: 16, bottom: 16, left: 0 }; // Added padding/margin
8-
export const barSize = 38;
98
export const maxNumberVisible = 12;
109
export let color;
1110

@@ -14,9 +13,10 @@ let updateBars, updateAxis, updateLabels, updateTicker, x;
1413
let dateFormatter;
1514

1615
// Function to initialize the chart
17-
export const initializeChart = (svgRef, dataset, width, title, colorPaletteArray, timeUnit, locale) => {
16+
export const initializeChart = (svgRef, dataset, width, maxHeight, title, colorPaletteArray, timeUnit, locale) => {
1817
const chartMarginTop = 30;
1918
const chartMarginBottom = 12;
19+
const barSize = 0.8 * (maxHeight - margin.top - margin.bottom - chartMarginBottom)/maxNumberVisible;
2020

2121
// Create SVG element
2222
svgRef.current = d3
@@ -75,9 +75,9 @@ export const initializeChart = (svgRef, dataset, width, title, colorPaletteArray
7575

7676
// Initialize update functions
7777
updateBars = bars(svgRef.current, x, y, prev, next);
78-
updateAxis = axis(svgRef.current, x, y, width);
78+
updateAxis = axis(svgRef.current, x, y, width, barSize);
7979
updateLabels = labels(svgRef.current, x, y, prev, next);
80-
updateTicker = ticker(svgRef.current, width, keyframes);
80+
updateTicker = ticker(svgRef.current, width, keyframes, barSize);
8181

8282
return keyframes;
8383
}
@@ -97,7 +97,7 @@ export const updateChart = (keyframe, transition) => {
9797
// Helper functions
9898

9999
// Ticker function
100-
function ticker(svgRef, width, keyframes) {
100+
function ticker(svgRef, width, keyframes, barSize) {
101101
const now = svgRef
102102
.append("text")
103103
.style("font", `bold ${barSize}px var(--sans-serif)`)
@@ -198,7 +198,7 @@ function textTween(a, b) {
198198
}
199199

200200
// Axis function
201-
function axis(svgRef, x, y, width) {
201+
function axis(svgRef, x, y, width, barSize) {
202202
const g = svgRef
203203
.append("g")
204204
.attr("transform", `translate(0,${margin.top})`);

src/Pages/Infographics.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ const Infographics = () => {
138138
<div style={{minHeight: "82vh"}} className="flex-1 border dark:border-gray-800 bg-white dark:bg-gray-600">
139139
<CodeEditor onCodeChange={handleCodeChange} handleFetchChartData={getChartData} isLoading={isLoading} errorMessage={error}/>
140140
</div>
141-
<div style={{minHeight: "82vh", minWidth: "50%" }} className="flex-1 border dark:border-gray-800 relative overflow-x-auto bg-white dark:bg-gray-600">
141+
<div id="graphicsPanel" style={{minHeight: "82vh", minWidth: "50%" }} className="flex-1 border dark:border-gray-800 relative overflow-x-auto bg-white dark:bg-gray-600">
142142
{isLoading && <Overlay />}
143143
{error && <div className="flex items-center justify-center mt-7"><ErrorAlert alertText={getContent(error) || error} /></div>}
144144
{Object.keys(chartData).length < 1 && !error && <div className="flex items-center justify-center mt-7"><InfoAlert alertText={getContent("preview-no-data")} /></div>}

0 commit comments

Comments
 (0)