owncast/web/pages/components/chart.tsx

117 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-10-27 07:53:04 +01:00
import { LineChart, XAxis, YAxis, Line, Tooltip, Legend } from "recharts";
import { timeFormat } from "d3-time-format";
import useWindowSize from '../../utils/hook-windowresize';
import styles from '../../styles/styles.module.css';
2020-10-27 07:53:04 +01:00
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-11-01 08:01:37 +01:00
}
2020-10-28 08:53:24 +01:00
interface ChartProps {
2020-11-01 08:01:37 +01:00
data?: TimedValue[],
2020-10-28 08:53:24 +01:00
color: string,
unit: string,
2020-11-01 08:01:37 +01:00
dataCollections?: any[],
2020-10-28 08:53:24 +01:00
}
function CustomizedTooltip(props: ToolTipProps) {
const { active, payload, unit } = props;
2020-10-28 08:53:24 +01:00
if (active && payload && payload[0]) {
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">
<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
export default function Chart({ data, color, unit, dataCollections }: ChartProps) {
2020-11-01 08:01:37 +01:00
if (!data && !dataCollections) {
return null;
}
const windowSize = useWindowSize();
const chartWidth = windowSize.width * .68;
const chartHeight = chartWidth * .333;
2020-11-01 08:01:37 +01:00
2020-10-28 08:53:24 +01:00
const timeFormatter = (tick: string) => {
return timeFormat("%I:%M")(new Date(tick));
2020-10-27 07:53:04 +01:00
};
let ticks = [];
2020-11-01 08:01:37 +01:00
if (dataCollections.length > 0) {
ticks = dataCollections[0].data?.map((collection) => {
2020-11-01 08:01:37 +01:00
return collection?.time;
})
} else if (data?.length > 0){
ticks = data?.map(item => {
2020-11-01 08:01:37 +01:00
return item?.time;
});
}
2020-11-01 07:17:44 +01:00
2020-10-27 07:53:04 +01:00
return (
<div className={styles.lineChartContainer}>
<LineChart width={chartWidth} height={chartHeight} data={data}>
<XAxis
dataKey="time"
tickFormatter={timeFormatter}
interval="preserveStartEnd"
tickCount={5}
minTickGap={15}
domain={["dataMin", "dataMax"]}
ticks={ticks}
/>
<YAxis
dataKey="value"
interval="preserveStartEnd"
unit={unit}
domain={["dataMin", "dataMax"]}
/>
<Tooltip content={<CustomizedTooltip unit={unit} />} />
<Legend />
<Line
type="monotone"
dataKey="value"
stroke={color}
dot={null}
strokeWidth={3}
/>
{dataCollections?.map((s) => (
<Line
dataKey="value"
data={s.data}
name={s.name}
key={s.name}
type="monotone"
stroke={s.color}
dot={null}
strokeWidth={3}
/>
))}
</LineChart>
</div>
2020-10-27 07:53:04 +01:00
);
}
2020-11-01 07:17:44 +01:00
Chart.defaultProps = {
dataCollections: [],
};