@@ -3,94 +3,85 @@ import { IWaterfallGraphProps } from '../types/types';
33import { useWaterfallChart } from './utils' ;
44import styles from './styles.module.scss' ;
55import '../index.css' ;
6- import { DEFAULT_BAR_WIDTH } from '../constants' ;
6+ import { DEFAULT_BAR_WIDTH , DEFAULT_PIXELS_PER_Y_UNIT } from '../constants' ;
77
88const WaterFallChart : FC < IWaterfallGraphProps > = ( props ) => {
9- const { transactions } = props ;
9+ const { transactions, barWidth } = props ;
1010
1111 const wrapperRef = useRef < HTMLDivElement | null > ( null ) ;
1212 const [ wrapperHeight , setWrapperHeight ] = useState ( 0 ) ;
13+ const [ barWidthVal , setBarWidthVal ] = useState ( barWidth ?? DEFAULT_BAR_WIDTH ) ;
1314
14- useEffect ( ( ) => {
15+ const { chartElements, yValueForZeroLine, yAxisPoints, yAxisScale, calculateBarWidth } = useWaterfallChart (
16+ transactions ,
17+ wrapperHeight
18+ ) ;
1519
16- const calculateWrapperHeight = ( ) : void => {
20+ useEffect ( ( ) => {
21+ const onWrapperDimensionsChange = ( ) : void => {
1722 if ( wrapperRef . current ) {
18- const height = wrapperRef ?. current ?. offsetHeight ;
19- setWrapperHeight ( height ) ;
23+ setWrapperHeight ( wrapperRef ?. current ?. offsetHeight ) ;
24+ if ( ! barWidth ) setBarWidthVal ( calculateBarWidth ( wrapperRef ?. current ?. offsetWidth ) ) ;
2025 }
2126 } ;
2227
23- calculateWrapperHeight ( ) ;
28+ onWrapperDimensionsChange ( ) ;
2429
25- window . addEventListener ( 'resize' , calculateWrapperHeight ) ;
30+ window . addEventListener ( 'resize' , onWrapperDimensionsChange ) ;
2631
27- return ( ) => {
28- window . removeEventListener ( 'resize' , calculateWrapperHeight ) ;
29- } ;
30- } , [ ] ) ;
31-
32- const {
33- chartElements,
34- yAxisUnitsPerPixel,
35- yValueForZeroLine,
36- yAxisPoints
37- } = useWaterfallChart ( transactions , wrapperHeight ) ;
32+ return ( ) => window . removeEventListener ( 'resize' , onWrapperDimensionsChange ) ;
33+ } , [ barWidth , calculateBarWidth ] ) ;
3834
3935 return (
4036 < div ref = { wrapperRef } className = { styles . chartWrapper } >
4137 < svg className = { styles . svgContainer } >
4238 { /* y-axis */ }
43- < line
44- x1 = '0'
45- y1 = '0'
46- x2 = '0'
47- y2 = '100%'
48- className = { styles . axisLines }
49- />
39+ < line x1 = '0' y1 = '0' x2 = '0' y2 = '100%' className = { styles . axisLines } />
5040 { /* x-axis */ }
51- < line
52- x1 = '0'
53- y1 = '100%'
54- x2 = '100%'
55- y2 = '100%'
56- className = { styles . axisLines }
57- />
41+ < line x1 = '0' y1 = '100%' x2 = '100%' y2 = '100%' className = { styles . axisLines } />
5842 { /* zero line if negative values present */ }
59- < line
60- x1 = '0'
61- y1 = { yValueForZeroLine }
62- x2 = '100%'
63- y2 = { yValueForZeroLine }
64- className = { `${ styles . axisLines } ${ yValueForZeroLine === wrapperHeight && styles . hideLine } ` }
65- />
43+ { yAxisPoints ?. map ( ( yPoint , index ) => (
44+ < line
45+ key = { yPoint }
46+ x1 = '0'
47+ y1 = { wrapperHeight - index * DEFAULT_PIXELS_PER_Y_UNIT }
48+ x2 = '100%'
49+ y2 = { wrapperHeight - index * DEFAULT_PIXELS_PER_Y_UNIT }
50+ className = { `${ styles . axisLines } ` }
51+ />
52+ ) ) }
6653 { chartElements ?. map ( ( chartElement , index ) => (
6754 < >
6855 < rect
6956 key = { chartElement ?. name }
70- width = { DEFAULT_BAR_WIDTH }
71- height = { Math . abs ( chartElement ?. value / yAxisUnitsPerPixel ) }
57+ width = { barWidthVal }
58+ height = { ( Math . abs ( chartElement ?. value ) / yAxisScale ) * DEFAULT_PIXELS_PER_Y_UNIT }
7259 y = { chartElement ?. yVal }
73- x = { ( 2 * index + 1 ) * DEFAULT_BAR_WIDTH }
60+ x = { ( 2 * index + 1 ) * barWidthVal }
7461 className = { `${ styles . graphBar } ${ chartElement ?. value >= 0 ? styles . positiveGraph : styles . negativeGraph } ` }
7562 />
7663 < line
7764 key = { chartElement ?. name }
78- x1 = { ( 2 * index + 2 ) * DEFAULT_BAR_WIDTH }
79- y1 = { wrapperHeight - chartElement ?. cumulativeSum / yAxisUnitsPerPixel - ( wrapperHeight - yValueForZeroLine ) }
80- x2 = { ( 2 * index + 3 ) * DEFAULT_BAR_WIDTH }
81- y2 = { wrapperHeight - chartElement ?. cumulativeSum / yAxisUnitsPerPixel - ( wrapperHeight - yValueForZeroLine ) }
65+ x1 = { ( 2 * index + 2 ) * barWidthVal }
66+ y1 = {
67+ wrapperHeight -
68+ ( chartElement ?. cumulativeSum / yAxisScale ) * DEFAULT_PIXELS_PER_Y_UNIT -
69+ ( wrapperHeight - yValueForZeroLine )
70+ }
71+ x2 = { ( 2 * index + 3 ) * barWidthVal }
72+ y2 = {
73+ wrapperHeight -
74+ ( chartElement ?. cumulativeSum / yAxisScale ) * DEFAULT_PIXELS_PER_Y_UNIT -
75+ ( wrapperHeight - yValueForZeroLine )
76+ }
8277 className = { styles . bridgeLine }
8378 />
8479 </ >
8580 ) ) }
8681 </ svg >
8782 < div className = { styles . yPoints } >
8883 { yAxisPoints ?. map ( ( yAxisPoint , index ) => (
89- < div
90- key = { yAxisPoint }
91- className = { styles . yPoint }
92- style = { { bottom : ( index * 30 ) - 8 } }
93- >
84+ < div key = { yAxisPoint } className = { styles . yPoint } style = { { bottom : index * DEFAULT_PIXELS_PER_Y_UNIT - 8 } } >
9485 { yAxisPoint }
9586 </ div >
9687 ) ) }
0 commit comments