From bbafc32cf8a3b4369a2ca3858426fafbc5512900 Mon Sep 17 00:00:00 2001 From: Eli Zibin Date: Tue, 6 Feb 2024 13:48:50 -0800 Subject: [PATCH 01/15] feat: pie chart This is a v1 pie chart implementation that renders pie charts, without labels and without gestures. It sort of only has one option beyond the pie chart, which is to render an inset inside the chart, which is like a border of each slice. No docs yet either, beside the example page, which only renders in dev mode currently. --- example/app/consts/routes.ts | 6 ++ example/app/pie-chart.tsx | 122 +++++++++++++++++++++++++ lib/src/index.ts | 9 ++ lib/src/pie/PieChart.tsx | 93 +++++++++++++++++++ lib/src/pie/PieSlice.tsx | 27 ++++++ lib/src/pie/PieSliceInset.tsx | 24 +++++ lib/src/pie/hooks/useSliceInsetPath.ts | 53 +++++++++++ lib/src/pie/hooks/useSlicePath.ts | 71 ++++++++++++++ lib/src/pie/utils/radians.ts | 16 ++++ lib/src/types.ts | 3 + 10 files changed, 424 insertions(+) create mode 100644 example/app/pie-chart.tsx create mode 100644 lib/src/pie/PieChart.tsx create mode 100644 lib/src/pie/PieSlice.tsx create mode 100644 lib/src/pie/PieSliceInset.tsx create mode 100644 lib/src/pie/hooks/useSliceInsetPath.ts create mode 100644 lib/src/pie/hooks/useSlicePath.ts create mode 100644 lib/src/pie/utils/radians.ts diff --git a/example/app/consts/routes.ts b/example/app/consts/routes.ts index 6c00c8cd..401ac9d2 100644 --- a/example/app/consts/routes.ts +++ b/example/app/consts/routes.ts @@ -85,6 +85,12 @@ if (__DEV__) { "This page shows multiple charts with gestures inside a scrollview to ensure both platforms allow for this behavior.", path: "/scrollview-charts", }, + { + title: "Pie Chart (in progres)", + description: + "This is a simple Pie chart in Victory that supports insets between each slice. No labels, no gestures...yet!! (coming soon ™️)", + path: "/pie-chart", + }, ); } diff --git a/example/app/pie-chart.tsx b/example/app/pie-chart.tsx new file mode 100644 index 00000000..7c99283e --- /dev/null +++ b/example/app/pie-chart.tsx @@ -0,0 +1,122 @@ +import React, { useState } from "react"; +import { SafeAreaView, ScrollView, StyleSheet, View } from "react-native"; +import { PieChart, PieSlice, PieSliceInset } from "victory-native"; +import { InfoCard } from "example/components/InfoCard"; +import { Button } from "example/components/Button"; +import { InputSlider } from "example/components/InputSlider"; +import { InputColor } from "example/components/InputColor"; +import { appColors } from "./consts/colors"; +import { descriptionForRoute } from "./consts/routes"; + +const randomNumber = () => Math.floor(Math.random() * (50 - 25 + 1)) + 25; +function generateRandomColor(): string { + // Generating a random number between 0 and 0xFFFFFF + const randomColor = Math.floor(Math.random() * 0xffffff); + // Converting the number to a hexadecimal string and padding with zeros + return `#${randomColor.toString(16).padStart(6, "0")}`; +} + +const DATA = (numberPoints = 5) => + Array.from({ length: numberPoints }, (_, index) => ({ + value: randomNumber(), + color: generateRandomColor(), + label: index % 2 ? "Even Label" : "Odd Label", + })); + +export default function PieChartPlayground(props: { segment: string }) { + const description = descriptionForRoute(props.segment); + const [data, setData] = useState(DATA(5)); + const [insetWidth, setInsetWidth] = useState(4); + const [insetColor, setInsetColor] = useState("#fafafa"); + + return ( + + + + + {({ slice, size }) => { + return ( + <> + + + + ); + }} + + + + + {description} + +