Skip to content

Commit 9509f8f

Browse files
committed
responsive width and improvements
1 parent b618e81 commit 9509f8f

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/components/graphs/HeatMap.tsx

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,31 @@ interface HeatMapChartProps {
77
}
88

99
// Define margins for the chart
10-
const margin = { top: 10, right: 50, bottom: 40, left: 50 };
10+
const margin = { top: 10, right: 0, bottom: 40, left: 0 };
1111
// Define height for the chart, adjusting for margins
1212
const height = 300 - margin.top - margin.bottom;
1313

14-
// Define width of bars and adjust for screenwidth
15-
const barWidth = 0.05 * window.innerWidth < 40 ? 40 : 0.05 * window.innerWidth;
16-
const barGap = 5;
17-
1814
const HeatMapChart = ({ title, data }: HeatMapChartProps) => {
19-
const svgRef = useRef<SVGSVGElement>(null); // Reference to the SVG element
20-
const containerRef = useRef<HTMLDivElement>(null); // Reference to the container div
15+
const svgRef = useRef(null); // Reference to the SVG element
16+
const containerRef = useRef(null); // Reference to the container div
2117
const [containerWidth, setContainerWidth] = useState(800); // Default container width
2218

2319
useEffect(() => {
2420
// Clear any previous SVG content to avoid overlapping elements
2521
d3.select(svgRef.current).selectAll('*').remove();
2622

23+
const barWidth = Math.max(
24+
10,
25+
Math.floor(Math.min(containerWidth, 500) / data[0].length)
26+
);
27+
const barHeight = barWidth;
2728
// Create the SVG container and set its dimensions
2829
const svg = d3
2930
.select(svgRef.current)
30-
.attr('class', `min-h-[${height}px]`)
31-
.attr(
32-
'width',
33-
Math.max(
34-
containerWidth,
35-
margin.left + data.length * (barWidth + barGap)
36-
)
37-
)
38-
.attr(
39-
'height',
40-
data.length * (barWidth + barGap) + margin.top + margin.bottom
41-
)
42-
.append('g')
43-
.attr('transform', `translate(${margin.left},${margin.top})`);
31+
.attr('width', containerWidth)
32+
.attr('height', data.length * barHeight)
33+
.append('g');
34+
//.attr('transform', `translate(${margin.left},${margin.top})`);
4435

4536
svg.append('defs')
4637
.append('style')
@@ -53,13 +44,14 @@ const HeatMapChart = ({ title, data }: HeatMapChartProps) => {
5344
.scaleSequential()
5445
.domain([-1, 1])
5546
.interpolator(d3.interpolateBlues);
47+
5648
data.forEach((dataRow, rowIndex) => {
5749
dataRow.forEach((dataCell, cellIndex) => {
5850
svg.append('rect')
5951
.attr('x', cellIndex * barWidth)
60-
.attr('y', rowIndex * barWidth)
52+
.attr('y', rowIndex * barHeight)
6153
.attr('width', barWidth)
62-
.attr('height', barWidth)
54+
.attr('height', barHeight)
6355
.style('fill', function () {
6456
return colorScale(dataCell);
6557
});

0 commit comments

Comments
 (0)