From a194f04510b954f9d9b3fa90b2e818a1c305f7d4 Mon Sep 17 00:00:00 2001 From: Harman-singh-waraich Date: Tue, 10 Dec 2024 19:44:43 +0530 Subject: [PATCH] refactor(web): charts-improvements --- web/src/pages/Home/CourtOverview/BarChart.tsx | 18 ++++++++++-- web/src/pages/Home/CourtOverview/Chart.tsx | 28 +++++++++++-------- .../Home/CourtOverview/TimeSeriesChart.tsx | 2 ++ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/web/src/pages/Home/CourtOverview/BarChart.tsx b/web/src/pages/Home/CourtOverview/BarChart.tsx index d75b707d2..647a863c9 100644 --- a/web/src/pages/Home/CourtOverview/BarChart.tsx +++ b/web/src/pages/Home/CourtOverview/BarChart.tsx @@ -1,5 +1,6 @@ -import React, { useCallback } from "react"; +import React, { useCallback, useMemo } from "react"; import styled, { useTheme } from "styled-components"; + import { Chart as ChartJS, BarElement, @@ -44,6 +45,16 @@ const BarChart: React.FC = ({ chartData }) => { const formatPNKValue = useCallback((value: number) => formatter.format(value), []); + const sortedData = useMemo(() => { + const sortedIndices = chartData.data.map((value, index) => ({ value, index })).sort((a, b) => b.value - a.value); + + return { + labels: sortedIndices.map((item) => chartData.labels[item.index]), + data: sortedIndices.map((item) => chartData.data[item.index]), + total: chartData.total, + }; + }, [chartData]); + const tickSize = 5; // suggested, if that many labels can't fit, chart will use even labels const options: ChartOptions<"bar"> = { @@ -95,13 +106,14 @@ const BarChart: React.FC = ({ chartData }) => { { const processedCourtsData = courtsChartData?.reduce( (accData: CasesByCourtsChartData, current) => { - return { - labels: [...accData.labels, current.name ?? ""], - cases: [...accData.cases, current.numberDisputes], - totalCases: accData.totalCases + parseInt(current.numberDisputes, 10), - }; + if (BigInt(current.numberDisputes) > 0) { + return { + labels: [...accData.labels, current.name ?? ""], + cases: [...accData.cases, current.numberDisputes], + totalCases: accData.totalCases + parseInt(current.numberDisputes, 10), + }; + } + return accData; }, { labels: [], cases: [], totalCases: 0 } ); const processedStakedPNKData = courtsChartData?.reduce( (accData: StakedPNKByCourtsChartData, current) => { - return { - labels: [...accData.labels, current.name ?? ""], - stakes: [...accData.stakes, parseFloat(formatUnits(current.stake, 18))], - totalStake: accData.totalStake + parseFloat(formatUnits(current.stake, 18)), - }; + if (BigInt(current.stake) > 0) { + return { + labels: [...accData.labels, current.name ?? ""], + stakes: [...accData.stakes, parseFloat(formatUnits(current.stake, 18))], + totalStake: accData.totalStake + parseFloat(formatUnits(current.stake, 18)), + }; + } + return accData; }, { labels: [], stakes: [], totalStake: 0 } ); diff --git a/web/src/pages/Home/CourtOverview/TimeSeriesChart.tsx b/web/src/pages/Home/CourtOverview/TimeSeriesChart.tsx index 41eb1ce59..594f6efb9 100644 --- a/web/src/pages/Home/CourtOverview/TimeSeriesChart.tsx +++ b/web/src/pages/Home/CourtOverview/TimeSeriesChart.tsx @@ -48,6 +48,8 @@ const TimeSeriesChart: React.FC = ({ data }) => { grid: { display: false }, ticks: { color: theme.secondaryText, + autoSkipPadding: 10, + maxRotation: 0, }, }, y: {