@@ -5,93 +5,90 @@ import './TotalReportBarGraph.css';
55function TotalReportBarGraph ( { barData, range } ) {
66 const svgId = `svg-container-${ range } ` ;
77
8- const drawChart = data => {
8+ const drawChart = ( data ) => {
99 data . sort ( ( a , b ) => ( a . label > b . label ? 1 : - 1 ) ) ;
10- const maxValue = Number (
11- data . reduce ( ( prev , curr ) => ( prev . value - curr . value > 0 ? prev : curr ) ) . value ,
12- ) ;
13- const margin = { top : 10 , right : 8 , bottom : 15 , left : 20 } ;
14- const width = 500 - margin . left - margin . right ;
15- const height = 300 - margin . top - margin . bottom ;
16-
17- const svg = d3 . select ( `#${ svgId } ` ) ;
10+
11+ const svgWidth = 500 ;
12+ const svgHeight = 450 ;
13+ const margin = { top : 10 , right : 8 , bottom : 100 , left : 20 } ;
14+ const width = svgWidth - margin . left - margin . right ;
15+ const height = svgHeight - margin . top - margin . bottom ;
16+
17+ const maxValue = Math . max ( ...data . map ( d => d . value ) ) ;
18+
19+ const svg = d3
20+ . select ( `#${ svgId } ` )
21+ . attr ( 'width' , svgWidth )
22+ . attr ( 'height' , svgHeight ) ;
23+
1824 svg . selectAll ( '*' ) . remove ( ) ;
19- const chart = svg . append ( 'g' ) . attr ( 'transform' , `translate(${ margin . left } , ${ margin . top } )` ) ;
25+
26+ const chart = svg
27+ . append ( 'g' )
28+ . attr ( 'transform' , `translate(${ margin . left } , ${ margin . top } )` ) ;
2029
2130 const xScale = d3
2231 . scaleBand ( )
32+ . domain ( data . map ( d => d . label ) )
2333 . range ( [ 0 , width ] )
24- . domain ( data . map ( s => s . label ) )
2534 . padding ( 0.4 ) ;
2635
2736 const yScale = d3
2837 . scaleLinear ( )
29- . range ( [ height , 10 ] )
30- . domain ( [ 0 , maxValue ] ) ;
38+ . domain ( [ 0 , maxValue ] )
39+ . range ( [ height , 0 ] ) ;
3140
3241 const colorScale = d3
3342 . scaleLinear ( )
3443 . domain ( [ 0 , maxValue ] )
3544 . range ( [ 'darksalmon' , 'darkslateblue' ] ) ;
3645
46+ data . forEach ( d => {
47+ const x = xScale ( d . label ) ;
48+ const barHeight = height - yScale ( d . value ) ;
49+ const y = yScale ( d . value ) ;
50+
51+
52+ chart
53+ . append ( 'rect' )
54+ . attr ( 'x' , x )
55+ . attr ( 'y' , y )
56+ . attr ( 'width' , xScale . bandwidth ( ) )
57+ . attr ( 'height' , barHeight )
58+ . attr ( 'fill' , colorScale ( d . value ) ) ;
59+
60+
61+ const yText =
62+ barHeight >= 30
63+ ? y + barHeight / 2
64+ :
65+ y - 10 ;
66+
67+ chart
68+ . append ( 'text' )
69+ . attr ( 'x' , x + xScale . bandwidth ( ) / 2 )
70+ . attr ( 'y' , yText )
71+ . attr ( 'text-anchor' , 'middle' )
72+ . style ( 'fill' , 'white' )
73+ . style ( 'font-size' , '20px' )
74+ . style ( 'font-weight' , 'bold' )
75+ . text ( Number . isNaN ( d . value ) ? '' : d . value ) ;
76+ } ) ;
77+
78+
3779 chart
3880 . append ( 'g' )
3981 . attr ( 'transform' , `translate(0, ${ height } )` )
40- . call ( d3 . axisBottom ( xScale ) ) ;
41-
42- const barGroups = chart
43- . selectAll ( )
44- . data ( data )
45- . enter ( )
46- . append ( 'g' ) ;
47-
48- barGroups
49- . append ( 'rect' )
50- . attr ( 'class' , 'bar' )
51- . attr ( 'x' , d => xScale ( d . label ) )
52- . attr ( 'y' , d => yScale ( d . value ) )
53- . attr ( 'height' , d => height - yScale ( d . value ) )
54- . attr ( 'width' , xScale . bandwidth ( ) )
55- . attr ( 'fill' , d => colorScale ( d . value ) )
56- // eslint-disable-next-line no-unused-vars
57- . on ( 'mouseenter' , ( event , i ) => {
58- d3 . selectAll ( '.value' ) . attr ( 'opacity' , 0 ) ;
59- d3 . select ( event . currentTarget )
60- . transition ( )
61- . duration ( 300 )
62- . attr ( 'opacity' , 0.6 ) ;
63- barGroups
64- . append ( 'text' )
65- . attr ( 'class' , 'value' )
66- . attr ( 'x' , d => xScale ( d . label ) + xScale . bandwidth ( ) / 2 )
67- . attr ( 'y' , d => yScale ( d . value ) )
68- . attr ( 'text-anchor' , 'middle' )
69- . text ( d => `${ d . value } ` )
70- . style ( 'fill' , 'black' ) ;
71-
72- if ( data [ 0 ] . months ) {
73- barGroups
74- . append ( 'text' )
75- . attr ( 'class' , 'value' )
76- . attr ( 'x' , d => xScale ( d . label ) + xScale . bandwidth ( ) / 2 )
77- . attr ( 'y' , yScale ( 0 ) + 30 )
78- . attr ( 'text-anchor' , 'middle' )
79- . text ( d => `${ d . months } mos.` )
80- . style ( 'fill' , 'black' ) ;
81- }
82- } )
83- . on ( 'mouseleave' , ( event ) => {
84- d3 . selectAll ( '.value' ) . attr ( 'opacity' , 1 ) ;
85- d3 . select ( event . currentTarget )
86- . transition ( )
87- . duration ( 300 )
88- . attr ( 'opacity' , 1 ) ;
89- chart . selectAll ( '.value' ) . remove ( ) ;
90- } ) ;
82+ . call ( d3 . axisBottom ( xScale ) )
83+ . selectAll ( 'text' )
84+ . attr ( 'dy' , '1em' )
85+ . style ( 'text-anchor' , 'middle' )
86+ . style ( 'font-size' , '14px' )
87+ . style ( 'fill' , 'black' ) ;
9188 } ;
9289
9390 useEffect ( ( ) => {
94- if ( barData . length > 0 ) {
91+ if ( barData && barData . length ) {
9592 drawChart ( barData ) ;
9693 }
9794 } , [ barData ] ) ;
0 commit comments