Massive/Chart.tsx

69 lines
1.7 KiB
TypeScript
Raw Normal View History

import {useTheme} from '@react-navigation/native'
2022-10-31 04:22:08 +00:00
import * as shape from 'd3-shape'
import {View} from 'react-native'
import {Grid, LineChart, XAxis, YAxis} from 'react-native-svg-charts'
import {CombinedDarkTheme, CombinedDefaultTheme} from './App'
import {MARGIN, PADDING} from './constants'
import GymSet from './gym-set'
import useDark from './use-dark'
2022-09-02 00:24:28 +00:00
export default function Chart({
yData,
xFormat,
xData,
yFormat,
}: {
2022-10-31 04:22:08 +00:00
yData: number[]
xData: GymSet[]
xFormat: (value: any, index: number) => string
yFormat: (value: any) => string
2022-09-02 00:24:28 +00:00
}) {
const {colors} = useTheme()
2022-10-31 04:22:08 +00:00
const dark = useDark()
const axesSvg = {
fontSize: 10,
fill: dark
? CombinedDarkTheme.colors.text
: CombinedDefaultTheme.colors.text,
2022-10-31 04:22:08 +00:00
}
const verticalContentInset = {top: 10, bottom: 10}
const xAxisHeight = 30
2022-09-02 00:24:28 +00:00
return (
<>
<View
style={{
height: 300,
padding: PADDING,
flexDirection: 'row',
}}>
2022-09-02 00:24:28 +00:00
<YAxis
data={yData}
style={{marginBottom: xAxisHeight}}
contentInset={verticalContentInset}
svg={axesSvg}
formatLabel={yFormat}
/>
<View style={{flex: 1, marginLeft: MARGIN}}>
2022-09-02 00:24:28 +00:00
<LineChart
style={{flex: 1}}
data={yData}
contentInset={verticalContentInset}
curve={shape.curveBasis}
svg={{
stroke: colors.primary,
2022-09-02 00:24:28 +00:00
}}>
<Grid />
</LineChart>
<XAxis
data={xData}
formatLabel={xFormat}
contentInset={{left: 15, right: 16}}
2022-09-02 00:24:28 +00:00
svg={axesSvg}
/>
</View>
</View>
</>
2022-10-31 04:22:08 +00:00
)
2022-09-02 00:24:28 +00:00
}