|
1 | | -import React, { FC, useEffect, useRef, useState } from "react"; |
2 | | -import { IWaterfallGraphProps } from "../types/types"; |
3 | | -import { useWaterfallChart } from './utils'; |
| 1 | +import React, { FC, useEffect, useRef, useState } from 'react'; |
| 2 | +import { IWaterfallGraphProps } from '../types/types'; |
| 3 | +import { useWaterfallChart } from './utils'; |
4 | 4 | import styles from './styles.module.scss'; |
5 | 5 | import '../index.css'; |
6 | | -import { DEFAULT_BAR_WIDTH } from "../constants"; |
| 6 | +import { DEFAULT_BAR_WIDTH } from '../constants'; |
7 | 7 |
|
8 | | -const WaterFallChart:FC<IWaterfallGraphProps> = (props) => { |
| 8 | +const WaterFallChart: FC<IWaterfallGraphProps> = (props) => { |
9 | 9 | const { transactions } = props; |
10 | 10 |
|
11 | 11 | const wrapperRef = useRef<HTMLDivElement | null>(null); |
12 | 12 | const [wrapperHeight, setWrapperHeight] = useState(0); |
13 | 13 |
|
14 | 14 | useEffect(() => { |
15 | | - // Function to calculate wrapper height |
| 15 | + |
16 | 16 | const calculateWrapperHeight = (): void => { |
17 | 17 | if (wrapperRef.current) { |
18 | | - const height = wrapperRef?.current?.offsetHeight; // Use offsetHeight to get the height |
19 | | - setWrapperHeight(height); // Update state with calculated height |
| 18 | + const height = wrapperRef?.current?.offsetHeight; |
| 19 | + setWrapperHeight(height); |
20 | 20 | } |
21 | 21 | }; |
22 | 22 |
|
23 | | - calculateWrapperHeight(); // Call the function initially |
| 23 | + calculateWrapperHeight(); |
24 | 24 |
|
25 | | - // Add event listener for window resize to recalculate wrapper height |
26 | 25 | window.addEventListener('resize', calculateWrapperHeight); |
27 | 26 |
|
28 | 27 | return () => { |
29 | | - // Cleanup: remove event listener on component unmount |
30 | 28 | window.removeEventListener('resize', calculateWrapperHeight); |
31 | 29 | }; |
32 | 30 | }, []); |
33 | | - const { chartElements, yAxisUnitsPerPixel, yValueForZeroLine } = useWaterfallChart(transactions, wrapperHeight); |
| 31 | + |
| 32 | + const { |
| 33 | + chartElements, |
| 34 | + yAxisUnitsPerPixel, |
| 35 | + yValueForZeroLine, |
| 36 | + yAxisPoints |
| 37 | + } = useWaterfallChart(transactions, wrapperHeight); |
34 | 38 |
|
35 | 39 | return ( |
36 | | - <div |
37 | | - ref={wrapperRef} |
38 | | - className={styles.chartWrapper} |
39 | | - > |
| 40 | + <div ref={wrapperRef} className={styles.chartWrapper}> |
40 | 41 | <svg className={styles.svgContainer}> |
41 | 42 | {/* y-axis */} |
42 | | - <line x1="0" y1="0" x2="0" y2="100%" style={{ strokeDasharray: 1, stroke: 'rgb(158, 158, 158)', strokeWidth: 2}} /> |
| 43 | + <line |
| 44 | + x1='0' |
| 45 | + y1='0' |
| 46 | + x2='0' |
| 47 | + y2='100%' |
| 48 | + className={styles.axisLines} |
| 49 | + /> |
43 | 50 | {/* 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 | | - )} |
| 51 | + <line |
| 52 | + x1='0' |
| 53 | + y1='100%' |
| 54 | + x2='100%' |
| 55 | + y2='100%' |
| 56 | + className={styles.axisLines} |
| 57 | + /> |
| 58 | + {/* 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 | + /> |
55 | 66 | {chartElements?.map((chartElement, index) => ( |
56 | 67 | <> |
57 | 68 | <rect |
58 | 69 | key={chartElement?.name} |
59 | 70 | width={DEFAULT_BAR_WIDTH} |
60 | 71 | height={Math.abs(chartElement?.value / yAxisUnitsPerPixel)} |
61 | 72 | 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 | | - }} /> |
| 73 | + x={(2 * index + 1) * DEFAULT_BAR_WIDTH} |
| 74 | + className={`${styles.graphBar} ${chartElement?.value >= 0 ? styles.positiveGraph : styles.negativeGraph}`} |
| 75 | + /> |
70 | 76 | <line |
71 | 77 | 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 | | - }} /> |
| 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)} |
| 82 | + className={styles.bridgeLine} |
| 83 | + /> |
81 | 84 | </> |
82 | 85 | ))} |
83 | 86 | </svg> |
| 87 | + <div className={styles.yPoints}> |
| 88 | + {yAxisPoints?.map((yAxisPoint, index) => ( |
| 89 | + <div |
| 90 | + key={yAxisPoint} |
| 91 | + className={styles.yPoint} |
| 92 | + style={{bottom: (index *30) - 8 }} |
| 93 | + > |
| 94 | + {yAxisPoint} |
| 95 | + </div> |
| 96 | + ))} |
| 97 | + </div> |
84 | 98 | </div> |
85 | 99 | ); |
86 | 100 | }; |
|
0 commit comments