From 69d530c9ff7f9fe88fbc10ee183e8e4ef5cc7f08 Mon Sep 17 00:00:00 2001 From: Eli Zibin <1131641+zibs@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:24:45 -0700 Subject: [PATCH] Add `dashPathEffect` prop to x/y axis (#372) --- .changeset/honest-kiwis-share.md | 5 + example/app/bar-chart.tsx | 10 +- example/app/consts/routes.ts | 5 + example/app/dashed-axes.tsx | 134 ++++++++++++++++++++++ lib/src/cartesian/components/Frame.tsx | 12 +- lib/src/cartesian/components/XAxis.tsx | 6 +- lib/src/cartesian/components/YAxis.tsx | 5 +- lib/src/types.ts | 32 +++++- website/docs/cartesian/cartesian-chart.md | 63 +++++----- 9 files changed, 228 insertions(+), 44 deletions(-) create mode 100644 .changeset/honest-kiwis-share.md create mode 100644 example/app/dashed-axes.tsx diff --git a/.changeset/honest-kiwis-share.md b/.changeset/honest-kiwis-share.md new file mode 100644 index 00000000..94a3a958 --- /dev/null +++ b/.changeset/honest-kiwis-share.md @@ -0,0 +1,5 @@ +--- +"victory-native": minor +--- + +Add dashed path effect option for X and Y axes diff --git a/example/app/bar-chart.tsx b/example/app/bar-chart.tsx index f71ef2cd..be9dbde2 100644 --- a/example/app/bar-chart.tsx +++ b/example/app/bar-chart.tsx @@ -1,4 +1,5 @@ import { + DashPathEffect, LinearGradient, useFont, vec, @@ -56,17 +57,18 @@ export default function BarChartPage(props: { segment: string }) { const date = new Date(2023, value - 1); return date.toLocaleString("default", { month: "short" }); }, + linePathEffect: , + }} + frame={{ + lineWidth: 0, }} yAxis={[ { yKeys: ["listenCount"], font, - lineWidth: 0, + linePathEffect: , }, ]} - frame={{ - lineWidth: 0, - }} data={data} > {({ points, chartBounds }) => { diff --git a/example/app/consts/routes.ts b/example/app/consts/routes.ts index 59271be4..4904f55b 100644 --- a/example/app/consts/routes.ts +++ b/example/app/consts/routes.ts @@ -118,6 +118,11 @@ export const ChartRoutes: { "This is mixture of Pie and Donut charts, showing off the different ways to customize the charts.", path: "/pie-and-donut-charts", }, + { + title: "Dashed Axes", + description: "This is an Area chart with dashed X and Y axes.", + path: "/dashed-axes", + }, ]; if (__DEV__) { diff --git a/example/app/dashed-axes.tsx b/example/app/dashed-axes.tsx new file mode 100644 index 00000000..84ca484e --- /dev/null +++ b/example/app/dashed-axes.tsx @@ -0,0 +1,134 @@ +import { + DashPathEffect, + LinearGradient, + useFont, + vec, +} from "@shopify/react-native-skia"; +import * as React from "react"; +import { SafeAreaView, ScrollView, StyleSheet, View } from "react-native"; +import { Area, CartesianChart, Line } from "victory-native"; +import inter from "../assets/inter-medium.ttf"; +import { appColors } from "./consts/colors"; +import { InfoCard } from "../components/InfoCard"; +import { descriptionForRoute } from "./consts/routes"; +import { Button } from "../components/Button"; + +const generateData = () => + Array.from({ length: 12 }, (_, index) => { + const low = Math.round(20 + 20 * Math.random()); + const high = Math.round(low + 3 + 20 * Math.random()); + + return { + month: new Date(2020, index).toLocaleString("default", { + month: "short", + }), + low, + high, + }; + }); + +export default function DashedAxesPage(props: { segment: string }) { + const description = descriptionForRoute(props.segment); + const font = useFont(inter, 12); + const [data, setData] = React.useState(generateData); + const [, setW] = React.useState(0); + const [, setH] = React.useState(0); + + return ( + <> + + + , + }} + yAxis={[ + { + labelOffset: 8, + yKeys: ["high"], + font, + + linePathEffect: , + }, + ]} + onChartBoundsChange={({ left, right, top, bottom }) => { + setW(right - left); + setH(bottom - top); + }} + > + {({ points, chartBounds }) => ( + <> + + + + + + )} + + + + {description} +