From fc0e4185ec81ace674454710a19c9432d28cf7cf Mon Sep 17 00:00:00 2001 From: lubej <9722540+lubej@users.noreply.github.com> Date: Tue, 13 Jun 2023 12:25:43 +0200 Subject: [PATCH] Add mobile ticks --- .changelog/511.feature.md | 1 + .changelog/{528.bugfix.md => 529.bugfix.md} | 0 src/app/components/charts/BarChart.tsx | 94 ++++++++++++------- src/app/components/charts/LineChart.tsx | 89 +++++++++++------- .../pages/DashboardPage/TotalTransactions.tsx | 5 +- .../pages/DashboardPage/TransactionsStats.tsx | 4 + src/locales/en/translation.json | 3 +- 7 files changed, 123 insertions(+), 73 deletions(-) create mode 100644 .changelog/511.feature.md rename .changelog/{528.bugfix.md => 529.bugfix.md} (100%) diff --git a/.changelog/511.feature.md b/.changelog/511.feature.md new file mode 100644 index 000000000..988a721c5 --- /dev/null +++ b/.changelog/511.feature.md @@ -0,0 +1 @@ +Abbreviate numbers in transaction charts diff --git a/.changelog/528.bugfix.md b/.changelog/529.bugfix.md similarity index 100% rename from .changelog/528.bugfix.md rename to .changelog/529.bugfix.md diff --git a/src/app/components/charts/BarChart.tsx b/src/app/components/charts/BarChart.tsx index a463a8bd8..29c16daf8 100644 --- a/src/app/components/charts/BarChart.tsx +++ b/src/app/components/charts/BarChart.tsx @@ -9,6 +9,8 @@ import { } from 'recharts' import { TooltipContent, type Formatters } from './Tooltip' import { COLORS } from '../../../styles/theme/colors' +import { Margin } from 'recharts/types/util/types' +import { useTranslation } from 'react-i18next' interface BarChartProps extends Formatters { barSize?: number @@ -19,6 +21,8 @@ interface BarChartProps extends Formatters { rounded?: boolean withBarBackground?: boolean withLabels?: boolean + tickMark?: boolean + margin?: Margin } const BarChartCmp = ({ @@ -31,42 +35,60 @@ const BarChartCmp = ({ rounded, withLabels, withBarBackground, -}: BarChartProps) => ( - - - {cartesianGrid && } - {withLabels && ( - ) => { + const { t } = useTranslation() + + return ( + + + {cartesianGrid && } + {withLabels && ( + { + return tickMark + ? t('common.valuePair', { + value: tick, + formatParams: { + value: { + notation: 'compact', + } satisfies Intl.NumberFormatOptions, + }, + }) + : tick + }} + /> + )} + } + offset={15} + /> + - )} - } - offset={15} - /> - - - -) + + + ) +} export const BarChart = memo(BarChartCmp) as typeof BarChartCmp diff --git a/src/app/components/charts/LineChart.tsx b/src/app/components/charts/LineChart.tsx index 6fb39b48f..d0fe4c942 100644 --- a/src/app/components/charts/LineChart.tsx +++ b/src/app/components/charts/LineChart.tsx @@ -10,6 +10,7 @@ import { Margin } from 'recharts/types/util/types' import { COLORS } from '../../../styles/theme/colors' import { memo, ReactElement } from 'react' import { Formatters, TooltipContent } from './Tooltip' +import { useTranslation } from 'react-i18next' interface LineChartProps extends Formatters { cartesianGrid?: boolean @@ -20,6 +21,7 @@ interface LineChartProps extends Formatters { tickMargin?: number tooltipActiveDotRadius?: number withLabels?: boolean + tickMark?: boolean } const LineChartCmp = ({ @@ -32,40 +34,57 @@ const LineChartCmp = ({ tickMargin = 0, tooltipActiveDotRadius = 5, withLabels, -}: LineChartProps): ReactElement => ( - - - {cartesianGrid && } - - - } - offset={15} - /> - - -) + tickMark, +}: LineChartProps): ReactElement => { + const { t } = useTranslation() + + return ( + + + {cartesianGrid && } + + { + return tickMark + ? t('common.valuePair', { + value: tick, + formatParams: { + value: { + notation: 'compact', + } satisfies Intl.NumberFormatOptions, + }, + }) + : tick + }} + /> + } + offset={15} + /> + + + ) +} export const LineChart = memo(LineChartCmp) as typeof LineChartCmp diff --git a/src/app/pages/DashboardPage/TotalTransactions.tsx b/src/app/pages/DashboardPage/TotalTransactions.tsx index cf4065c58..c60b61ca3 100644 --- a/src/app/pages/DashboardPage/TotalTransactions.tsx +++ b/src/app/pages/DashboardPage/TotalTransactions.tsx @@ -9,8 +9,10 @@ import { DurationPills } from './DurationPills' import { CardHeaderWithResponsiveActions } from './CardHeaderWithResponsiveActions' import { ChartDuration, cumulativeSum } from '../../utils/chart-utils' import { useRequiredScopeParam } from '../../hooks/useScopeParam' +import { useScreenSize } from '../../hooks/useScreensize' export const TotalTransactions: FC = () => { + const { isMobile } = useScreenSize() const { t } = useTranslation() const [chartDuration, setChartDuration] = useState(ChartDuration.MONTH) const statsParams = durationToQueryParams[chartDuration] @@ -42,7 +44,7 @@ export const TotalTransactions: FC = () => { strokeWidth={3} dataKey="tx_volume" data={buckets} - margin={{ left: 16, right: 0 }} + margin={{ left: isMobile ? 0 : 16, right: 0 }} tickMargin={16} withLabels formatters={{ @@ -60,6 +62,7 @@ export const TotalTransactions: FC = () => { timestamp: new Date(value), }), }} + tickMark={isMobile} /> )} diff --git a/src/app/pages/DashboardPage/TransactionsStats.tsx b/src/app/pages/DashboardPage/TransactionsStats.tsx index 2970e046d..af69a291d 100644 --- a/src/app/pages/DashboardPage/TransactionsStats.tsx +++ b/src/app/pages/DashboardPage/TransactionsStats.tsx @@ -13,8 +13,10 @@ import { DurationPills } from './DurationPills' import { CardHeaderWithResponsiveActions } from './CardHeaderWithResponsiveActions' import { ChartDuration } from '../../utils/chart-utils' import { useRequiredScopeParam } from '../../hooks/useScopeParam' +import { useScreenSize } from '../../hooks/useScreensize' export const TransactionsStats: FC = () => { + const { isMobile } = useScreenSize() const { t } = useTranslation() const [chartDuration, setChartDuration] = useState(ChartDuration.MONTH) const statsParams = durationToQueryParams[chartDuration] @@ -65,6 +67,8 @@ export const TransactionsStats: FC = () => { }), }} withLabels + tickMark={isMobile} + margin={{ left: isMobile ? -16 : 8, right: 8 }} /> )} diff --git a/src/locales/en/translation.json b/src/locales/en/translation.json index 2c5a0963a..eafc77b79 100644 --- a/src/locales/en/translation.json +++ b/src/locales/en/translation.json @@ -84,7 +84,8 @@ "paraTimeOnline": "ParaTime Online", "paraTimeOutOfDate": "ParaTime Out of date", "mainnet": "Mainnet", - "testnet": "Testnet" + "testnet": "Testnet", + "valuePair": "{{value, number}}" }, "nodes": { "title": "Active nodes",