Skip to content

Commit d0d8789

Browse files
committed
x-axis and y-axis labels
1 parent 9509f8f commit d0d8789

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/components/graphs/HeatMap.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ const HeatMapChart = ({ title, data }: HeatMapChartProps) => {
1919
useEffect(() => {
2020
// Clear any previous SVG content to avoid overlapping elements
2121
d3.select(svgRef.current).selectAll('*').remove();
22-
22+
const widthForHeatmap = containerWidth - 50;
2323
const barWidth = Math.max(
2424
10,
25-
Math.floor(Math.min(containerWidth, 500) / data[0].length)
25+
Math.floor(Math.min(widthForHeatmap, 500) / data[0].length)
2626
);
2727
const barHeight = barWidth;
2828
// Create the SVG container and set its dimensions
2929
const svg = d3
3030
.select(svgRef.current)
3131
.attr('width', containerWidth)
32-
.attr('height', data.length * barHeight)
32+
.attr('height', 50 + data.length * barHeight)
3333
.append('g');
3434
//.attr('transform', `translate(${margin.left},${margin.top})`);
3535

@@ -48,7 +48,7 @@ const HeatMapChart = ({ title, data }: HeatMapChartProps) => {
4848
data.forEach((dataRow, rowIndex) => {
4949
dataRow.forEach((dataCell, cellIndex) => {
5050
svg.append('rect')
51-
.attr('x', cellIndex * barWidth)
51+
.attr('x', 50 + cellIndex * barWidth)
5252
.attr('y', rowIndex * barHeight)
5353
.attr('width', barWidth)
5454
.attr('height', barHeight)
@@ -57,6 +57,29 @@ const HeatMapChart = ({ title, data }: HeatMapChartProps) => {
5757
});
5858
});
5959
});
60+
61+
// Add x-axis labels (columns)
62+
svg.append('g')
63+
.selectAll('text')
64+
.data(data[0])
65+
.join('text')
66+
.attr('x', (_, i) => 50 + i * barWidth + barWidth / 2)
67+
.attr('y', 50 + barHeight * data.length)
68+
.attr('text-anchor', 'middle')
69+
.style('font-size', '12px')
70+
.text((_, i) => `Cell ${i + 1}`);
71+
72+
// Add y-axis labels (rows)
73+
svg.append('g')
74+
.selectAll('text')
75+
.data(data)
76+
.join('text')
77+
.attr('x', 40)
78+
.attr('y', (_, i) => i * barHeight + barHeight / 2)
79+
.attr('dy', '0.35em')
80+
.attr('text-anchor', 'end')
81+
.style('font-size', '12px')
82+
.text((_, i) => `Row ${i + 1}`);
6083
}, [data, title, containerWidth]);
6184

6285
useEffect(() => {

0 commit comments

Comments
 (0)