Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix(Chart): handle no data volume to 0
Browse files Browse the repository at this point in the history
Signed-off-by: reslene <reslene@numary.com>
  • Loading branch information
reslene committed Mar 8, 2023
1 parent b11495e commit bcd8de4
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 44 deletions.
11 changes: 8 additions & 3 deletions app/routes/apps/$appName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
import Table from '~/src/components/Wrappers/Table';
import { useService } from '~/src/hooks/useService';
import i18n from '~/src/translations';
import { Chart } from '~/src/types/chart';
import { Chart, ChartTypes } from '~/src/types/chart';
import {
Connector,
ConnectorStatuses,
Expand Down Expand Up @@ -157,8 +157,13 @@ export const loader: LoaderFunction = async ({ request, params }) => {
tasks,
connector,
chart: {
pie: buildChart(buildLabels([datasetPie]), [datasetPie]),
line: buildChart(buildLabels([datasetLine], 'LT'), [datasetLine]),
pie: buildChart(
buildLabels([datasetPie]),
[datasetPie],
undefined,
ChartTypes.PIE
),
line: buildChart(buildLabels([datasetLine]), [datasetLine]),
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion app/routes/ledgers/$ledgerId/accounts/$accountId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const loader: LoaderFunction = async ({ params, request }) => {
return {
account: account ? normalizeBalance(account) : undefined,
transactions,
chart: buildChart(buildLabels([dataset], 'LT'), [dataset]),
chart: buildChart(buildLabels([dataset]), [dataset]),
};
}

Expand Down
4 changes: 2 additions & 2 deletions app/routes/overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const getTransactionLedgerChartData = async (
}
}

return buildChart(buildLabels(datasets, 'dd LT'), datasets);
return buildChart(buildLabels(datasets, 'dd LT'), datasets, 'dd LT');
}
};

Expand All @@ -143,7 +143,7 @@ const getPaymentChartData = async (api: ApiClient) => {
if (chart) {
const dataset = buildLineChartDataset(chart);

return buildChart(buildLabels([dataset], 'LT'), [dataset]);
return buildChart(buildLabels([dataset]), [dataset]);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { QueryStats } from '@mui/icons-material';
import { Box, Typography } from '@mui/material';
import { useTranslation } from 'react-i18next';

import { ChartPlaceholderProps, ChartPlaceholderTypes } from './types';
import { ChartPlaceholderProps } from './types';

import { LoadingButton, ObjectOf } from '@numaryhq/storybook';

import Line from '~/src/components/Dataviz/Charts/Line';
import Pie from '~/src/components/Dataviz/Charts/Pie';
import { Chart } from '~/src/types/chart';
import { Chart, ChartTypes } from '~/src/types/chart';

const lineDataMock = {
labels: ['11:00 PM', '1:00 PM', '2:00 PM', '4:00 PM', '8:00 PM', '9:00 PM'],
Expand Down Expand Up @@ -43,8 +43,8 @@ const ChartPlaceholder: FunctionComponent<ChartPlaceholderProps> = ({
}) => {
const { t } = useTranslation();
const chartsMap = {
[ChartPlaceholderTypes.LINE]: <Line data={lineDataMock} height={220} />,
[ChartPlaceholderTypes.PIE]: <Pie data={pieDataMock} height={220} />,
[ChartTypes.LINE]: <Line data={lineDataMock} height={220} />,
[ChartTypes.PIE]: <Pie data={pieDataMock} height={220} />,
} as ObjectOf<ReactElement>;

return (
Expand Down
7 changes: 2 additions & 5 deletions app/src/components/Dataviz/Charts/ChartPlaceholder/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
export enum ChartPlaceholderTypes {
LINE = 'line',
PIE = 'pie',
}
import { ChartTypes } from '~/src/types/chart';

export type ChartPlaceholderProps = {
type: ChartPlaceholderTypes;
type: ChartTypes;
title?: string;
time?: { value: string; kind: string };
};
8 changes: 2 additions & 6 deletions app/src/components/Dataviz/Charts/Line/Line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { useTranslation } from 'react-i18next';
import { LineProps } from './types';

import ChartPlaceholder from '~/src/components/Dataviz/Charts/ChartPlaceholder';
import { ChartPlaceholderTypes } from '~/src/components/Dataviz/Charts/ChartPlaceholder/types';
import { getChartOptions } from '~/src/components/Dataviz/Charts/utils';
import { ChartTypes } from '~/src/types/chart';

ChartJS.register(
CategoryScale,
Expand All @@ -40,11 +40,7 @@ const Line: FunctionComponent<LineProps> = ({
const { t } = useTranslation();
if (data.datasets.length === 0 || data.labels.length === 0) {
return (
<ChartPlaceholder
type={ChartPlaceholderTypes.LINE}
title={title}
time={time}
/>
<ChartPlaceholder type={ChartTypes.LINE} title={title} time={time} />
);
}

Expand Down
10 changes: 2 additions & 8 deletions app/src/components/Dataviz/Charts/Pie/Pie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { useTranslation } from 'react-i18next';
import { PieProps } from './types';

import ChartPlaceholder from '~/src/components/Dataviz/Charts/ChartPlaceholder';
import { ChartPlaceholderTypes } from '~/src/components/Dataviz/Charts/ChartPlaceholder/types';
import { getChartOptions } from '~/src/components/Dataviz/Charts/utils';
import { ChartTypes } from '~/src/types/chart';

ChartJS.register(ArcElement, Tooltip, Legend);

Expand All @@ -23,13 +23,7 @@ const Pie: FunctionComponent<PieProps> = ({
const { t } = useTranslation();

if (data.datasets.length === 0 || data.labels.length === 0) {
return (
<ChartPlaceholder
type={ChartPlaceholderTypes.PIE}
title={title}
time={time}
/>
);
return <ChartPlaceholder type={ChartTypes.PIE} title={title} time={time} />;
}

return (
Expand Down
46 changes: 32 additions & 14 deletions app/src/components/Dataviz/Charts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { compact, flatten, get, omit, sortedUniq } from 'lodash';

import { ObjectOf, theme } from '@numaryhq/storybook';

import { Chart, ChartDataset } from '~/src/types/chart';
import { Chart, ChartDataset, ChartTypes } from '~/src/types/chart';
import { BooleanConfig, SearchTargets } from '~/src/types/search';

export const buildQueryPayloadMatchPhrase = (
Expand Down Expand Up @@ -125,21 +125,40 @@ export const getRandomContrast = (): 'bright' | 'normal' | 'darker' =>

export const buildChart = (
labels: (string | Date)[],
datasets: any
): Chart => ({
labels,
datasets: compact(
datasets.map((dataset: ChartDataset) => {
if (dataset && dataset.data) {
return omit(dataset, ['labels']);
}
})
),
});
datasets: any,
reverseDateFormat = 'LT',
type = ChartTypes.LINE
): Chart => {
console.log(type);
if (type === ChartTypes.LINE) {
datasets.forEach((dataset: ChartDataset) => {
const dataFormated = labels.map(() => 0);
dataset.labels.forEach((label: string | Date, index: number) => {
const myIndex = labels.findIndex(
(item: string | Date) =>
dayjs(label).format(reverseDateFormat) === item
);
dataFormated[myIndex] = dataset.data[index];
});
dataset.data = dataFormated;
});
}

return {
labels,
datasets: compact(
datasets.map((dataset: ChartDataset) => {
if (dataset && dataset.data) {
return omit(dataset, ['labels']);
}
})
),
};
};

export const buildLabels = (
datasets: any,
dateFormat = 'ddd D MMM'
dateFormat = 'LT'
): (string | Date)[] => {
const labels = sortedUniq(
flatten(compact(datasets.map((dataset: ChartDataset) => dataset.labels)))
Expand All @@ -149,7 +168,6 @@ export const buildLabels = (
) as string[];

return uniqLabels

.sort((a, b) => (dayjs(a) < dayjs(b) ? 1 : -1))
.reverse()
.map((item: Date | string) => {
Expand Down
2 changes: 1 addition & 1 deletion app/src/translations/en/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default {
title: 'Overview',
status: 'Activity',
charts: {
transaction: 'Transactions per ledger top 3',
transaction: 'Transactions volume per ledger top 3',
payment: 'Payments volume',
},
tasks: {
Expand Down
5 changes: 5 additions & 0 deletions app/src/types/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export type Chart = {
labels: (Date | string)[];
datasets: Omit<ChartDataset, 'labels'>[];
};

export enum ChartTypes {
LINE = 'line',
PIE = 'pie',
}

0 comments on commit bcd8de4

Please sign in to comment.