|
| 1 | +import * as React from 'react'; |
| 2 | +import { useColorMode } from '@chakra-ui/react'; |
| 3 | +import { |
| 4 | + Chart as ChartJS, |
| 5 | + CategoryScale, |
| 6 | + LinearScale, |
| 7 | + Title, |
| 8 | + Tooltip, |
| 9 | + Legend, |
| 10 | + ChartData, |
| 11 | + BarElement, |
| 12 | + CoreChartOptions, |
| 13 | + ElementChartOptions, |
| 14 | + PluginChartOptions, |
| 15 | + DatasetChartOptions, |
| 16 | + ScaleChartOptions, |
| 17 | + BarControllerChartOptions, |
| 18 | +} from 'chart.js'; |
| 19 | +import { _DeepPartialObject } from 'chart.js/types/utils'; |
| 20 | +import { Bar } from 'react-chartjs-2'; |
| 21 | +import { useTranslation } from 'react-i18next'; |
| 22 | +import GraphStatDisplay from 'components/Containers/GraphStatDisplay'; |
| 23 | +import { COLORS } from 'constants/colors'; |
| 24 | +import { |
| 25 | + FirmwareDashboardRevision, |
| 26 | +} from 'hooks/Network/Firmware'; |
| 27 | + |
| 28 | +ChartJS.register( |
| 29 | + CategoryScale, |
| 30 | + LinearScale, |
| 31 | + BarElement, |
| 32 | + Title, |
| 33 | + Tooltip, |
| 34 | + Legend, |
| 35 | +); |
| 36 | + |
| 37 | +const OPTIONS: ( |
| 38 | + colorMode: string, |
| 39 | +) => _DeepPartialObject< |
| 40 | + CoreChartOptions<'bar'> & |
| 41 | + ElementChartOptions<'bar'> & |
| 42 | + PluginChartOptions<'bar'> & |
| 43 | + DatasetChartOptions<'bar'> & |
| 44 | + ScaleChartOptions<'bar'> & |
| 45 | + BarControllerChartOptions |
| 46 | +> = (colorMode) => ({ |
| 47 | + responsive: true, |
| 48 | + indexAxis: 'y', |
| 49 | + interaction: { |
| 50 | + mode: 'nearest', |
| 51 | + axis: 'y', |
| 52 | + intersect: false, |
| 53 | + }, |
| 54 | + scales: { |
| 55 | + xAxes: { |
| 56 | + ticks: { |
| 57 | + color: colorMode === 'dark' ? 'white' : undefined, |
| 58 | + }, |
| 59 | + }, |
| 60 | + yAxes: { |
| 61 | + ticks: { |
| 62 | + color: colorMode === 'dark' ? 'white' : undefined, |
| 63 | + callback(value) { |
| 64 | + return ( |
| 65 | + this.getLabelForValue(value as number) |
| 66 | + ?.split(' ')?.[0] ?? '' |
| 67 | + ); |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + plugins: { |
| 73 | + legend: { display: false }, |
| 74 | + title: { |
| 75 | + display: false, |
| 76 | + }, |
| 77 | + tooltip: { |
| 78 | + callbacks: { |
| 79 | + label: (context) => |
| 80 | + `${context.label}: ${context.formattedValue} (${ |
| 81 | + Math.round( |
| 82 | + // @ts-ignore |
| 83 | + (context.raw / |
| 84 | + context.dataset.data.reduce( |
| 85 | + (acc, curr) => acc + curr, |
| 86 | + 0, |
| 87 | + )) * |
| 88 | + 100 * |
| 89 | + 100, |
| 90 | + ) / 100 |
| 91 | + }%)`, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | +}); |
| 96 | + |
| 97 | +type Props = { |
| 98 | + data: FirmwareDashboardRevision[]; |
| 99 | +}; |
| 100 | +const RecognizedFirmwareBarChart = ({ data }: Props) => { |
| 101 | + const { t } = useTranslation(); |
| 102 | + const { colorMode } = useColorMode(); |
| 103 | + |
| 104 | + const parsedData: ChartData<'bar', number[], unknown> = |
| 105 | + React.useMemo(() => { |
| 106 | + const values: number[] = []; |
| 107 | + const labels: string[] = []; |
| 108 | + |
| 109 | + for (const { tag, value } of data.sort( |
| 110 | + (a, b) => b.value - a.value, |
| 111 | + )) { |
| 112 | + values.push(value); |
| 113 | + const split = tag.split(' / '); |
| 114 | + if (split[1]) labels.push(split['1']); |
| 115 | + else labels.push(tag === '' ? t('common.unknown') : tag); |
| 116 | + } |
| 117 | + |
| 118 | + return { |
| 119 | + labels, |
| 120 | + datasets: [ |
| 121 | + { |
| 122 | + data: values, |
| 123 | + backgroundColor: COLORS, |
| 124 | + borderColor: COLORS, |
| 125 | + borderWidth: 1, |
| 126 | + }, |
| 127 | + ], |
| 128 | + }; |
| 129 | + }, [data, colorMode]); |
| 130 | + |
| 131 | + return ( |
| 132 | + <GraphStatDisplay |
| 133 | + title={t('controller.firmware.recognized_firmware')} |
| 134 | + explanation={t( |
| 135 | + 'controller.firmware.recognized_firmware_explanation', |
| 136 | + )} |
| 137 | + chart={ |
| 138 | + <Bar |
| 139 | + data={parsedData} |
| 140 | + height="300px" |
| 141 | + options={OPTIONS(colorMode)} |
| 142 | + /> |
| 143 | + } |
| 144 | + /> |
| 145 | + ); |
| 146 | +}; |
| 147 | + |
| 148 | +export default RecognizedFirmwareBarChart; |
0 commit comments