2021-09-23 15:43:43 +05:30
|
|
|
import {
|
2021-10-20 09:24:55 +05:30
|
|
|
ActiveElement,
|
2021-09-23 15:43:43 +05:30
|
|
|
BarController,
|
|
|
|
|
BarElement,
|
|
|
|
|
CategoryScale,
|
|
|
|
|
Chart,
|
2021-10-20 09:24:55 +05:30
|
|
|
ChartData,
|
|
|
|
|
ChartEvent,
|
2021-09-23 15:43:43 +05:30
|
|
|
ChartOptions,
|
|
|
|
|
ChartType,
|
|
|
|
|
Decimation,
|
|
|
|
|
Filler,
|
|
|
|
|
Legend,
|
|
|
|
|
LinearScale,
|
|
|
|
|
LineController,
|
|
|
|
|
LineElement,
|
|
|
|
|
PointElement,
|
|
|
|
|
SubTitle,
|
|
|
|
|
TimeScale,
|
|
|
|
|
TimeSeriesScale,
|
|
|
|
|
Title,
|
|
|
|
|
Tooltip,
|
|
|
|
|
} from 'chart.js';
|
2021-10-27 15:54:12 +08:00
|
|
|
import * as chartjsAdapter from 'chartjs-adapter-date-fns';
|
2022-07-14 13:23:15 +05:30
|
|
|
import annotationPlugin from 'chartjs-plugin-annotation';
|
2023-01-11 14:39:06 +05:30
|
|
|
import { useIsDarkMode } from 'hooks/useDarkMode';
|
2021-09-23 15:43:43 +05:30
|
|
|
import React, { useCallback, useEffect, useRef } from 'react';
|
|
|
|
|
|
2022-03-29 16:05:08 +05:30
|
|
|
import { hasData } from './hasData';
|
2023-01-18 19:40:15 +05:30
|
|
|
import { getAxisLabelColor } from './helpers';
|
2022-03-22 12:10:31 +05:30
|
|
|
import { legend } from './Plugin';
|
2023-01-17 13:30:34 +02:00
|
|
|
import {
|
|
|
|
|
createDragSelectPlugin,
|
|
|
|
|
createDragSelectPluginOptions,
|
|
|
|
|
dragSelectPluginId,
|
|
|
|
|
DragSelectPluginOptions,
|
|
|
|
|
} from './Plugin/DragSelect';
|
2022-03-29 16:05:08 +05:30
|
|
|
import { emptyGraph } from './Plugin/EmptyGraph';
|
2023-01-17 13:30:34 +02:00
|
|
|
import {
|
|
|
|
|
createIntersectionCursorPlugin,
|
|
|
|
|
createIntersectionCursorPluginOptions,
|
|
|
|
|
intersectionCursorPluginId,
|
|
|
|
|
IntersectionCursorPluginOptions,
|
|
|
|
|
} from './Plugin/IntersectionCursor';
|
2022-03-22 12:10:31 +05:30
|
|
|
import { LegendsContainer } from './styles';
|
2022-03-11 13:39:04 +05:30
|
|
|
import { useXAxisTimeUnit } from './xAxisConfig';
|
2022-05-04 19:30:57 +05:30
|
|
|
import { getToolTipValue, getYAxisFormattedValue } from './yAxisConfig';
|
2022-03-15 15:18:33 +05:30
|
|
|
|
2021-09-28 18:20:14 +05:30
|
|
|
Chart.register(
|
|
|
|
|
LineElement,
|
|
|
|
|
PointElement,
|
|
|
|
|
LineController,
|
|
|
|
|
CategoryScale,
|
|
|
|
|
LinearScale,
|
|
|
|
|
TimeScale,
|
|
|
|
|
TimeSeriesScale,
|
|
|
|
|
Decimation,
|
|
|
|
|
Filler,
|
|
|
|
|
Legend,
|
|
|
|
|
Title,
|
|
|
|
|
Tooltip,
|
|
|
|
|
SubTitle,
|
|
|
|
|
BarController,
|
|
|
|
|
BarElement,
|
2022-07-14 13:23:15 +05:30
|
|
|
annotationPlugin,
|
2021-09-28 18:20:14 +05:30
|
|
|
);
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2022-03-22 12:10:31 +05:30
|
|
|
function Graph({
|
2022-03-22 16:22:02 +05:30
|
|
|
animate = true,
|
2021-09-23 15:43:43 +05:30
|
|
|
data,
|
|
|
|
|
type,
|
|
|
|
|
title,
|
|
|
|
|
isStacked,
|
|
|
|
|
onClickHandler,
|
2021-12-10 14:28:31 +05:30
|
|
|
name,
|
2022-03-15 15:18:33 +05:30
|
|
|
yAxisUnit = 'short',
|
2022-03-22 16:22:02 +05:30
|
|
|
forceReRender,
|
2022-07-14 13:23:15 +05:30
|
|
|
staticLine,
|
2022-08-11 11:45:28 +05:30
|
|
|
containerHeight,
|
2023-01-17 13:30:34 +02:00
|
|
|
onDragSelect,
|
|
|
|
|
dragSelectColor,
|
2022-03-22 12:10:31 +05:30
|
|
|
}: GraphProps): JSX.Element {
|
2021-09-23 15:43:43 +05:30
|
|
|
const chartRef = useRef<HTMLCanvasElement>(null);
|
2023-01-11 14:39:06 +05:30
|
|
|
const isDarkMode = useIsDarkMode();
|
|
|
|
|
|
2021-09-28 18:50:10 +05:30
|
|
|
const currentTheme = isDarkMode ? 'dark' : 'light';
|
2022-03-11 13:39:04 +05:30
|
|
|
const xAxisTimeUnit = useXAxisTimeUnit(data); // Computes the relevant time unit for x axis by analyzing the time stamp data
|
2022-03-11 11:20:52 +05:30
|
|
|
|
2021-09-23 15:43:43 +05:30
|
|
|
const lineChartRef = useRef<Chart>();
|
|
|
|
|
const getGridColor = useCallback(() => {
|
|
|
|
|
if (currentTheme === undefined) {
|
|
|
|
|
return 'rgba(231,233,237,0.1)';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentTheme === 'dark') {
|
|
|
|
|
return 'rgba(231,233,237,0.1)';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'rgba(231,233,237,0.8)';
|
|
|
|
|
}, [currentTheme]);
|
|
|
|
|
|
2022-03-28 21:01:57 +05:30
|
|
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
2021-09-23 15:43:43 +05:30
|
|
|
const buildChart = useCallback(() => {
|
|
|
|
|
if (lineChartRef.current !== undefined) {
|
|
|
|
|
lineChartRef.current.destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (chartRef.current !== null) {
|
2023-01-17 13:30:34 +02:00
|
|
|
const options: CustomChartOptions = {
|
2022-03-22 16:22:02 +05:30
|
|
|
animation: {
|
|
|
|
|
duration: animate ? 200 : 0,
|
|
|
|
|
},
|
2021-09-23 15:43:43 +05:30
|
|
|
responsive: true,
|
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
|
interaction: {
|
|
|
|
|
mode: 'index',
|
|
|
|
|
intersect: false,
|
|
|
|
|
},
|
|
|
|
|
plugins: {
|
2022-07-14 13:23:15 +05:30
|
|
|
annotation: staticLine
|
|
|
|
|
? {
|
|
|
|
|
annotations: [
|
|
|
|
|
{
|
|
|
|
|
type: 'line',
|
|
|
|
|
yMin: staticLine.yMin,
|
|
|
|
|
yMax: staticLine.yMax,
|
|
|
|
|
borderColor: staticLine.borderColor,
|
|
|
|
|
borderWidth: staticLine.borderWidth,
|
|
|
|
|
label: {
|
|
|
|
|
content: staticLine.lineText,
|
|
|
|
|
enabled: true,
|
|
|
|
|
font: {
|
|
|
|
|
size: 10,
|
|
|
|
|
},
|
|
|
|
|
borderWidth: 0,
|
|
|
|
|
position: 'start',
|
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
|
color: staticLine.textColor,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2021-09-23 15:43:43 +05:30
|
|
|
title: {
|
2022-03-22 12:10:31 +05:30
|
|
|
display: title !== undefined,
|
2021-09-23 15:43:43 +05:30
|
|
|
text: title,
|
|
|
|
|
},
|
|
|
|
|
legend: {
|
2021-12-10 14:28:31 +05:30
|
|
|
display: false,
|
2021-09-23 15:43:43 +05:30
|
|
|
},
|
2022-03-24 11:44:38 +05:30
|
|
|
tooltip: {
|
|
|
|
|
callbacks: {
|
|
|
|
|
label(context) {
|
|
|
|
|
let label = context.dataset.label || '';
|
|
|
|
|
|
|
|
|
|
if (label) {
|
|
|
|
|
label += ': ';
|
|
|
|
|
}
|
|
|
|
|
if (context.parsed.y !== null) {
|
2022-05-04 19:30:57 +05:30
|
|
|
label += getToolTipValue(context.parsed.y.toString(), yAxisUnit);
|
2022-03-24 11:44:38 +05:30
|
|
|
}
|
|
|
|
|
return label;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-01-17 13:30:34 +02:00
|
|
|
[dragSelectPluginId]: createDragSelectPluginOptions(
|
|
|
|
|
!!onDragSelect,
|
|
|
|
|
onDragSelect,
|
|
|
|
|
dragSelectColor,
|
|
|
|
|
),
|
|
|
|
|
[intersectionCursorPluginId]: createIntersectionCursorPluginOptions(
|
|
|
|
|
!!onDragSelect,
|
|
|
|
|
currentTheme === 'dark' ? 'white' : 'black',
|
|
|
|
|
),
|
2021-09-23 15:43:43 +05:30
|
|
|
},
|
|
|
|
|
layout: {
|
|
|
|
|
padding: 0,
|
|
|
|
|
},
|
|
|
|
|
scales: {
|
|
|
|
|
x: {
|
|
|
|
|
grid: {
|
|
|
|
|
display: true,
|
|
|
|
|
color: getGridColor(),
|
2022-03-29 16:05:08 +05:30
|
|
|
drawTicks: true,
|
2021-09-23 15:43:43 +05:30
|
|
|
},
|
|
|
|
|
adapters: {
|
|
|
|
|
date: chartjsAdapter,
|
|
|
|
|
},
|
2021-10-20 09:24:55 +05:30
|
|
|
time: {
|
2022-03-11 11:20:52 +05:30
|
|
|
unit: xAxisTimeUnit?.unitName || 'minute',
|
|
|
|
|
stepSize: xAxisTimeUnit?.stepSize || 1,
|
|
|
|
|
displayFormats: {
|
2022-03-22 16:22:02 +05:30
|
|
|
millisecond: 'HH:mm:ss',
|
|
|
|
|
second: 'HH:mm:ss',
|
2022-03-11 11:20:52 +05:30
|
|
|
minute: 'HH:mm',
|
|
|
|
|
hour: 'MM/dd HH:mm',
|
|
|
|
|
day: 'MM/dd',
|
|
|
|
|
week: 'MM/dd',
|
|
|
|
|
month: 'yy-MM',
|
|
|
|
|
year: 'yy',
|
|
|
|
|
},
|
2021-10-20 09:24:55 +05:30
|
|
|
},
|
2021-12-10 11:27:45 +05:30
|
|
|
type: 'time',
|
2023-01-18 19:40:15 +05:30
|
|
|
ticks: { color: getAxisLabelColor(currentTheme) },
|
2021-09-23 15:43:43 +05:30
|
|
|
},
|
|
|
|
|
y: {
|
|
|
|
|
display: true,
|
|
|
|
|
grid: {
|
|
|
|
|
display: true,
|
|
|
|
|
color: getGridColor(),
|
|
|
|
|
},
|
2022-03-15 15:18:33 +05:30
|
|
|
ticks: {
|
2023-01-18 19:40:15 +05:30
|
|
|
color: getAxisLabelColor(currentTheme),
|
2022-03-15 15:18:33 +05:30
|
|
|
// Include a dollar sign in the ticks
|
2022-03-24 12:06:57 +05:30
|
|
|
callback(value) {
|
2022-05-04 19:30:57 +05:30
|
|
|
return getYAxisFormattedValue(value.toString(), yAxisUnit);
|
2022-03-15 15:18:33 +05:30
|
|
|
},
|
|
|
|
|
},
|
2021-09-23 15:43:43 +05:30
|
|
|
},
|
|
|
|
|
stacked: {
|
|
|
|
|
display: isStacked === undefined ? false : 'auto',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
elements: {
|
|
|
|
|
line: {
|
|
|
|
|
tension: 0,
|
|
|
|
|
cubicInterpolationMode: 'monotone',
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-10-20 09:24:55 +05:30
|
|
|
onClick: (event, element, chart) => {
|
|
|
|
|
if (onClickHandler) {
|
|
|
|
|
onClickHandler(event, element, chart, data);
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-09-23 15:43:43 +05:30
|
|
|
};
|
2022-07-14 13:23:15 +05:30
|
|
|
|
2022-03-29 16:05:08 +05:30
|
|
|
const chartHasData = hasData(data);
|
|
|
|
|
const chartPlugins = [];
|
2022-06-24 15:00:21 +05:30
|
|
|
|
2023-01-17 13:30:34 +02:00
|
|
|
if (chartHasData) {
|
|
|
|
|
chartPlugins.push(createIntersectionCursorPlugin());
|
|
|
|
|
chartPlugins.push(createDragSelectPlugin());
|
|
|
|
|
} else {
|
|
|
|
|
chartPlugins.push(emptyGraph);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 15:00:21 +05:30
|
|
|
chartPlugins.push(legend(name, data.datasets.length > 3));
|
|
|
|
|
|
2021-09-23 15:43:43 +05:30
|
|
|
lineChartRef.current = new Chart(chartRef.current, {
|
2022-03-22 12:10:31 +05:30
|
|
|
type,
|
|
|
|
|
data,
|
2021-09-23 15:43:43 +05:30
|
|
|
options,
|
2022-03-29 16:05:08 +05:30
|
|
|
plugins: chartPlugins,
|
2021-09-23 15:43:43 +05:30
|
|
|
});
|
|
|
|
|
}
|
2022-03-22 16:22:02 +05:30
|
|
|
}, [
|
|
|
|
|
animate,
|
|
|
|
|
title,
|
|
|
|
|
getGridColor,
|
|
|
|
|
xAxisTimeUnit?.unitName,
|
|
|
|
|
xAxisTimeUnit?.stepSize,
|
|
|
|
|
isStacked,
|
|
|
|
|
type,
|
|
|
|
|
data,
|
|
|
|
|
name,
|
|
|
|
|
yAxisUnit,
|
|
|
|
|
onClickHandler,
|
2022-07-14 13:23:15 +05:30
|
|
|
staticLine,
|
2023-01-17 13:30:34 +02:00
|
|
|
onDragSelect,
|
|
|
|
|
dragSelectColor,
|
|
|
|
|
currentTheme,
|
2022-03-22 16:22:02 +05:30
|
|
|
]);
|
2021-09-23 15:43:43 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
buildChart();
|
2022-03-22 16:22:02 +05:30
|
|
|
}, [buildChart, forceReRender]);
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2021-12-10 14:28:31 +05:30
|
|
|
return (
|
2022-08-11 11:45:28 +05:30
|
|
|
<div style={{ height: containerHeight }}>
|
2021-12-10 14:28:31 +05:30
|
|
|
<canvas ref={chartRef} />
|
|
|
|
|
<LegendsContainer id={name} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2023-01-17 13:30:34 +02:00
|
|
|
type CustomChartOptions = ChartOptions & {
|
|
|
|
|
plugins: {
|
|
|
|
|
[dragSelectPluginId]: DragSelectPluginOptions | false;
|
|
|
|
|
[intersectionCursorPluginId]: IntersectionCursorPluginOptions | false;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-23 15:43:43 +05:30
|
|
|
interface GraphProps {
|
2022-03-22 16:22:02 +05:30
|
|
|
animate?: boolean;
|
2021-09-23 15:43:43 +05:30
|
|
|
type: ChartType;
|
|
|
|
|
data: Chart['data'];
|
|
|
|
|
title?: string;
|
|
|
|
|
isStacked?: boolean;
|
2022-03-24 12:06:57 +05:30
|
|
|
onClickHandler?: GraphOnClickHandler;
|
2021-12-10 14:28:31 +05:30
|
|
|
name: string;
|
2022-03-15 15:18:33 +05:30
|
|
|
yAxisUnit?: string;
|
2022-03-22 16:22:02 +05:30
|
|
|
forceReRender?: boolean | null | number;
|
2022-07-14 13:23:15 +05:30
|
|
|
staticLine?: StaticLineProps | undefined;
|
2022-08-11 11:45:28 +05:30
|
|
|
containerHeight?: string | number;
|
2023-01-17 13:30:34 +02:00
|
|
|
onDragSelect?: (start: number, end: number) => void;
|
|
|
|
|
dragSelectColor?: string;
|
2022-07-14 13:23:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface StaticLineProps {
|
|
|
|
|
yMin: number | undefined;
|
|
|
|
|
yMax: number | undefined;
|
|
|
|
|
borderColor: string;
|
|
|
|
|
borderWidth: number;
|
|
|
|
|
lineText: string;
|
|
|
|
|
textColor: string;
|
2021-09-23 15:43:43 +05:30
|
|
|
}
|
|
|
|
|
|
2022-03-24 12:06:57 +05:30
|
|
|
export type GraphOnClickHandler = (
|
2021-10-20 09:24:55 +05:30
|
|
|
event: ChartEvent,
|
|
|
|
|
elements: ActiveElement[],
|
|
|
|
|
chart: Chart,
|
|
|
|
|
data: ChartData,
|
|
|
|
|
) => void;
|
|
|
|
|
|
2022-03-24 12:06:57 +05:30
|
|
|
Graph.defaultProps = {
|
|
|
|
|
animate: undefined,
|
|
|
|
|
title: undefined,
|
|
|
|
|
isStacked: undefined,
|
|
|
|
|
onClickHandler: undefined,
|
|
|
|
|
yAxisUnit: undefined,
|
|
|
|
|
forceReRender: undefined,
|
2022-07-14 13:23:15 +05:30
|
|
|
staticLine: undefined,
|
2022-08-11 11:45:28 +05:30
|
|
|
containerHeight: '85%',
|
2023-01-17 13:30:34 +02:00
|
|
|
onDragSelect: undefined,
|
|
|
|
|
dragSelectColor: undefined,
|
2022-03-24 12:06:57 +05:30
|
|
|
};
|
2021-09-23 15:43:43 +05:30
|
|
|
export default Graph;
|