2020-10-27 07:53:04 +01:00
|
|
|
import { LineChart, XAxis, YAxis, Line, Tooltip, Legend } from "recharts";
|
|
|
|
import { timeFormat } from "d3-time-format";
|
|
|
|
|
2020-10-28 08:53:24 +01:00
|
|
|
interface ToolTipProps {
|
|
|
|
active?: boolean,
|
|
|
|
payload?: object,
|
2020-10-29 20:39:59 +01:00
|
|
|
unit?: string
|
2020-11-01 07:17:44 +01:00
|
|
|
}
|
|
|
|
|
2020-10-28 08:53:24 +01:00
|
|
|
const defaultProps = {
|
|
|
|
active: false,
|
2020-11-01 08:01:37 +01:00
|
|
|
payload: Object,
|
|
|
|
unit: "",
|
2020-10-28 08:53:24 +01:00
|
|
|
};
|
|
|
|
|
2020-11-01 08:01:37 +01:00
|
|
|
interface TimedValue {
|
|
|
|
time: Date;
|
|
|
|
value: Number;
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:53:24 +01:00
|
|
|
interface ChartProps {
|
2020-11-01 08:01:37 +01:00
|
|
|
// eslint-disable-next-line react/require-default-props
|
|
|
|
data?: TimedValue[],
|
2020-10-28 08:53:24 +01:00
|
|
|
color: string,
|
|
|
|
unit: string,
|
2020-11-01 08:01:37 +01:00
|
|
|
// eslint-disable-next-line react/require-default-props
|
|
|
|
dataCollections?: any[],
|
2020-10-28 08:53:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function CustomizedTooltip(props: ToolTipProps) {
|
2020-10-29 02:36:25 +01:00
|
|
|
const { active, payload, unit } = props;
|
2020-10-28 08:53:24 +01:00
|
|
|
if (active && payload && payload[0]) {
|
2020-10-29 02:36:25 +01:00
|
|
|
const time = payload[0].payload ? timeFormat("%I:%M")(new Date(payload[0].payload.time)) : "";
|
2020-10-28 08:53:24 +01:00
|
|
|
return (
|
|
|
|
<div className="custom-tooltip">
|
|
|
|
<p className="label">
|
2020-10-29 02:36:25 +01:00
|
|
|
<strong>{time}</strong> {payload[0].payload.value} {unit}
|
2020-10-28 08:53:24 +01:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
CustomizedTooltip.defaultProps = defaultProps;
|
2020-10-27 07:53:04 +01:00
|
|
|
|
2020-10-29 03:28:52 +01:00
|
|
|
export default function Chart({ data, color, unit, dataCollections }: ChartProps) {
|
2020-11-01 08:01:37 +01:00
|
|
|
if (!data && !dataCollections) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:53:24 +01:00
|
|
|
const timeFormatter = (tick: string) => {
|
2020-10-29 02:36:25 +01:00
|
|
|
return timeFormat("%I:%M")(new Date(tick));
|
2020-10-27 07:53:04 +01:00
|
|
|
};
|
|
|
|
|
2020-11-01 08:01:37 +01:00
|
|
|
let ticks
|
|
|
|
if (dataCollections.length > 0) {
|
|
|
|
ticks = dataCollections[0].data?.map(function (collection) {
|
|
|
|
return collection?.time;
|
|
|
|
})
|
|
|
|
} else if (data?.length > 0){
|
|
|
|
ticks = data?.map(function (item) {
|
|
|
|
return item?.time;
|
|
|
|
});
|
2020-10-29 03:28:52 +01:00
|
|
|
}
|
2020-11-01 07:17:44 +01:00
|
|
|
|
2020-10-27 07:53:04 +01:00
|
|
|
return (
|
|
|
|
<LineChart width={1200} height={400} data={data}>
|
|
|
|
<XAxis
|
|
|
|
dataKey="time"
|
|
|
|
tickFormatter={timeFormatter}
|
|
|
|
interval="preserveStartEnd"
|
|
|
|
tickCount={5}
|
|
|
|
minTickGap={15}
|
|
|
|
domain={["dataMin", "dataMax"]}
|
2020-10-29 03:28:52 +01:00
|
|
|
ticks={ticks}
|
2020-10-27 07:53:04 +01:00
|
|
|
/>
|
|
|
|
<YAxis
|
|
|
|
dataKey="value"
|
|
|
|
interval="preserveStartEnd"
|
|
|
|
unit={unit}
|
|
|
|
domain={["dataMin", "dataMax"]}
|
|
|
|
/>
|
2020-10-29 02:36:25 +01:00
|
|
|
<Tooltip content={<CustomizedTooltip unit={unit} />} />
|
2020-10-27 07:53:04 +01:00
|
|
|
<Legend />
|
|
|
|
<Line
|
|
|
|
type="monotone"
|
|
|
|
dataKey="value"
|
|
|
|
stroke={color}
|
|
|
|
dot={null}
|
|
|
|
strokeWidth={3}
|
|
|
|
/>
|
2020-10-29 03:28:52 +01:00
|
|
|
{dataCollections?.map((s) => (
|
|
|
|
<Line
|
|
|
|
dataKey="value"
|
|
|
|
data={s.data}
|
|
|
|
name={s.name}
|
|
|
|
key={s.name}
|
|
|
|
type="monotone"
|
|
|
|
stroke={s.color}
|
|
|
|
dot={null}
|
|
|
|
strokeWidth={3}
|
|
|
|
/>
|
|
|
|
))}
|
2020-10-27 07:53:04 +01:00
|
|
|
</LineChart>
|
|
|
|
);
|
|
|
|
}
|
2020-11-01 07:17:44 +01:00
|
|
|
|
|
|
|
Chart.defaultProps = {
|
|
|
|
dataCollections: [],
|
|
|
|
};
|