@@ -8,10 +8,11 @@ interface HeatMapChartProps {
88 rangeMin : number ;
99 rangeMax : number ;
1010 colors : 'RdYlBu' | 'LtRd' ;
11+ showLegend ?: boolean ;
1112}
1213
1314// Define margins for the chart
14- const margin = { top : 10 , right : 0 , bottom : 40 , left : 0 } ;
15+ const margin = { top : 50 , right : 0 , bottom : 40 , left : 0 } ;
1516// Define height for the chart, adjusting for margins
1617const height = 300 - margin . top - margin . bottom ;
1718
@@ -22,6 +23,7 @@ const HeatMapChart = ({
2223 rangeMin,
2324 rangeMax,
2425 colors,
26+ showLegend = true ,
2527} : HeatMapChartProps ) => {
2628 const svgRef = useRef ( null ) ; // Reference to the SVG element
2729 const containerRef = useRef ( null ) ; // Reference to the container div
@@ -37,10 +39,11 @@ const HeatMapChart = ({
3739 Math . floor ( Math . min ( widthForHeatmap , 500 ) / data [ 0 ] . length )
3840 ) ;
3941 const barHeight = barWidth ;
42+ const heatMapTop = 50 ;
4043 const svg = d3
4144 . select ( svgRef . current )
4245 . attr ( 'width' , containerWidth )
43- . attr ( 'height' , 50 + data . length * barHeight )
46+ . attr ( 'height' , heatMapTop + 50 + data . length * barHeight )
4447 . append ( 'g' ) ;
4548
4649 svg . append ( 'defs' )
@@ -94,7 +97,7 @@ const HeatMapChart = ({
9497 dataRow . forEach ( ( dataCell , cellIndex ) => {
9598 svg . append ( 'rect' )
9699 . attr ( 'x' , 50 + cellIndex * barWidth )
97- . attr ( 'y' , rowIndex * barHeight )
100+ . attr ( 'y' , heatMapTop + rowIndex * barHeight )
98101 . attr ( 'width' , barWidth )
99102 . attr ( 'height' , barHeight )
100103 . style ( 'fill' , function ( ) {
@@ -113,7 +116,10 @@ const HeatMapChart = ({
113116 const textColor = luminance < 0.5 ? 'white' : 'black' ;
114117 svg . append ( 'text' )
115118 . attr ( 'x' , barWidth / 2 + 50 + cellIndex * barWidth )
116- . attr ( 'y' , barHeight / 2 + rowIndex * barHeight )
119+ . attr (
120+ 'y' ,
121+ heatMapTop + barHeight / 2 + rowIndex * barHeight
122+ )
117123 . attr ( 'text-anchor' , 'middle' )
118124 . attr ( 'dominant-baseline' , 'middle' )
119125 . style ( 'fill' , textColor )
@@ -127,7 +133,7 @@ const HeatMapChart = ({
127133 . data ( data [ 0 ] )
128134 . join ( 'text' )
129135 . attr ( 'x' , ( _ , i ) => 50 + i * barWidth + barWidth / 2 )
130- . attr ( 'y' , 20 + barHeight * data . length )
136+ . attr ( 'y' , heatMapTop + 20 + barHeight * data . length )
131137 . attr ( 'text-anchor' , 'middle' )
132138 . style ( 'font-size' , '12px' )
133139 . text ( ( _ , i ) => `${ columns ?. [ i ] } ` ) ;
@@ -137,60 +143,77 @@ const HeatMapChart = ({
137143 . data ( data )
138144 . join ( 'text' )
139145 . attr ( 'x' , 40 )
140- . attr ( 'y' , ( _ , i ) => i * barHeight + barHeight / 2 )
146+ . attr ( 'y' , ( _ , i ) => heatMapTop + i * barHeight + barHeight / 2 )
141147 . attr ( 'dy' , '0.35em' )
142148 . attr ( 'text-anchor' , 'end' )
143149 . style ( 'font-size' , '12px' )
144150 . text ( ( _ , i ) => `${ columns ?. [ i ] } ` ) ;
145-
146- const svgBar = autoBandwidth ( seriesSvgBar ( ) )
147- . xScale ( xScale )
148- . yScale ( yScale )
149- . crossValue ( 0 )
150- . baseValue ( ( _ : unknown , i : number ) =>
151- i > 0 ? expandedDomain [ i - 1 ] : 0
152- )
153- . mainValue ( ( d : number ) => d )
154- . decorate (
155- (
156- selection : d3 . Selection <
157- SVGGElement ,
158- unknown ,
159- null ,
160- undefined
161- >
162- ) => {
163- selection
164- . selectAll ( 'path' )
165- . style ( 'fill' , d =>
166- colorScale ( d as unknown as d3 . NumberValue )
167- ) ;
168- }
151+ if ( showLegend ) {
152+ const svgBar = autoBandwidth ( seriesSvgBar ( ) )
153+ . xScale ( xScale )
154+ . yScale ( yScale )
155+ . crossValue ( 0 )
156+ . baseValue ( ( _ : unknown , i : number ) =>
157+ i > 0 ? expandedDomain [ i - 1 ] : 0
158+ )
159+ . mainValue ( ( d : number ) => d )
160+ . decorate (
161+ (
162+ selection : d3 . Selection <
163+ SVGGElement ,
164+ unknown ,
165+ null ,
166+ undefined
167+ >
168+ ) => {
169+ selection
170+ . selectAll ( 'path' )
171+ . style ( 'fill' , d =>
172+ colorScale ( d as unknown as d3 . NumberValue )
173+ ) ;
174+ }
175+ ) ;
176+
177+ const axisLabel = d3
178+ . axisRight ( yScale )
179+ . tickValues (
180+ colors === 'RdYlBu'
181+ ? [ ...domain , ( domain [ 1 ] + domain [ 0 ] ) / 2 ]
182+ : [ ...domain ]
183+ )
184+ . tickSizeOuter ( 0 ) ;
185+
186+ const legendBar = svg
187+ . append ( 'g' )
188+ . datum ( expandedDomain )
189+ . call ( svgBar ) ;
190+ legendBar . attr (
191+ 'transform' ,
192+ `translate(${ legendWidth - 50 + barWidth * data . length } ,${ heatMapTop } )`
169193 ) ;
170194
171- const axisLabel = d3
172- . axisRight ( yScale )
173- . tickValues (
174- colors === 'RdYlBu'
175- ? [ ...domain , ( domain [ 1 ] + domain [ 0 ] ) / 2 ]
176- : [ ...domain ]
177- )
178- . tickSizeOuter ( 0 ) ;
179-
180- const legendBar = svg . append ( 'g' ) . datum ( expandedDomain ) . call ( svgBar ) ;
181- legendBar . attr (
182- 'transform' ,
183- `translate(${ legendWidth - 50 + barWidth * data . length } ,0)`
184- ) ;
185- svg . append ( 'g' )
195+ svg . append ( 'g' )
196+ . attr (
197+ 'transform' ,
198+ `translate(${ legendWidth - 50 + barWidth * data . length + 20 } ,${ heatMapTop } )`
199+ )
200+ . datum ( expandedDomain )
201+ . call ( axisLabel )
202+ . select ( '.domain' )
203+ . attr ( 'visibility' , 'hidden' ) ;
204+ }
205+ d3 . select ( svgRef . current )
206+ . append ( 'text' )
186207 . attr (
187- 'transform' ,
188- `translate(${ legendWidth - 50 + barWidth * data . length + 20 } )`
208+ 'x' ,
209+ 50 + ( barWidth * data . length ) / 2
210+ //showLegend ? containerWidth / 2 : containerWidth / 2 - 50
189211 )
190- . datum ( expandedDomain )
191- . call ( axisLabel )
192- . select ( '.domain' )
193- . attr ( 'visibility' , 'hidden' ) ;
212+ . attr ( 'y' , 20 )
213+ . attr ( 'text-anchor' , 'middle' )
214+ . style ( 'font-size' , '16px' )
215+ . style ( 'font-weight' , 'bold' )
216+ . text ( `${ title } ` ) ;
194217 } , [ data , title , containerWidth ] ) ;
195218
196219 useEffect ( ( ) => {
@@ -217,7 +240,7 @@ const HeatMapChart = ({
217240 // Render the chart container and SVG element with horizontal scroll if needed
218241 return (
219242 < div className = "flex flex-col gap-4" >
220- < h3 className = "text-center font-semibold text-base" > { title } </ h3 >
243+ { /* <h3 className="text-center font-semibold text-base">{title}</h3> */ }
221244 < div
222245 ref = { containerRef }
223246 style = { { width : '100%' , display : 'flex' , overflowX : 'auto' } }
0 commit comments