|
| 1 | +import React, { useCallback } from "react"; |
| 2 | +import styled, { useTheme } from "styled-components"; |
| 3 | +import { |
| 4 | + Chart as ChartJS, |
| 5 | + BarElement, |
| 6 | + Tooltip, |
| 7 | + CategoryScale, |
| 8 | + LinearScale, |
| 9 | + PointElement, |
| 10 | + LineElement, |
| 11 | + TimeScale, |
| 12 | + ChartOptions, |
| 13 | +} from "chart.js"; |
| 14 | +import ChartDataLabels from "chartjs-plugin-datalabels"; |
| 15 | +import { Bar } from "react-chartjs-2"; |
| 16 | +import "chartjs-adapter-moment"; |
| 17 | + |
| 18 | +ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, TimeScale, Tooltip); |
| 19 | +const formatter = new Intl.NumberFormat("en", { notation: "compact" }); |
| 20 | + |
| 21 | +const BarContainer = styled.div` |
| 22 | + height: 220px; |
| 23 | + margin-top: 16px; |
| 24 | +`; |
| 25 | + |
| 26 | +ChartJS.register(BarElement); |
| 27 | + |
| 28 | +export interface IBarChartData { |
| 29 | + labels: string[]; |
| 30 | + data: number[]; |
| 31 | + total: number; |
| 32 | +} |
| 33 | + |
| 34 | +interface IBarChartProps { |
| 35 | + chartData: IBarChartData; |
| 36 | +} |
| 37 | + |
| 38 | +const BarChart: React.FC<IBarChartProps> = ({ chartData }) => { |
| 39 | + const theme = useTheme(); |
| 40 | + const getPercentValue = useCallback( |
| 41 | + (value: number) => `${Math.floor((value * 100) / chartData.total)} %`, |
| 42 | + [chartData] |
| 43 | + ); |
| 44 | + |
| 45 | + const formatPNKValue = useCallback((value: number) => formatter.format(value), []); |
| 46 | + |
| 47 | + const tickSize = 5; // suggested, if that many labels can't fit, chart will use even labels |
| 48 | + |
| 49 | + const options: ChartOptions<"bar"> = { |
| 50 | + responsive: true, |
| 51 | + maintainAspectRatio: false, |
| 52 | + scales: { |
| 53 | + x: { |
| 54 | + grid: { display: false }, |
| 55 | + ticks: { |
| 56 | + color: theme.secondaryText, |
| 57 | + }, |
| 58 | + }, |
| 59 | + y: { |
| 60 | + grid: { color: theme.stroke, borderDash: [4, 4] }, |
| 61 | + ticks: { |
| 62 | + color: theme.secondaryText, |
| 63 | + stepSize: (chartData.total * tickSize) / 100, |
| 64 | + callback: (value) => getPercentValue(value), |
| 65 | + }, |
| 66 | + max: chartData.total, |
| 67 | + }, |
| 68 | + }, |
| 69 | + plugins: { |
| 70 | + datalabels: { |
| 71 | + anchor: "end", |
| 72 | + align: "top", |
| 73 | + offset: -4, |
| 74 | + color: theme.primaryText, |
| 75 | + font: { |
| 76 | + weight: "bold", |
| 77 | + }, |
| 78 | + formatter: formatPNKValue, |
| 79 | + }, |
| 80 | + tooltip: { |
| 81 | + backgroundColor: theme.whiteBackground, |
| 82 | + titleColor: theme.primaryText, |
| 83 | + borderColor: theme.stroke, |
| 84 | + borderWidth: 1, |
| 85 | + displayColors: false, |
| 86 | + callbacks: { |
| 87 | + label: (context) => getPercentValue(context.parsed.y), |
| 88 | + labelTextColor: () => theme.primaryText, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + return ( |
| 95 | + <BarContainer> |
| 96 | + <Bar |
| 97 | + data={{ |
| 98 | + labels: chartData.labels, |
| 99 | + datasets: [ |
| 100 | + { |
| 101 | + data: chartData.data, |
| 102 | + backgroundColor: theme.secondaryPurple, |
| 103 | + hoverBackgroundColor: theme.primaryBlue, |
| 104 | + maxBarThickness: 60, |
| 105 | + }, |
| 106 | + ], |
| 107 | + }} |
| 108 | + options={options} |
| 109 | + plugins={[ChartDataLabels]} |
| 110 | + /> |
| 111 | + </BarContainer> |
| 112 | + ); |
| 113 | +}; |
| 114 | + |
| 115 | +export default BarChart; |
0 commit comments