2022-06-26 00:49:25 +00:00
|
|
|
import {
|
|
|
|
Chart as ChartJS,
|
|
|
|
ChartData,
|
|
|
|
ChartDataset,
|
|
|
|
ChartOptions,
|
|
|
|
Filler,
|
|
|
|
LinearScale,
|
|
|
|
LineElement,
|
|
|
|
PointElement,
|
|
|
|
} from 'chart.js';
|
|
|
|
import { DeepPartial } from 'ts-essentials';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import { deepmerge, deepmergeCustom } from 'deepmerge-ts';
|
|
|
|
import { theme } from 'twin.macro';
|
2022-06-26 18:34:09 +00:00
|
|
|
import { hexToRgba } from '@/lib/helpers';
|
2022-06-26 00:49:25 +00:00
|
|
|
|
|
|
|
ChartJS.register(LineElement, PointElement, Filler, LinearScale);
|
|
|
|
|
|
|
|
const options: ChartOptions<'line'> = {
|
|
|
|
responsive: true,
|
|
|
|
animation: false,
|
|
|
|
plugins: {
|
|
|
|
legend: { display: false },
|
|
|
|
title: { display: false },
|
|
|
|
tooltip: { enabled: false },
|
|
|
|
},
|
|
|
|
layout: {
|
|
|
|
padding: 0,
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
x: {
|
|
|
|
min: 0,
|
|
|
|
max: 19,
|
|
|
|
type: 'linear',
|
|
|
|
grid: {
|
|
|
|
display: false,
|
|
|
|
drawBorder: false,
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
min: 0,
|
|
|
|
type: 'linear',
|
|
|
|
grid: {
|
|
|
|
display: true,
|
2022-12-16 02:06:14 +00:00
|
|
|
color: theme('colors.slate.700'),
|
2022-06-26 00:49:25 +00:00
|
|
|
drawBorder: false,
|
|
|
|
},
|
|
|
|
ticks: {
|
|
|
|
display: true,
|
|
|
|
count: 3,
|
2022-12-16 02:06:14 +00:00
|
|
|
color: theme('colors.slate.200'),
|
2022-06-26 00:49:25 +00:00
|
|
|
font: {
|
|
|
|
family: theme('fontFamily.sans'),
|
|
|
|
size: 11,
|
|
|
|
weight: '400',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
elements: {
|
|
|
|
point: {
|
|
|
|
radius: 0,
|
|
|
|
},
|
|
|
|
line: {
|
2022-06-27 23:59:36 +00:00
|
|
|
tension: 0.15,
|
2022-06-26 00:49:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
function getOptions(opts?: DeepPartial<ChartOptions<'line'>> | undefined): ChartOptions<'line'> {
|
2022-11-25 20:25:03 +00:00
|
|
|
// @ts-expect-error go away
|
|
|
|
return deepmerge(options, opts ?? {});
|
2022-06-26 00:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ChartDatasetCallback = (value: ChartDataset<'line'>, index: number) => ChartDataset<'line'>;
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
function getEmptyData(label: string, sets = 1, callback?: ChartDatasetCallback | undefined): ChartData<'line'> {
|
2022-11-25 20:25:03 +00:00
|
|
|
const next = callback || (value => value);
|
2022-06-26 00:49:25 +00:00
|
|
|
|
|
|
|
return {
|
2022-06-26 19:13:52 +00:00
|
|
|
labels: Array(20)
|
|
|
|
.fill(0)
|
|
|
|
.map((_, index) => index),
|
|
|
|
datasets: Array(sets)
|
|
|
|
.fill(0)
|
|
|
|
.map((_, index) =>
|
|
|
|
next(
|
|
|
|
{
|
|
|
|
fill: true,
|
|
|
|
label,
|
2022-06-27 23:59:36 +00:00
|
|
|
data: Array(20).fill(-5),
|
2022-06-26 19:13:52 +00:00
|
|
|
borderColor: theme('colors.cyan.400'),
|
|
|
|
backgroundColor: hexToRgba(theme('colors.cyan.700'), 0.5),
|
|
|
|
},
|
2022-11-25 20:25:03 +00:00
|
|
|
index,
|
|
|
|
),
|
2022-06-26 19:13:52 +00:00
|
|
|
),
|
2022-06-26 00:49:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const merge = deepmergeCustom({ mergeArrays: false });
|
|
|
|
|
|
|
|
interface UseChartOptions {
|
|
|
|
sets: number;
|
|
|
|
options?: DeepPartial<ChartOptions<'line'>> | number | undefined;
|
|
|
|
callback?: ChartDatasetCallback | undefined;
|
|
|
|
}
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
function useChart(label: string, opts?: UseChartOptions) {
|
|
|
|
const options = getOptions(
|
2022-11-25 20:25:03 +00:00
|
|
|
typeof opts?.options === 'number' ? { scales: { y: { min: 0, suggestedMax: opts.options } } } : opts?.options,
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
|
|
|
const [data, setData] = useState(getEmptyData(label, opts?.sets || 1, opts?.callback));
|
2022-06-26 00:49:25 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const push = (items: number | null | (number | null)[]) =>
|
2022-11-25 20:25:03 +00:00
|
|
|
setData(state =>
|
2022-06-26 19:13:52 +00:00
|
|
|
merge(state, {
|
|
|
|
datasets: (Array.isArray(items) ? items : [items]).map((item, index) => ({
|
|
|
|
...state.datasets[index],
|
2022-11-25 20:25:03 +00:00
|
|
|
data:
|
|
|
|
state.datasets[index]?.data
|
|
|
|
?.slice(1)
|
|
|
|
?.concat(typeof item === 'number' ? Number(item.toFixed(2)) : item) ?? [],
|
2022-06-26 19:13:52 +00:00
|
|
|
})),
|
2022-11-25 20:25:03 +00:00
|
|
|
}),
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
2022-06-26 00:49:25 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const clear = () =>
|
2022-11-25 20:25:03 +00:00
|
|
|
setData(state =>
|
2022-06-26 19:13:52 +00:00
|
|
|
merge(state, {
|
2022-11-25 20:25:03 +00:00
|
|
|
datasets: state.datasets.map(value => ({
|
2022-06-26 19:13:52 +00:00
|
|
|
...value,
|
2022-06-27 23:59:36 +00:00
|
|
|
data: Array(20).fill(-5),
|
2022-06-26 19:13:52 +00:00
|
|
|
})),
|
2022-11-25 20:25:03 +00:00
|
|
|
}),
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
2022-06-26 00:49:25 +00:00
|
|
|
|
|
|
|
return { props: { data, options }, push, clear };
|
|
|
|
}
|
|
|
|
|
2022-07-01 00:23:27 +00:00
|
|
|
function useChartTickLabel(label: string, max: number, tickLabel: string, roundTo?: number) {
|
2022-06-26 00:49:25 +00:00
|
|
|
return useChart(label, {
|
|
|
|
sets: 1,
|
|
|
|
options: {
|
|
|
|
scales: {
|
|
|
|
y: {
|
|
|
|
suggestedMax: max,
|
|
|
|
ticks: {
|
2022-06-26 19:13:52 +00:00
|
|
|
callback(value) {
|
2022-07-01 00:23:27 +00:00
|
|
|
return `${roundTo ? Number(value).toFixed(roundTo) : value}${tickLabel}`;
|
2022-06-26 00:49:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useChart, useChartTickLabel, getOptions, getEmptyData };
|