diff --git a/example/ios/GraphExample.xcodeproj/project.pbxproj b/example/ios/GraphExample.xcodeproj/project.pbxproj index 962d37f..3004a62 100644 --- a/example/ios/GraphExample.xcodeproj/project.pbxproj +++ b/example/ios/GraphExample.xcodeproj/project.pbxproj @@ -265,7 +265,7 @@ TestTargetID = 13B07F861A680F5B00A75B9A; }; 13B07F861A680F5B00A75B9A = { - DevelopmentTeam = CJW62Q77E7; + DevelopmentTeam = TFP6882UUN; LastSwiftMigration = 1120; }; 2D02E47A1E0B4A5D006451C7 = { @@ -542,7 +542,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = CJW62Q77E7; + DEVELOPMENT_TEAM = TFP6882UUN; ENABLE_BITCODE = NO; INFOPLIST_FILE = GraphExample/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; diff --git a/example/src/screens/GraphPage.tsx b/example/src/screens/GraphPage.tsx index c2fc13b..6b96767 100644 --- a/example/src/screens/GraphPage.tsx +++ b/example/src/screens/GraphPage.tsx @@ -16,6 +16,7 @@ const POINT_COUNT = 70 const POINTS = generateRandomGraphData(POINT_COUNT) const COLOR = '#6a7ee7' const GRADIENT_FILL_COLORS = ['#7476df5D', '#7476df4D', '#7476df00'] +const GRADIENT_LINE_COLORS = ['#FF0000', '#00FF00', '#0000FF'] const SMALL_POINTS = generateSinusGraphData(9) export function GraphPage() { @@ -26,7 +27,8 @@ export function GraphPage() { const [enableFadeInEffect, setEnableFadeInEffect] = useState(false) const [enableCustomSelectionDot, setEnableCustomSelectionDot] = useState(false) - const [enableGradient, setEnableGradient] = useState(false) + const [enableFillGradient, setEnableFillGradient] = useState(false) + const [enableLineGradient, setEnableLineGradient] = useState(false) const [enableRange, setEnableRange] = useState(false) const [enableIndicator, setEnableIndicator] = useState(false) const [indicatorPulsating, setIndicatorPulsating] = useState(false) @@ -89,9 +91,11 @@ export function GraphPage() { hapticFeedback('impactLight')} @@ -126,9 +130,14 @@ export function GraphPage() { setIsEnabled={setEnableCustomSelectionDot} /> + [ - 0, - Math.min(0.15, pathEnd.current), - pathEnd.current, - pathEnd.current, - 1, - ], - [pathEnd] - ) + const positions = useComputedValue(() => { + if (typeof color === 'string') { + return [ + 0, + Math.min(0.15, pathEnd.current), + pathEnd.current, + pathEnd.current, + 1, + ] + } else { + return color.map((_, index) => index / (color.length - 1)) + } + }, [pathEnd, color]) + const onLayout = useCallback( ({ nativeEvent: { layout } }: LayoutChangeEvent) => { setWidth(Math.round(layout.width)) @@ -164,7 +168,15 @@ export function AnimatedLineGraph({ [commandsChanged, indicatorX] ) - const indicatorPulseColor = useMemo(() => hexToRgba(color, 0.4), [color]) + const primaryColor = useMemo(() => { + if (typeof color === 'string') return color + return color[0] ?? '#FFF' + }, [color]) + + const indicatorPulseColor = useMemo( + () => hexToRgba(primaryColor, 0.4), + [primaryColor] + ) const shouldFillGradient = gradientFillColors != null @@ -269,22 +281,26 @@ export function AnimatedLineGraph({ ]) const gradientColors = useMemo(() => { - if (enableFadeInMask) { - return [ - `${getSixDigitHex(color)}00`, - `${getSixDigitHex(color)}ff`, - `${getSixDigitHex(color)}ff`, - `${getSixDigitHex(color)}33`, - `${getSixDigitHex(color)}33`, - ] + if (typeof color === 'string') { + if (enableFadeInMask) { + return [ + `${getSixDigitHex(color)}00`, + `${getSixDigitHex(color)}ff`, + `${getSixDigitHex(color)}ff`, + `${getSixDigitHex(color)}33`, + `${getSixDigitHex(color)}33`, + ] + } else { + return [ + color, + color, + color, + `${getSixDigitHex(color)}33`, + `${getSixDigitHex(color)}33`, + ] + } } else { - return [ - color, - color, - color, - `${getSixDigitHex(color)}33`, - `${getSixDigitHex(color)}33`, - ] + return color } }, [color, enableFadeInMask]) @@ -490,7 +506,7 @@ export function AnimatedLineGraph({ {SelectionDot != null && ( )} diff --git a/src/LineGraphProps.ts b/src/LineGraphProps.ts index 172eef0..445c8e8 100644 --- a/src/LineGraphProps.ts +++ b/src/LineGraphProps.ts @@ -30,9 +30,9 @@ interface BaseLineGraphProps extends ViewProps { */ range?: GraphRange /** - * Color of the graph line (path) + * Color of the graph line (path) either as a single solid color or an array of colors for a line gradient */ - color: string + color: string | string[] /** * (Optional) Colors for the fill gradient below the graph line */ diff --git a/src/SelectionDot.tsx b/src/SelectionDot.tsx index e72098b..499749e 100644 --- a/src/SelectionDot.tsx +++ b/src/SelectionDot.tsx @@ -1,4 +1,4 @@ -import React, { useCallback } from 'react' +import React, { useCallback, useMemo } from 'react' import { runOnJS, useAnimatedReaction } from 'react-native-reanimated' import { runSpring, @@ -45,6 +45,11 @@ export function SelectionDot({ [isActive, setIsActive] ) + const primaryColor = useMemo(() => { + if (typeof color === 'string') return color + return color[0] ?? '#FFF' + }, [color]) + return ( - + diff --git a/src/StaticLineGraph.tsx b/src/StaticLineGraph.tsx index b7149bf..00ffc5b 100644 --- a/src/StaticLineGraph.tsx +++ b/src/StaticLineGraph.tsx @@ -47,12 +47,23 @@ export function StaticLineGraph({ [height, lineThickness, pathRange, points, width] ) + const primaryColor = useMemo(() => { + if (typeof color === 'string') return color + return color[0] ?? '#FFF' + }, [color]) + const gradientColors = useMemo( - () => [`${getSixDigitHex(color)}00`, `${getSixDigitHex(color)}ff`], + () => + typeof color === 'string' + ? [`${getSixDigitHex(color)}00`, `${getSixDigitHex(color)}ff`] + : color, [color] ) const gradientFrom = useMemo(() => vec(0, 0), []) - const gradientTo = useMemo(() => vec(width * 0.15, 0), [width]) + const gradientTo = useMemo( + () => vec(typeof color === 'string' ? width * 0.15 : width, 0), + [width, color] + ) return ( @@ -60,12 +71,12 @@ export function StaticLineGraph({ - {enableFadeInMask && ( + {(enableFadeInMask || typeof color !== 'string') && (