From 8b8ea61a137f86dc1eefd412b0d0cdfe2608b9b8 Mon Sep 17 00:00:00 2001 From: amaliadwiy Date: Mon, 20 Apr 2026 16:27:44 +0800 Subject: [PATCH] fix: grafik bar --- components/division/reportChartDocument.tsx | 46 ++++++++++++------- components/division/reportChartEvent.tsx | 50 ++++++++++++--------- components/home/chartDokumenHome.tsx | 18 +++++--- 3 files changed, 72 insertions(+), 42 deletions(-) diff --git a/components/division/reportChartDocument.tsx b/components/division/reportChartDocument.tsx index 9cb0957..80beea6 100644 --- a/components/division/reportChartDocument.tsx +++ b/components/division/reportChartDocument.tsx @@ -2,40 +2,54 @@ import Styles from "@/constants/Styles"; import { Dimensions, View } from "react-native"; import { BarChart } from "react-native-gifted-charts"; import { useTheme } from "@/providers/ThemeProvider"; +import { useMemo } from "react"; import Text from "../Text"; export default function ReportChartDocument({ data }: { data: { label: string; value: number; }[] }) { const { colors } = useTheme(); - const maxValue = Math.max(...data.map(i => i.value)) const width = Dimensions.get("window").width; + + const maxValue = useMemo(() => { + const maxVal = data.reduce((max: number, obj: { value: number; }) => Math.max(max, obj.value), 0); + if (maxVal === 0) return 10; + if (maxVal < 5) return 5; + return Math.ceil(maxVal / 10) * 10; + }, [data]); + + const barData = useMemo(() => { + return data.map(item => ({ + ...item, + frontColor: item.value > 0 ? "#fac858" : "transparent", + topLabelComponent: () => ( + + + {item.value > 0 ? item.value : ""} + + + ) + })) + }, [data, colors.text]); return ( JUMLAH DOKUMEN { - return ( - - {item.value} - - ); - }} /> ) diff --git a/components/division/reportChartEvent.tsx b/components/division/reportChartEvent.tsx index ec3398e..c9f9235 100644 --- a/components/division/reportChartEvent.tsx +++ b/components/division/reportChartEvent.tsx @@ -2,44 +2,54 @@ import Styles from "@/constants/Styles"; import { Dimensions, View } from "react-native"; import { BarChart } from "react-native-gifted-charts"; import { useTheme } from "@/providers/ThemeProvider"; +import { useMemo } from "react"; import Text from "../Text"; export default function ReportChartEvent({ data }: { data: { label: string; value: number; }[] }) { const { colors } = useTheme(); const width = Dimensions.get("window").width; - const maxValue = Math.max(...data.map(i => i.value)) - const barData = [ - { value: 23, label: 'Akan Datang', }, - { value: 12, label: 'Selesai' }, - ]; + + const maxValue = useMemo(() => { + const maxVal = data.reduce((max: number, obj: { value: number; }) => Math.max(max, obj.value), 0); + if (maxVal === 0) return 10; + if (maxVal < 5) return 5; + return Math.ceil(maxVal / 10) * 10; + }, [data]); + + const barData = useMemo(() => { + return data.map(item => ({ + ...item, + frontColor: item.value > 0 ? "#177AD5" : "transparent", + topLabelComponent: () => ( + + + {item.value > 0 ? item.value : ""} + + + ) + })) + }, [data, colors.text]); return ( ACARA DIVISI { - return ( - - {item.value} - - ); - }} /> ) diff --git a/components/home/chartDokumenHome.tsx b/components/home/chartDokumenHome.tsx index fa87790..c36d7ed 100644 --- a/components/home/chartDokumenHome.tsx +++ b/components/home/chartDokumenHome.tsx @@ -32,11 +32,6 @@ export default function ChartDokumenHome({ refreshing }: { refreshing: boolean } frontColor: val > 0 ? (item.color || '#fac858') : 'transparent', label: item.label, value: val, - topLabelComponent: () => ( - - {val > 0 ? val : ""} - - ) } }) as Props }, @@ -52,6 +47,17 @@ export default function ChartDokumenHome({ refreshing }: { refreshing: boolean } if (maxVal < 5) return 5; return Math.ceil(maxVal / 10) * 10; }, [chartData]); + + const barData = useMemo(() => { + return chartData.map(item => ({ + ...item, + topLabelComponent: () => ( + + {item.value > 0 ? item.value : ""} + + ) + })) + }, [chartData, colors.text]); return ( @@ -65,7 +71,7 @@ export default function ChartDokumenHome({ refreshing }: { refreshing: boolean } showYAxisIndices noOfSections={maxValue < 5 ? (maxValue === 0 ? 4 : maxValue) : 4} maxValue={maxValue} - data={chartData} + data={barData} isAnimated width={width - 140} barWidth={width * 0.25}