Skip to content

Commit

Permalink
feat: improve api, add axis, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
e-younan committed Mar 4, 2024
1 parent 468297f commit a8fd0a9
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 215 deletions.
13 changes: 12 additions & 1 deletion example/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/* eslint-disable import/no-duplicates */
/* eslint-disable prettier/prettier */
import "react-native-gesture-handler";
import { Slot } from "expo-router";
import { StyleSheet } from "react-native";
import { GestureHandlerRootView } from "react-native-gesture-handler";

export default Slot;
export default () => (
<GestureHandlerRootView style={styles.container}>
<Slot />
</GestureHandlerRootView>
);

const styles = StyleSheet.create({ container: { flex: 1 } });
23 changes: 12 additions & 11 deletions example/app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { LineChart } from "@codeherence/react-native-graph";
import { useMemo } from "react";
import { StyleSheet, View } from "react-native";
import { useCallback, useState } from "react";
import { Button, StyleSheet, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";

import { Banner } from "../components/Banner";

const generateRandomData = (): [number, number][] => {
return Array.from({ length: 100 }, (_, i) => [i, Math.random() * 2000]);
};

export default () => {
const { top, bottom } = useSafeAreaInsets();
const [data, setData] = useState<[number, number][]>(generateRandomData());

const data: [number, number][] = useMemo(() => {
return Array.from({ length: 20 }, (_, i) => [i, Math.random() * 2000]);
const handlePress = useCallback(() => {
setData(generateRandomData());
}, []);

return (
<View style={[styles.container, { paddingTop: top, paddingBottom: bottom }]}>
<LineChart
points={data}
style={styles.chart}
BannerComponent={Banner}
TopAxisLabel={() => <View />}
/>
<Button title="Randomize data" onPress={handlePress} />
<LineChart points={data} style={styles.chart} BannerComponent={Banner} />
</View>
);
};

const styles = StyleSheet.create({
container: { flex: 1 },
chart: { flex: 1, maxHeight: 200 },
chart: { flex: 1 },
});
12 changes: 9 additions & 3 deletions example/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ config.resolver.extraNodeModules = new Proxy(
// npm v7+ will install ../node_modules/react-native because of peerDependencies.
// To prevent the incompatible react-native bewtween ./node_modules/react-native and ../node_modules/react-native,
// excludes the one from the parent folder when bundling.

const IGNORED_LIBS = [
"react-native",
"@shopify/react-native-skia",
"react-native-reanimated",
"react-native-gesture-handler",
];

config.resolver.blockList = [
...Array.from(config.resolver.blockList ?? []),
new RegExp(path.resolve("..", "node_modules", "react-native")),
new RegExp(path.resolve("..", "node_modules", "@shopify/react-native-skia")),
new RegExp(path.resolve("..", "node_modules", "react-native-reanimated")),
...IGNORED_LIBS.map((lib) => new RegExp(path.resolve("..", "node_modules", lib))),
];

config.resolver.nodeModulesPaths = [
Expand Down
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"react-native": "0.73.4",
"react-native-gesture-handler": "^2.15.0",
"react-native-reanimated": "^3.7.2",
"react-native-safe-area-context": "4.8.2",
"react-native-screens": "~3.29.0",
Expand Down
32 changes: 31 additions & 1 deletion example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,13 @@
"@babel/helper-validator-identifier" "^7.22.20"
to-fast-properties "^2.0.0"

"@egjs/hammerjs@^2.0.17":
version "2.0.17"
resolved "https://registry.yarnpkg.com/@egjs/hammerjs/-/hammerjs-2.0.17.tgz#5dc02af75a6a06e4c2db0202cae38c9263895124"
integrity sha512-XQsZgjm2EcVUiZQf11UBJQfmZeEmOW8DpI1gsFeln6w0ae0ii4dMQEQ0kjl6DspdWX1aGY1/loyXnP0JS06e/A==
dependencies:
"@types/hammerjs" "^2.0.36"

"@eslint-community/eslint-utils@^4.2.0", "@eslint-community/eslint-utils@^4.4.0":
version "4.4.0"
resolved "https://registry.yarnpkg.com/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz#a23514e8fb9af1269d5f7788aa556798d61c6b59"
Expand Down Expand Up @@ -2523,6 +2530,11 @@
"@types/minimatch" "*"
"@types/node" "*"

"@types/hammerjs@^2.0.36":
version "2.0.45"
resolved "https://registry.yarnpkg.com/@types/hammerjs/-/hammerjs-2.0.45.tgz#ffa764bb68a66c08db6efb9c816eb7be850577b1"
integrity sha512-qkcUlZmX6c4J8q45taBKTL3p+LbITgyx7qhlPYOdOHZB7B31K0mXbP5YA7i7SgDeEGuI9MnumiKPEMrxg8j3KQ==

"@types/html-minifier-terser@^6.0.0":
version "6.1.0"
resolved "https://registry.yarnpkg.com/@types/html-minifier-terser/-/html-minifier-terser-6.1.0.tgz#4fc33a00c1d0c16987b1a20cf92d20614c55ac35"
Expand Down Expand Up @@ -5941,6 +5953,13 @@ hermes-profile-transformer@^0.0.6:
dependencies:
source-map "^0.7.3"

hoist-non-react-statics@^3.3.0:
version "3.3.2"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
dependencies:
react-is "^16.7.0"

hosted-git-info@^3.0.2:
version "3.0.8"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-3.0.8.tgz#6e35d4cc87af2c5f816e4cb9ce350ba87a3f370d"
Expand Down Expand Up @@ -8574,7 +8593,7 @@ react-helmet-async@^1.3.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.2.0.tgz#199431eeaaa2e09f86427efbb4f1473edb47609b"
integrity sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==

react-is@^16.13.0, react-is@^16.13.1:
react-is@^16.13.0, react-is@^16.13.1, react-is@^16.7.0:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
Expand All @@ -8584,6 +8603,17 @@ react-is@^17.0.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==

react-native-gesture-handler@^2.15.0:
version "2.15.0"
resolved "https://registry.yarnpkg.com/react-native-gesture-handler/-/react-native-gesture-handler-2.15.0.tgz#f8e6c0451a7bdf065edb7b9be605480db402baa0"
integrity sha512-cmMGW8k86o/xgVTBZZOPohvR5re4Vh65PUxH4HbBBJAYTog4aN4wTVTUlnoky01HuSN8/X4h3tI/K3XLPoDnsg==
dependencies:
"@egjs/hammerjs" "^2.0.17"
hoist-non-react-statics "^3.3.0"
invariant "^2.2.4"
lodash "^4.17.21"
prop-types "^15.7.2"

react-native-reanimated@^3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/react-native-reanimated/-/react-native-reanimated-3.7.2.tgz#688a0002d129a8ddcef919093c45c67de18923f5"
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"jest": "^28.1.1",
"prettier": "^3.2.5",
"react-native-builder-bob": "^0.20.4",
"react-native-gesture-handler": "^2.15.0",
"react-native-reanimated": "3.7.2",
"react-native-safe-area-context": "^4.5.0",
"release-it": "^15.0.0",
Expand All @@ -87,6 +88,7 @@
"@shopify/react-native-skia": "0.1.241",
"react": ">=18.0.0",
"react-native": ">=0.71.0",
"react-native-gesture-handler": ">=2.0.0",
"react-native-reanimated": ">=2.0.0"
},
"engines": {
Expand Down
60 changes: 53 additions & 7 deletions src/components/LineChart/AxisLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { useCallback, useState } from "react";
import { LayoutChangeEvent } from "react-native";
import Reanimated from "react-native-reanimated";
import { LayoutChangeEvent, StyleSheet, View } from "react-native";
import Reanimated, {
clamp,
useAnimatedStyle,
useDerivedValue,
withTiming,
} from "react-native-reanimated";

export interface AxisLabelComponentProps {
value: number;
}

interface AxisLabelContainerProps {
children: React.ReactNode;
x: number;
containerWidth: number;
}

export const AxisLabelContainer: React.FC<AxisLabelContainerProps> = ({ children }) => {
const [_, setWidth] = useState(0);
const [__, setHeight] = useState(0);
export const AxisLabelContainer: React.FC<AxisLabelContainerProps> = ({
x,
containerWidth,
children,
}) => {
const [width, setWidth] = useState(0);

const onLayout = useCallback((event: LayoutChangeEvent) => {
setWidth(event.nativeEvent.layout.width);
setHeight(event.nativeEvent.layout.height);
}, []);

return <Reanimated.View onLayout={onLayout}>{children}</Reanimated.View>;
const translateX = useDerivedValue(() => {
const halfWidth = Math.round(width / 2);
const minX = 0;
const maxX = containerWidth - width;

return withTiming(clamp(x - halfWidth, minX, maxX));
}, [x, containerWidth, width]);

const transform = useAnimatedStyle(() => {
return {
transform: [{ translateX: translateX.value }],
};
});

return (
<View style={styles.container}>
<Reanimated.View style={[styles.child, transform]} onLayout={onLayout}>
{children}
</Reanimated.View>
</View>
);
};

const styles = StyleSheet.create({
container: {
position: "relative",
flexDirection: "row",
width: "100%",
overflow: "hidden",
},
child: {
justifyContent: "center",
alignItems: "center",
},
});
Loading

0 comments on commit a8fd0a9

Please sign in to comment.