|
| 1 | +import React, { FC, useEffect, useRef, useState } from 'react'; |
| 2 | +import { IWaterfallGraphProps } from '../types/types'; |
| 3 | +import useWaterfallChart from './useWaterFallChart'; |
| 4 | +import styles from './styles.module.scss'; |
| 5 | + |
| 6 | +import { |
| 7 | + DEFAULT_BAR_WIDTH, |
| 8 | + DEFAULT_PIXELS_PER_Y_UNIT, |
| 9 | + DEFAULT_SUMMARY_LABEL, |
| 10 | + FINAL_SUMMARY_GRAPH_KEY, |
| 11 | + FINAL_SUMMARY_X_LABEL_KEY |
| 12 | +} from '../constants'; |
| 13 | + |
| 14 | +const WaterFallChart: FC<IWaterfallGraphProps> = (props) => { |
| 15 | + const { |
| 16 | + transactions, |
| 17 | + barWidth, |
| 18 | + showBridgeLines = true, |
| 19 | + showYAxisScaleLines = false, |
| 20 | + yAxisPixelsPerUnit = DEFAULT_PIXELS_PER_Y_UNIT, |
| 21 | + showFinalSummary = true, |
| 22 | + summaryXLabel = DEFAULT_SUMMARY_LABEL, |
| 23 | + summaryBarStyles = {}, |
| 24 | + positiveBarStyles = {}, |
| 25 | + negativeBarStyles = {}, |
| 26 | + onChartClick |
| 27 | + } = props; |
| 28 | + |
| 29 | + const wrapperRef = useRef<HTMLDivElement | null>(null); |
| 30 | + const [wrapperHeight, setWrapperHeight] = useState(0); |
| 31 | + const [barWidthVal, setBarWidthVal] = useState(barWidth ?? DEFAULT_BAR_WIDTH); |
| 32 | + |
| 33 | + const { chartElements, yValueForZeroLine, yAxisPoints, yAxisScale, calculateBarWidth } = useWaterfallChart( |
| 34 | + transactions, |
| 35 | + wrapperHeight, |
| 36 | + yAxisPixelsPerUnit, |
| 37 | + showFinalSummary |
| 38 | + ); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + const onWrapperDimensionsChange = (): void => { |
| 42 | + if (wrapperRef.current) { |
| 43 | + setWrapperHeight(wrapperRef?.current?.offsetHeight); |
| 44 | + if (!barWidth || barWidth <= 0) setBarWidthVal(calculateBarWidth(wrapperRef?.current?.offsetWidth)); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + onWrapperDimensionsChange(); |
| 49 | + |
| 50 | + window.addEventListener('resize', onWrapperDimensionsChange); |
| 51 | + |
| 52 | + return () => window.removeEventListener('resize', onWrapperDimensionsChange); |
| 53 | + }, [barWidth, calculateBarWidth]); |
| 54 | + |
| 55 | + const summaryValue = Math.abs(chartElements[chartElements?.length - 1]?.cumulativeSum); |
| 56 | + const summaryBarHeight = Math.abs((summaryValue / yAxisScale) * yAxisPixelsPerUnit); |
| 57 | + const summaryChartElement = { |
| 58 | + name: summaryXLabel, |
| 59 | + value: summaryValue, |
| 60 | + yVal: yValueForZeroLine - (summaryValue / yAxisScale) * yAxisPixelsPerUnit, |
| 61 | + cumulativeSum: 0, |
| 62 | + barHeight: summaryBarHeight |
| 63 | + }; |
| 64 | + |
| 65 | + return ( |
| 66 | + <div ref={wrapperRef} className={styles.chartWrapper}> |
| 67 | + <svg className={styles.svgContainer}> |
| 68 | + {/* y-axis */} |
| 69 | + <line x1='0' y1='0' x2='0' y2='100%' className={styles.axisLines} /> |
| 70 | + {/* x-axis */} |
| 71 | + <line x1='0' y1='100%' x2='100%' y2='100%' className={styles.axisLines} /> |
| 72 | + {/*y axis scale lines */} |
| 73 | + {showYAxisScaleLines && yAxisPoints?.map((yPoint, index) => ( |
| 74 | + <line |
| 75 | + key={yPoint} |
| 76 | + x1='0' |
| 77 | + y1={wrapperHeight - index * yAxisPixelsPerUnit} |
| 78 | + x2='100%' |
| 79 | + y2={wrapperHeight - index * yAxisPixelsPerUnit} |
| 80 | + className={`${styles.axisLines}`} |
| 81 | + /> |
| 82 | + ))} |
| 83 | + {chartElements?.map((chartElement, index) => ( |
| 84 | + <> |
| 85 | + <rect |
| 86 | + key={`${chartElement?.name}-bar-graph`} |
| 87 | + width={barWidthVal} |
| 88 | + height={chartElement?.barHeight} |
| 89 | + y={chartElement?.yVal} |
| 90 | + x={(2 * index + 1) * barWidthVal} |
| 91 | + className={`${styles.graphBar} ${chartElement?.value >= 0 ? styles.positiveGraph : styles.negativeGraph}`} |
| 92 | + style={chartElement?.value >= 0 ? positiveBarStyles : negativeBarStyles} |
| 93 | + onClick={(): void => onChartClick && onChartClick(chartElement)} |
| 94 | + /> |
| 95 | + {showBridgeLines && (showFinalSummary || index !== chartElements?.length - 1) && ( |
| 96 | + <line |
| 97 | + key={`${chartElement?.name}-bridge-line`} |
| 98 | + className={styles.bridgeLine} |
| 99 | + x1={(2 * index + 2) * barWidthVal} |
| 100 | + y1={yValueForZeroLine - (chartElement?.cumulativeSum / yAxisScale) * yAxisPixelsPerUnit} |
| 101 | + x2={(2 * index + 3) * barWidthVal} |
| 102 | + y2={yValueForZeroLine - (chartElement?.cumulativeSum / yAxisScale) * yAxisPixelsPerUnit} |
| 103 | + /> |
| 104 | + )} |
| 105 | + </> |
| 106 | + ))} |
| 107 | + {showFinalSummary && ( |
| 108 | + <rect |
| 109 | + key={FINAL_SUMMARY_GRAPH_KEY} |
| 110 | + width={barWidthVal} |
| 111 | + height={summaryChartElement?.barHeight} |
| 112 | + y={summaryChartElement?.yVal} |
| 113 | + x={(2 * chartElements?.length + 1) * barWidthVal} |
| 114 | + className={`${styles.graphBar} ${styles.summaryGraphBar}`} |
| 115 | + onClick={(): void => onChartClick && onChartClick(summaryChartElement)} |
| 116 | + /> |
| 117 | + )} |
| 118 | + </svg> |
| 119 | + <div className={styles.yPoints}> |
| 120 | + {yAxisPoints?.map((yAxisPoint, index) => ( |
| 121 | + <div |
| 122 | + key={yAxisPoint} |
| 123 | + className={styles.yPoint} |
| 124 | + style={{ bottom: index * yAxisPixelsPerUnit - 8 }} |
| 125 | + > |
| 126 | + {yAxisPoint} |
| 127 | + </div> |
| 128 | + ))} |
| 129 | + </div> |
| 130 | + <div className={styles.xPoints}> |
| 131 | + {transactions?.map((transaction, index) => ( |
| 132 | + <div |
| 133 | + key={transaction?.label} |
| 134 | + className={styles.xPoint} |
| 135 | + style={{ left: (2 * index + 1.25) * barWidthVal }} |
| 136 | + // the 1.25 is to reduce chances for the label to overflow to right |
| 137 | + > |
| 138 | + {transaction?.label} |
| 139 | + </div> |
| 140 | + ))} |
| 141 | + {showFinalSummary && ( |
| 142 | + <div |
| 143 | + key={FINAL_SUMMARY_X_LABEL_KEY} |
| 144 | + className={styles.xPoint} |
| 145 | + style={{ ...summaryBarStyles, left: (2 * chartElements?.length + 1.25) * barWidthVal }} |
| 146 | + > |
| 147 | + {summaryXLabel} |
| 148 | + </div> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + ); |
| 153 | +}; |
| 154 | + |
| 155 | +export default WaterFallChart; |
0 commit comments