diff --git a/src/component/charts/container/chart-container.tsx b/src/component/charts/container/chart-container.tsx new file mode 100644 index 0000000..58f5c09 --- /dev/null +++ b/src/component/charts/container/chart-container.tsx @@ -0,0 +1,91 @@ +import React from "react"; +import { View } from "react-native"; +import { YAxis } from "react-native-svg-charts"; + +import { graphStyles } from "../chart-styles"; +import ChartView from "../chart-view"; +import { ChartContainerProps } from "../graph-types"; +import { useGraphData } from "../use-graph-data"; + +// TODO: separate PR will update useGraphData to take into account useCommonScale +// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars +const ChartContainer: React.FC = ({ + ChartComponent, + data, + useCommonScale = false, + testID, + ...props +}) => { + if (!data || typeof data !== "object") { + return null; + } // TODO:#38 + const { + xValues, + yData, + yAxisLeftLabels, + yAxisRightLabels, + defaultProps: { + height, + contentInset, + min, + numberOfTicks, + lineStrokeWidth, + yAxisProps: { + maxLeftYAxisValue, + maxRightYAxisValue, + formatLeftYAxisLabel, + formatRightYAxisLabel, + }, + }, + // Proper error/loading handling from useQueryHandler can work with this rule #38 + // eslint-disable-next-line react-hooks/rules-of-hooks + } = useGraphData(data, props); + + // TODO: This could be done by the hook since it already handles spreading props. + // Depending on if we want a context provider for Charts, that could be another way to handle it + const chartComponentProps = { + yData, + xValues, + lineStrokeWidth, + min, + contentInset, + numberOfTicks, + }; + return ( + + + + + + + + + + ); +}; + +export default ChartContainer; diff --git a/src/component/charts/graph-types.ts b/src/component/charts/graph-types.ts index e498466..bc36201 100644 --- a/src/component/charts/graph-types.ts +++ b/src/component/charts/graph-types.ts @@ -76,3 +76,12 @@ export interface CommonProps { yAxisProps?: YAxisProps; testID?: string; } +/** + * Common properties for graph render components. + * + * @interface ChartContainerProps + * @property {React.FC} ChartComponent - The graph component to render + */ +export interface ChartContainerProps extends CommonProps { + ChartComponent?: React.FC; +} diff --git a/src/component/charts/line-graph/line-graph.tsx b/src/component/charts/line-graph/line-graph.tsx index 3b7af11..066f88d 100644 --- a/src/component/charts/line-graph/line-graph.tsx +++ b/src/component/charts/line-graph/line-graph.tsx @@ -1,102 +1,38 @@ import * as shape from "d3-shape"; import React from "react"; -import { View } from "react-native"; -import { LineChart, XAxis, YAxis } from "react-native-svg-charts"; - +import { LineChart, XAxis } from "react-native-svg-charts"; import { graphStyles } from "../chart-styles"; -import ChartView from "../chart-view"; import { CustomGrid } from "../custom-grid"; import { CommonProps } from "../graph-types"; -import { useGraphData } from "../use-graph-data"; // TODO: separate PR will update useGraphData to take into account useCommonScale // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars -const LineGraph: React.FC = ({ - data, - useCommonScale = false, - testID, - ...props -}) => { - if (!data || typeof data !== "object") { - return null; - } // TODO:#38 - const { - xValues, - yData, - yAxisLeftLabels, - yAxisRightLabels, - defaultProps: { - height, - contentInset, - min, - numberOfTicks, - lineStrokeWidth, - yAxisProps: { - maxLeftYAxisValue, - maxRightYAxisValue, - formatLeftYAxisLabel, - formatRightYAxisLabel, - }, - }, - // Proper error/loading handling from useQueryHandler can work with this rule #38 - // eslint-disable-next-line react-hooks/rules-of-hooks - } = useGraphData(data, props); - +const LineGraph: React.FC = ({ ...chartComponentProps }) => { + const { yData, xValues, lineStrokeWidth, min, contentInset, numberOfTicks } = + chartComponentProps; return ( - - + (item as unknown as { value: number }).value} + xAccessor={({ index }) => xValues[index] as number} + gridMin={min} + contentInset={contentInset} + numberOfTicks={numberOfTicks} > - - - - (item as unknown as { value: number }).value - } - xAccessor={({ index }) => xValues[index] as number} - gridMin={min} - contentInset={contentInset} - numberOfTicks={numberOfTicks} - > - - - index)} // TODO: update when useGraphHook returns explicit display values - style={[graphStyles.xAxisMarginTop]} - svg={graphStyles.xAxisFontStyle} - numberOfTicks={numberOfTicks} - contentInset={graphStyles.horizontalInset} - /> - - - - + + + index)} // TODO: update when useGraphHook returns explicit display values + style={[graphStyles.xAxisMarginTop]} + svg={graphStyles.xAxisFontStyle} + numberOfTicks={numberOfTicks} + contentInset={graphStyles.horizontalInset} + /> + ); }; diff --git a/src/screens/session.tsx b/src/screens/session.tsx index e44b554..ef1d982 100644 --- a/src/screens/session.tsx +++ b/src/screens/session.tsx @@ -1,14 +1,15 @@ import React from "react"; import { StyleSheet, View } from "react-native"; -import { graph_data_two_measures } from "../../test/mock/charts/mock-data"; -import BarGraph from "../component/charts/bar-graph/bar-graph"; +import { line_chart_two_y_data } from "../../test/mock/charts/mock-data"; +import ChartContainer from "../component/charts/container/chart-container"; +import LineGraph from "../component/charts/line-graph/line-graph"; // Session Mock - can be used for session summary screen using a query handler component // BarGraph component using mocked data currently export default function SessionScreen() { return ( - + ); }