mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Moved common rendering chart labels code to external module for reuse
This commit is contained in:
parent
67495fa302
commit
871868f0a4
3 changed files with 32 additions and 27 deletions
|
@ -1,7 +1,9 @@
|
|||
import bowser from 'bowser';
|
||||
import { zipObj } from 'ramda';
|
||||
import { ChartData, ChartTooltipItem } from 'chart.js';
|
||||
import { Empty, hasValue } from '../utils';
|
||||
import { Stats, UserAgent } from '../../visits/types';
|
||||
import { prettify } from './numbers';
|
||||
|
||||
const DEFAULT = 'Others';
|
||||
const BROWSERS_WHITELIST = [
|
||||
|
@ -40,3 +42,26 @@ export const extractDomain = (url: string | Empty): string => {
|
|||
|
||||
export const fillTheGaps = (stats: Stats, labels: string[]): number[] =>
|
||||
Object.values({ ...zipObj(labels, labels.map(() => 0)), ...stats });
|
||||
|
||||
export const renderDoughnutChartLabel = (
|
||||
{ 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))}`;
|
||||
};
|
||||
|
||||
export const renderNonDoughnutChartLabel = (labelToPick: 'yLabel' | 'xLabel') => (
|
||||
item: ChartTooltipItem,
|
||||
{ datasets }: ChartData,
|
||||
) => {
|
||||
const { datasetIndex } = item;
|
||||
const value = item[labelToPick];
|
||||
const datasetLabel = datasetIndex !== undefined && datasets?.[datasetIndex]?.label || '';
|
||||
|
||||
return `${datasetLabel}: ${prettify(Number(value))}`;
|
||||
};
|
||||
|
|
|
@ -2,11 +2,11 @@ import React, { ChangeEvent, useRef } from 'react';
|
|||
import { Doughnut, HorizontalBar } from 'react-chartjs-2';
|
||||
import { keys, values } from 'ramda';
|
||||
import classNames from 'classnames';
|
||||
import Chart, { ChartData, ChartDataSets, ChartOptions, ChartTooltipItem } from 'chart.js';
|
||||
import { fillTheGaps } from '../../utils/helpers/visits';
|
||||
import Chart, { ChartData, ChartDataSets, ChartOptions } from 'chart.js';
|
||||
import { fillTheGaps, renderDoughnutChartLabel, renderNonDoughnutChartLabel } from '../../utils/helpers/visits';
|
||||
import { Stats } from '../types';
|
||||
import './DefaultChart.scss';
|
||||
import { prettify } from '../../utils/helpers/numbers';
|
||||
import './DefaultChart.scss';
|
||||
|
||||
export interface DefaultChartProps {
|
||||
title: Function | string;
|
||||
|
@ -142,23 +142,7 @@ const DefaultChart = (
|
|||
// Do not show tooltip on items with empty label when in a bar chart
|
||||
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))}`;
|
||||
},
|
||||
},
|
||||
label: isBarChart ? renderNonDoughnutChartLabel('xLabel') : renderDoughnutChartLabel,
|
||||
},
|
||||
},
|
||||
onHover: !isBarChart ? undefined : ((e: ChangeEvent<HTMLElement>, chartElement: HorizontalBar[] | Doughnut[]) => {
|
||||
|
|
|
@ -11,9 +11,9 @@ import {
|
|||
import { Line } from 'react-chartjs-2';
|
||||
import { always, cond, reverse } from 'ramda';
|
||||
import moment from 'moment';
|
||||
import { ChartData, ChartDataSets, ChartTooltipItem, ChartOptions } from 'chart.js';
|
||||
import { ChartData, ChartDataSets, ChartOptions } from 'chart.js';
|
||||
import { NormalizedVisit, Stats } from '../types';
|
||||
import { fillTheGaps } from '../../utils/helpers/visits';
|
||||
import { fillTheGaps, renderNonDoughnutChartLabel } from '../../utils/helpers/visits';
|
||||
import { useToggle } from '../../utils/helpers/hooks';
|
||||
import { rangeOf } from '../../utils/utils';
|
||||
import ToggleSwitch from '../../utils/ToggleSwitch';
|
||||
|
@ -163,11 +163,7 @@ const LineChartCard = ({ title, visits, highlightedVisits, highlightedLabel = 'S
|
|||
// @ts-expect-error
|
||||
axis: 'x',
|
||||
callbacks: {
|
||||
label({ datasetIndex, yLabel }: ChartTooltipItem, data: ChartData) {
|
||||
const datasetLabel = datasetIndex !== undefined && data.datasets?.[datasetIndex]?.label || '';
|
||||
|
||||
return `${datasetLabel}: ${prettify(Number(yLabel))}`;
|
||||
},
|
||||
label: renderNonDoughnutChartLabel('yLabel'),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue