|
| 1 | +import React, { FC, useEffect, useRef, useState } from "react"; |
| 2 | +import { IWaterfallGraphProps } from "../types/types"; |
| 3 | +import { useWaterfallChart } from './utils'; |
| 4 | +import styles from './styles.module.scss'; |
| 5 | +import '../index.css'; |
| 6 | +import { DEFAULT_BAR_WIDTH } from "../constants"; |
| 7 | + |
| 8 | +const WaterFallChart:FC<IWaterfallGraphProps> = (props) => { |
| 9 | + const { transactions } = props; |
| 10 | + |
| 11 | + const wrapperRef = useRef<HTMLDivElement | null>(null); |
| 12 | + const [wrapperHeight, setWrapperHeight] = useState(0); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + // Function to calculate wrapper height |
| 16 | + const calculateWrapperHeight = (): void => { |
| 17 | + if (wrapperRef.current) { |
| 18 | + const height = wrapperRef?.current?.offsetHeight; // Use offsetHeight to get the height |
| 19 | + setWrapperHeight(height); // Update state with calculated height |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + calculateWrapperHeight(); // Call the function initially |
| 24 | + |
| 25 | + // Add event listener for window resize to recalculate wrapper height |
| 26 | + window.addEventListener('resize', calculateWrapperHeight); |
| 27 | + |
| 28 | + return () => { |
| 29 | + // Cleanup: remove event listener on component unmount |
| 30 | + window.removeEventListener('resize', calculateWrapperHeight); |
| 31 | + }; |
| 32 | + }, []); |
| 33 | + const { chartElements, yAxisUnitsPerPixel, yValueForZeroLine } = useWaterfallChart(transactions, wrapperHeight); |
| 34 | + |
| 35 | + return ( |
| 36 | + <div |
| 37 | + ref={wrapperRef} |
| 38 | + className={styles.chartWrapper} |
| 39 | + > |
| 40 | + <svg className={styles.svgContainer}> |
| 41 | + {/* y-axis */} |
| 42 | + <line x1="0" y1="0" x2="0" y2="100%" style={{ strokeDasharray: 1, stroke: 'rgb(158, 158, 158)', strokeWidth: 2}} /> |
| 43 | + {/* x-axis */} |
| 44 | + <line x1="0" y1="100%" x2="100%" y2="100%" style={{ strokeDasharray: 1, stroke: 'rgb(158, 158, 158)', strokeWidth: 2}} /> |
| 45 | + {yValueForZeroLine > 0 && ( |
| 46 | + // zero line if negative values present |
| 47 | + <line |
| 48 | + x1="0" |
| 49 | + y1={yValueForZeroLine} |
| 50 | + x2="100%" |
| 51 | + y2={yValueForZeroLine} |
| 52 | + style={{ strokeDasharray: 1, stroke: 'rgb(158, 158, 158)', strokeWidth: 2}} |
| 53 | + /> |
| 54 | + )} |
| 55 | + {chartElements?.map((chartElement, index) => ( |
| 56 | + <> |
| 57 | + <rect |
| 58 | + key={chartElement?.name} |
| 59 | + width={DEFAULT_BAR_WIDTH} |
| 60 | + height={Math.abs(chartElement?.value / yAxisUnitsPerPixel)} |
| 61 | + y={chartElement?.yVal} |
| 62 | + x={((2*index)+1) * DEFAULT_BAR_WIDTH} |
| 63 | + style={{ |
| 64 | + fill: chartElement?.value > 0 ? '#00B050' : '#FF0000', |
| 65 | + strokeWidth: 2, |
| 66 | + stroke: chartElement?.value > 0 ? '#00B050' : '#FF0000', |
| 67 | + zIndex: 1, |
| 68 | + transition: 'all 0.3s ease-in-out' |
| 69 | + }} /> |
| 70 | + <line |
| 71 | + key={chartElement?.name} |
| 72 | + x1={((2*index)+2) * DEFAULT_BAR_WIDTH} |
| 73 | + y1={wrapperHeight - (chartElement?.cumulativeSum / yAxisUnitsPerPixel) - (wrapperHeight - yValueForZeroLine)} |
| 74 | + x2={((2*index)+3) * DEFAULT_BAR_WIDTH} |
| 75 | + y2={wrapperHeight - (chartElement?.cumulativeSum / yAxisUnitsPerPixel) - (wrapperHeight - yValueForZeroLine)} |
| 76 | + style={{ |
| 77 | + stroke: '#545453', |
| 78 | + strokeWidth: 2, |
| 79 | + zIndex: 1 |
| 80 | + }} /> |
| 81 | + </> |
| 82 | + ))} |
| 83 | + </svg> |
| 84 | + </div> |
| 85 | + ); |
| 86 | +}; |
| 87 | + |
| 88 | +export default WaterFallChart; |
0 commit comments