mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-11 10:03:51 +00:00
Added number formatting to charts
This commit is contained in:
@@ -2,10 +2,11 @@ import React, { ChangeEvent, useRef } from 'react';
|
|||||||
import { Doughnut, HorizontalBar } from 'react-chartjs-2';
|
import { Doughnut, HorizontalBar } from 'react-chartjs-2';
|
||||||
import { keys, values } from 'ramda';
|
import { keys, values } from 'ramda';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import Chart, { ChartData, ChartDataSets, ChartOptions } from 'chart.js';
|
import Chart, { ChartData, ChartDataSets, ChartOptions, ChartTooltipItem } from 'chart.js';
|
||||||
import { fillTheGaps } from '../../utils/helpers/visits';
|
import { fillTheGaps } from '../../utils/helpers/visits';
|
||||||
import { Stats } from '../types';
|
import { Stats } from '../types';
|
||||||
import './DefaultChart.scss';
|
import './DefaultChart.scss';
|
||||||
|
import { prettify } from '../../utils/helpers/numbers';
|
||||||
|
|
||||||
export interface DefaultChartProps {
|
export interface DefaultChartProps {
|
||||||
title: Function | string;
|
title: Function | string;
|
||||||
@@ -124,7 +125,13 @@ const DefaultChart = (
|
|||||||
scales: !isBarChart ? undefined : {
|
scales: !isBarChart ? undefined : {
|
||||||
xAxes: [
|
xAxes: [
|
||||||
{
|
{
|
||||||
ticks: { beginAtZero: true, precision: 0, max } as any,
|
ticks: {
|
||||||
|
beginAtZero: true,
|
||||||
|
// @ts-expect-error
|
||||||
|
precision: 0,
|
||||||
|
callback: prettify,
|
||||||
|
max,
|
||||||
|
},
|
||||||
stacked: true,
|
stacked: true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@@ -132,9 +139,27 @@ const DefaultChart = (
|
|||||||
},
|
},
|
||||||
tooltips: {
|
tooltips: {
|
||||||
intersect: !isBarChart,
|
intersect: !isBarChart,
|
||||||
|
|
||||||
// Do not show tooltip on items with empty label when in a bar chart
|
// Do not show tooltip on items with empty label when in a bar chart
|
||||||
filter: ({ yLabel }) => !isBarChart || yLabel !== '',
|
filter: ({ yLabel }) => !isBarChart || yLabel !== '',
|
||||||
|
callbacks: {
|
||||||
|
...isBarChart ? {} : {
|
||||||
|
label({ datasetIndex, index }: ChartTooltipItem, { labels, datasets }: ChartData) {
|
||||||
|
const datasetLabel = index !== undefined && labels?.[index] || '';
|
||||||
|
const value = datasetIndex !== undefined && index !== undefined
|
||||||
|
&& datasets?.[datasetIndex]?.data?.[index]
|
||||||
|
|| '';
|
||||||
|
|
||||||
|
return `${datasetLabel}: ${prettify(Number(value))}`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
...!isBarChart ? {} : {
|
||||||
|
label({ datasetIndex, xLabel }: ChartTooltipItem, { datasets }: ChartData) {
|
||||||
|
const datasetLabel = datasetIndex !== undefined && datasets?.[datasetIndex]?.label || '';
|
||||||
|
|
||||||
|
return `${datasetLabel}: ${prettify(Number(xLabel))}`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
onHover: !isBarChart ? undefined : ((e: ChangeEvent<HTMLElement>, chartElement: HorizontalBar[] | Doughnut[]) => {
|
onHover: !isBarChart ? undefined : ((e: ChangeEvent<HTMLElement>, chartElement: HorizontalBar[] | Doughnut[]) => {
|
||||||
const { target } = e;
|
const { target } = e;
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ import { fillTheGaps } from '../../utils/helpers/visits';
|
|||||||
import { useToggle } from '../../utils/helpers/hooks';
|
import { useToggle } from '../../utils/helpers/hooks';
|
||||||
import { rangeOf } from '../../utils/utils';
|
import { rangeOf } from '../../utils/utils';
|
||||||
import ToggleSwitch from '../../utils/ToggleSwitch';
|
import ToggleSwitch from '../../utils/ToggleSwitch';
|
||||||
import './LineChartCard.scss';
|
|
||||||
import { prettify } from '../../utils/helpers/numbers';
|
import { prettify } from '../../utils/helpers/numbers';
|
||||||
|
import './LineChartCard.scss';
|
||||||
|
|
||||||
interface LineChartCardProps {
|
interface LineChartCardProps {
|
||||||
title: string;
|
title: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user