1
- import { useEffect , useState } from 'react' ;
1
+ import { useEffect , useState , useRef } from 'react' ;
2
+ import { getAxisRanges } from './utils' ;
3
+ import { GraphPoint , GraphDetailsHookProps } from '../types/types' ;
2
4
3
- const useGraphWidthResize = ( parentNode : React . MutableRefObject < HTMLDivElement | null > ) : number => {
5
+ const useGraphDetails = (
6
+ data : Array < GraphPoint > ,
7
+ graphHeight : number
8
+ ) : GraphDetailsHookProps => {
9
+ // states
10
+ const [ pos , setPos ] = useState ( { x : 0 , y : 0 , yPlot : 0 , xPlot : 0 } ) ;
11
+ const [ showVerticalLine , setShowVerticalLine ] = useState ( false ) ;
4
12
const [ graphWidth , setGraphWidth ] = useState ( 0 ) ;
13
+
14
+ // refs
15
+ const parentNode = useRef < HTMLDivElement | null > ( null ) ;
16
+ const yPointsRef = useRef < HTMLDivElement | null > ( null ) ;
17
+
18
+ const axisValues = getAxisRanges ( data ) ;
19
+
5
20
const gutterSpacing = 30 ;
6
21
7
22
const getGraphParentWidth = ( elem : React . MutableRefObject < HTMLDivElement | null > ) : number =>
@@ -15,7 +30,49 @@ const useGraphWidthResize = (parentNode: React.MutableRefObject<HTMLDivElement |
15
30
window . onresize = ( ) : void => setGraphWidth ( getGraphParentWidth ( parentNode ) ) ;
16
31
} , [ parentNode ] ) ;
17
32
18
- return graphWidth ;
19
- } ;
33
+ // consts
34
+ const textHeight = 16 ;
35
+
36
+ const graphHeightDiff = axisValues . yMax - axisValues . yMin ;
37
+ const graphWidthDiff = axisValues . xMax - axisValues . xMin ;
38
+
39
+ const yPoints = Array . from (
40
+ { length : graphHeightDiff / axisValues . yInterval + 1 } ,
41
+ ( _ , index ) => Math . round ( index * axisValues . yInterval * 1000 ) / 1000 + axisValues . yMin ) ;
42
+ const xPoints = Array . from (
43
+ { length : graphWidthDiff / axisValues . xInterval + 1 } ,
44
+ ( _ , index ) => Math . round ( index * axisValues . xInterval * 1000 ) / 1000 + axisValues . xMin ) ;
45
+
46
+ const yRangeDiff = ( yPoints [ yPoints . length - 1 ] - yPoints [ 0 ] ) ;
47
+ const xRangeDiff = ( xPoints [ xPoints . length - 1 ] - xPoints [ 0 ] ) ;
48
+
49
+ const yRatio = graphHeight / yRangeDiff ;
50
+ const xRatio = graphWidth / xRangeDiff ;
51
+
52
+ const getGraphCoordinate = ( point : number , ratio : number ) : number => point * ratio ;
53
+
54
+ const formattedGraphPoints = data . map ( ( point : GraphPoint ) => ( {
55
+ ...point ,
56
+ yPlot : graphHeight - getGraphCoordinate ( point . y , yRatio ) + getGraphCoordinate ( axisValues . yMin , yRatio ) ,
57
+ xPlot : getGraphCoordinate ( point . x , xRatio ) - getGraphCoordinate ( axisValues . xMin , xRatio )
58
+ } ) ) ;
59
+
60
+ return {
61
+ pos,
62
+ setPos,
63
+ showVerticalLine,
64
+ setShowVerticalLine,
65
+ yPointsRef,
66
+ textHeight,
67
+ formattedGraphPoints,
68
+ getGraphCoordinate,
69
+ graphWidth,
70
+ axisValues,
71
+ yPoints,
72
+ xPoints,
73
+ yRatio,
74
+ parentNode
75
+ }
76
+ }
20
77
21
- export { useGraphWidthResize } ;
78
+ export { useGraphDetails } ;
0 commit comments