-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineChartStructure.tsx
More file actions
174 lines (161 loc) · 5.04 KB
/
lineChartStructure.tsx
File metadata and controls
174 lines (161 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { Children, type FC, type ReactElement, useEffect, useMemo, useState } from 'react';
import { SvgContainer } from '@/components/svgContainer/svgContainer';
import { useId } from '@/hooks/useId/useId';
import { useResponsiveCanvas } from '@/hooks/useResponsiveCanvas/useResponsiveCanvas';
import { DefaultCanvasConfig } from '@/types/canvas.type';
import type { ChartError, ErrorType } from '@/types/errors.type';
import { createErrorAccumulator } from '@/utils/createErrorAccumulator';
import { getChildrenAttr } from '@/utils/getChildrenAttr/getChildrenAttr';
import { getDataFingerprint } from '@/utils/getDataFingerprint/getDataFingerprint';
import { buildLineContextValue } from './context/buildLineContextValue';
import { LineChartContext } from './context/lineChartContext';
import { LineChartXAxis } from './fragments/lineChartXAxis';
import { LineChartYAxis } from './fragments/lineChartYAxis';
import { useHover } from './hook/useHover';
import type { LineChartProps } from './lineChart.type';
/**
* Renders a line chart component.
*
* @component
* @example
* ```
* <LineChart
* caption="Line Chart"
* canvasConfig={DefaultCanvasConfig}
* dataTestId="line-chart"
* width="100%"
* height="70%"
* data={chartData}
* >
* {chartChildren}
* </LineChart>
* ```
*
* @param props - `LineChartProps` include:
* - `width`: The width of the chart (default is '100%').
* - `height`: The height of the chart (default is '100%').
* - `data`: The data for the chart.
* - `xKey`: The key for the x-axis data.
* - `canvasConfig`: The configuration for the chart canvas (default is `DefaultCanvasConfig`).
* - `dataTestId`: The data test ID for the chart (default is 'line-chart').
* - `caption`: The caption for the chart (default is 'Line chart').
*
* @returns The rendered LineChart component.
*/
export const LineChartStructure: FC<LineChartProps> = ({
ariaHidden,
ariaLabel,
canvasConfig = DefaultCanvasConfig,
caption,
children,
classNames,
data,
dataTestId: dataTestIdProp = 'line-chart',
getPathArea,
height = '100%',
onErrors,
role,
tabIndex,
width = '100%',
xKey,
...props
}): ReactElement => {
const dataTestId = useId(dataTestIdProp);
const [childrenYKeys, setChildrenYKey] = useState<string>('');
// Use the responsive canvas hook for dimension management
const { parsedCanvas, parsedCanvasExtraSpace, viewBox } = useResponsiveCanvas({
canvasConfig,
dataTestId,
height,
width,
});
// Set the default axis for the chart
const defaultAxis = [
<LineChartXAxis key="default-x-axis" />,
<LineChartYAxis key="default-y-axis" />,
];
const arrayChildren = (Children.toArray(children) || []) as React.JSX.Element[];
const chidrenWithDefaultAxis = defaultAxis.concat(arrayChildren);
// watch the Y childs keys
getChildrenAttr({
attrName: 'dataKey',
children: chidrenWithDefaultAxis,
originalValue: childrenYKeys,
updateValue: setChildrenYKey,
});
const errorAccumulator = useMemo(() => createErrorAccumulator(onErrors), [onErrors]);
const dataFingerprint = getDataFingerprint(data);
const contextValue = useMemo(() => {
// Clear previous errors before building new context
errorAccumulator.clearErrors();
return buildLineContextValue({
addError: (errorType: keyof typeof ErrorType, error: Omit<ChartError, 'type'>) => {
errorAccumulator.addError(errorType, error);
},
canvasHeight: parsedCanvas.height,
canvasWidth: parsedCanvas.width,
children: chidrenWithDefaultAxis,
data,
viewBox,
xKey,
});
}, [
parsedCanvas.width,
parsedCanvas.height,
dataFingerprint,
xKey,
childrenYKeys,
errorAccumulator.addError,
]);
const { svgRef, xCursor, yCursor } = useHover({
canvasHeight: parsedCanvas.height,
canvasWidth: parsedCanvas.width,
});
useEffect(() => {
getPathArea?.({
x1: contextValue.xAxisCoordinates.coordinates.x1,
y1: contextValue.yAxisCoordinates.coordinates.y1,
x2: contextValue.xAxisCoordinates.coordinates.x2,
y2: contextValue.yAxisCoordinates.coordinates.y2,
});
}, [
contextValue.xAxisCoordinates.coordinates.x1,
contextValue.yAxisCoordinates.coordinates.y1,
contextValue.xAxisCoordinates.coordinates.x2,
contextValue.yAxisCoordinates.coordinates.y2,
getPathArea,
]);
return (
<SvgContainer
ref={svgRef}
ariaHidden={ariaHidden}
ariaLabel={ariaLabel}
caption={caption}
className={classNames}
data-kbt-svg={true}
dataTestId={dataTestId}
height={height}
role={role}
tabIndex={tabIndex}
viewBox={viewBox}
width={width}
{...props}
>
<LineChartContext.Provider
value={{
...contextValue,
canvasExtraSpace: parsedCanvasExtraSpace,
canvasHeight: parsedCanvas.height,
canvasWidth: parsedCanvas.width,
data,
dataTestId,
xCursor,
xKey,
yCursor,
}}
>
{children}
</LineChartContext.Provider>
</SvgContainer>
);
};