-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
606 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
example/storybook-nativewind/src/components/LineChart/LineChart.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { ComponentMeta } from '@storybook/react-native'; | ||
import LineChart from './LineChart'; | ||
|
||
const AccordionMeta: ComponentMeta<typeof LineChart> = { | ||
title: 'stories/Line Chart', | ||
component: LineChart, | ||
// metaInfo is required for figma generation | ||
// @ts-ignore | ||
metaInfo: { | ||
componentDescription: `The Line Chart component displays data compatable with a two-dimensional cartesian plane`, | ||
}, | ||
argTypes: {}, | ||
}; | ||
|
||
export default AccordionMeta; | ||
|
||
export { LineChart }; |
63 changes: 63 additions & 0 deletions
63
example/storybook-nativewind/src/components/LineChart/LineChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { LineChart } from '@/components/ui/line-chart'; | ||
|
||
import { Button, View, StyleSheet, useColorScheme } from 'react-native'; | ||
|
||
const DATA = Array.from({ length: 31 }, (_, i) => ({ | ||
x: i, | ||
y: 40 + 30 * Math.random(), | ||
})); | ||
const DATA2 = Array.from({ length: 31 }, (_, i) => ({ | ||
x: i, | ||
y: 40 + 30 * Math.random(), | ||
})); | ||
|
||
const LineChartBasic = () => { | ||
const [data, setData] = useState(DATA); | ||
const colorMode = useColorScheme(); | ||
|
||
const labelColor = colorMode === 'dark' ? 'white' : 'black'; | ||
const lineColor = colorMode === 'dark' ? 'lightgrey' : 'black'; | ||
|
||
return ( | ||
<View style={style.container}> | ||
<LineChart | ||
width={300} | ||
height={300} | ||
data={data} | ||
outlineColor="lightgreen" | ||
gradientColors={['green', '#90ee9050']} | ||
labelColor={labelColor} | ||
lineColor={lineColor} | ||
topLabelPrefix="$" | ||
/> | ||
<Button | ||
title="Change to Data 1" | ||
onPress={() => { | ||
setData(DATA); | ||
}} | ||
/> | ||
<Button | ||
title="Change to Data 2" | ||
onPress={() => { | ||
setData(DATA2); | ||
}} | ||
/> | ||
</View> | ||
); | ||
}; | ||
|
||
LineChartBasic.description = | ||
'This is a basic Line Chart example. The Line Chart is a component that lets you display data on a two dimensional cartesian plane'; | ||
|
||
export default LineChartBasic; | ||
|
||
const style = StyleSheet.create({ | ||
container: { | ||
height: '100%', | ||
width: '100%', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
}); |
122 changes: 122 additions & 0 deletions
122
example/storybook-nativewind/src/core-components/nativewind/line-chart/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React from 'react'; | ||
|
||
import { | ||
Circle, | ||
LinearGradient, | ||
matchFont, | ||
vec, | ||
} from '@shopify/react-native-skia'; | ||
import { Platform } from 'react-native'; | ||
import { useDerivedValue, type SharedValue } from 'react-native-reanimated'; | ||
import { Area, CartesianChart, Line, useChartPressState } from 'victory-native'; | ||
|
||
import { Text as SKText } from '@shopify/react-native-skia'; | ||
import { Box } from '@gluestack-ui/themed'; | ||
|
||
const fontFamily = Platform.select({ ios: 'Helvetica', default: 'sans-serif' }); | ||
|
||
const legendFontStyle = { | ||
fontFamily, | ||
fontSize: 14, | ||
fontWeight: 'bold', | ||
}; | ||
|
||
const indicatorFontStyle = { | ||
fontFamily, | ||
fontSize: 25, | ||
fontWeight: 'bold', | ||
}; | ||
|
||
// @ts-ignore | ||
const legendFont = matchFont(legendFontStyle); | ||
// @ts-ignore | ||
const indicatorFont = matchFont(indicatorFontStyle); | ||
|
||
interface ChartData { | ||
[x: string]: number; | ||
} | ||
|
||
interface Props { | ||
height: number; | ||
width: number; | ||
data: ChartData[]; | ||
outlineColor: string; | ||
gradientColors: string[]; | ||
labelColor: string; | ||
lineColor: string; | ||
topLabelPrefix?: string; | ||
topLabelSuffix?: string; | ||
} | ||
|
||
export const LineChart = ({ | ||
height, | ||
width, | ||
data, | ||
outlineColor, | ||
gradientColors, | ||
labelColor, | ||
lineColor, | ||
topLabelPrefix = '', | ||
topLabelSuffix = '', | ||
}: Props) => { | ||
const { state, isActive } = useChartPressState({ x: 0, y: { y: 0 } }); | ||
|
||
const value = useDerivedValue(() => { | ||
return topLabelPrefix + state.y.y.value.value.toFixed(2) + topLabelSuffix; | ||
}, [state]); | ||
|
||
return ( | ||
<Box height={height} width={width}> | ||
<CartesianChart | ||
data={data} | ||
xKey="x" | ||
yKeys={['y']} | ||
domainPadding={{ top: 30 }} | ||
axisOptions={{ | ||
font: legendFont, | ||
labelColor, | ||
lineColor, | ||
}} | ||
chartPressState={state} | ||
> | ||
{({ points, chartBounds }) => ( | ||
<> | ||
<SKText | ||
x={chartBounds.left + 10} | ||
y={chartBounds.top + indicatorFont.measureText('0').height + 5} | ||
font={indicatorFont} | ||
text={value} | ||
color={labelColor} | ||
style={'fill'} | ||
/> | ||
<Line | ||
points={points.y} | ||
color={outlineColor} | ||
strokeWidth={3} | ||
animate={{ type: 'timing', duration: 500 }} | ||
/> | ||
<Area | ||
points={points.y} | ||
y0={chartBounds.bottom} | ||
animate={{ type: 'timing', duration: 500 }} | ||
> | ||
<LinearGradient | ||
start={vec(chartBounds.bottom, 200)} | ||
end={vec(chartBounds.bottom, chartBounds.bottom)} | ||
colors={gradientColors} | ||
/> | ||
</Area> | ||
|
||
{isActive ? ( | ||
<ToolTip x={state.x.position} y={state.y.y.position} /> | ||
) : null} | ||
</> | ||
)} | ||
</CartesianChart> | ||
</Box> | ||
); | ||
}; | ||
|
||
function ToolTip({ x, y }: { x: SharedValue<number>; y: SharedValue<number> }) { | ||
return <Circle cx={x} cy={y} r={8} color={'grey'} opacity={0.8} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.