diff --git a/example/app/axis-images.tsx b/example/app/axis-images.tsx index c949fa2a..fecf9f13 100644 --- a/example/app/axis-images.tsx +++ b/example/app/axis-images.tsx @@ -8,6 +8,9 @@ import { Skia, Paragraph, TileMode, + Text as SkiaText, + type ImageProps, + type SkImage, } from "@shopify/react-native-skia"; import { CartesianChart, Line } from "victory-native"; import { Text } from "example/components/Text"; @@ -23,9 +26,49 @@ const DATA = [ { temperature: 15, day: 6 }, ]; +export type TickImage = Partial> & { + image?: string; + skImage?: SkImage | null; + width?: number; + height?: number; +}; + +type AxisImageProps = TickImage & { + y: number; + x: number; +}; + +export const AxisImage: React.FC = ({ + image, + skImage, + y, + x, + ...rest +}) => { + const imageSKFromHook = useImage(image || ""); + const imageSK = skImage || imageSKFromHook; + + return ( + + ); +}; + const ChartWithRemoteImages = () => { const font = useFont(inter, 12); const [data] = useState(DATA); + const images = [ + "https://picsum.photos/32/32", + "https://picsum.photos/32/32", + "https://picsum.photos/32/32", + ]; return ( @@ -44,11 +87,18 @@ const ChartWithRemoteImages = () => { yAxis={[ { tickValues: [15, 50, 80], - tickImages: [ - { image: "https://picsum.photos/32/32", width: 32, height: 32 }, - { image: "https://picsum.photos/32/32", width: 32, height: 32 }, - { image: "https://picsum.photos/32/32", width: 32, height: 32 }, - ], + renderYLabel: ({ x, y, index }) => { + return ( + + ); + }, labelOffset: 12, font, linePathEffect: , @@ -77,6 +127,7 @@ const ChartWithLocalImages = () => { const warmImage = useImage(require("../assets/warm.png")); const medImage = useImage(require("../assets/med.png")); const coldImage = useImage(require("../assets/cold.png")); + const images = [coldImage, medImage, warmImage]; return ( @@ -91,15 +142,38 @@ const ChartWithLocalImages = () => { font, labelOffset: 14, linePathEffect: , + renderXLabel: ({ x, y, content, canFitContent }) => { + if (!canFitContent) { + return null; + } + + return ( + + ); + }, }} yAxis={[ { tickValues: [15, 50, 80], - tickImages: [ - { skImage: coldImage, width: 32, height: 32 }, - { skImage: medImage, width: 32, height: 32 }, - { skImage: warmImage, width: 32, height: 32 }, - ], + renderYLabel: ({ x, y, index }) => { + return ( + + ); + }, labelOffset: 12, font, linePathEffect: , diff --git a/lib/src/cartesian/components/AxisImage.tsx b/lib/src/cartesian/components/AxisImage.tsx deleted file mode 100644 index da388611..00000000 --- a/lib/src/cartesian/components/AxisImage.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import React from "react"; -import { - Image, - useImage, - type ImageProps, - type SkImage, -} from "@shopify/react-native-skia"; - -export type TickImage = Partial> & { - image?: string; - skImage?: SkImage | null; - width?: number; - height?: number; -}; - -type AxisImageProps = TickImage & { - y: number; - x: number; -}; - -export const AxisImage: React.FC = ({ - image, - skImage, - y, - x, - ...rest -}) => { - const imageSKFromHook = useImage(image || ""); - const imageSK = skImage || imageSKFromHook; - - return ( - - ); -}; diff --git a/lib/src/cartesian/components/XAxis.tsx b/lib/src/cartesian/components/XAxis.tsx index 99d8a0da..54591f38 100644 --- a/lib/src/cartesian/components/XAxis.tsx +++ b/lib/src/cartesian/components/XAxis.tsx @@ -10,7 +10,6 @@ import type { XAxisProps, XAxisPropsWithDefaults, } from "../../types"; -import { AxisImage } from "./AxisImage"; export const XAxis = < RawData extends Record, @@ -35,7 +34,6 @@ export const XAxis = < chartBounds, enableRescaling, zoom, - tickImages, renderXLabel, }: XAxisProps) => { const xScale = zoom ? zoom.rescaleX(xScaleProp) : xScaleProp; @@ -104,13 +102,7 @@ export const XAxis = < }) ) : ( <> - {tickImages && tickImages[index] && canFitLabelContent && ( - - )} - {font && - labelWidth && - canFitLabelContent && - !tickImages?.[index] ? ( + {font && labelWidth && canFitLabelContent ? ( ) => String(label), linePathEffect, chartBounds, - tickImages, renderYLabel, -}: YAxisProps & { tickImages?: TickImage[] }) => { +}: YAxisProps) => { const [x1 = 0, x2 = 0] = xScale.domain(); const [_ = 0, y2 = 0] = yScale.domain(); const fontSize = font?.getSize() ?? 0; @@ -85,10 +83,7 @@ export const YAxis = < }) ) : ( <> - {tickImages && tickImages[index] && canFitLabelContent && ( - - )} - {font && canFitLabelContent && !tickImages?.[index] && ( + {font && canFitLabelContent && ( React.ReactNode; }; @@ -202,7 +200,6 @@ export type XAxisPropsWithDefaults< | "tickValues" | "linePathEffect" | "enableRescaling" - | "tickImages" | "renderXLabel" > > & @@ -213,7 +210,6 @@ export type XAxisPropsWithDefaults< | "tickValues" | "linePathEffect" | "enableRescaling" - | "tickImages" | "renderXLabel" > >; @@ -257,7 +253,6 @@ export type YAxisInputProps< domain?: YAxisDomain; linePathEffect?: DashPathEffectComponent; enableRescaling?: boolean; - tickImages?: TickImage[]; }; export type YAxisPropsWithDefaults< @@ -270,7 +265,6 @@ export type YAxisPropsWithDefaults< | "tickValues" | "linePathEffect" | "enableRescaling" - | "tickImages" | "renderYLabel" > > & @@ -281,7 +275,6 @@ export type YAxisPropsWithDefaults< | "tickValues" | "linePathEffect" | "enableRescaling" - | "tickImages" | "renderYLabel" > >;