Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(web): charts-improvements #1780

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions web/src/pages/Home/CourtOverview/BarChart.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -44,6 +45,16 @@ const BarChart: React.FC<IBarChartProps> = ({ 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"> = {
Expand Down Expand Up @@ -95,13 +106,14 @@ const BarChart: React.FC<IBarChartProps> = ({ chartData }) => {
<BarContainer>
<Bar
data={{
labels: chartData.labels,
labels: sortedData.labels,
datasets: [
{
data: chartData.data,
data: sortedData.data,
backgroundColor: theme.secondaryPurple,
hoverBackgroundColor: theme.primaryBlue,
maxBarThickness: 60,
borderRadius: 3,
},
],
}}
Expand Down
28 changes: 17 additions & 11 deletions web/src/pages/Home/CourtOverview/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { responsiveSize } from "styles/responsiveSize";

import { StyledSkeleton } from "components/StyledSkeleton";

import StakedPNKByCourtsChart, { StakedPNKByCourtsChartData } from "./StakedPNKByCourtsChart";
import CasesByCourtsChart, { CasesByCourtsChartData } from "./CasesByCourtsChart";
import StakedPNKByCourtsChart, { StakedPNKByCourtsChartData } from "./StakedPNKByCourtsChart";
import TimeSeriesChart from "./TimeSeriesChart";

const Container = styled.div`
Expand Down Expand Up @@ -74,22 +74,28 @@ const Chart: React.FC = () => {

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 }
);
Expand Down
2 changes: 2 additions & 0 deletions web/src/pages/Home/CourtOverview/TimeSeriesChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const TimeSeriesChart: React.FC<ITimeSeriesChart> = ({ data }) => {
grid: { display: false },
ticks: {
color: theme.secondaryText,
autoSkipPadding: 10,
maxRotation: 0,
},
},
y: {
Expand Down
Loading