Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 373x 373x 373x 373x 373x 373x 373x 79x 9x 70x 24x 46x 79x 24x 55x 51x 373x 373x 373x 79x 373x 373x 373x 373x 373x 373x 79x 79x 79x 373x 373x 373x 32x 19x 373x 373x 373x 373x 373x 373x 373x 11x 11x 11x 11x 11x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 4x 4x 3x 3x 1x 19x 19x 11x 373x 373x 15x 15x 15x 373x 15x 358x 373x 14x 359x 373x 358x 358x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x 373x | // Stryker disable all
import { useEffect, useMemo, useState } from "react";
import { Form } from "react-bootstrap";
import {
CartesianGrid,
Legend,
Line,
LineChart,
Tooltip,
XAxis,
YAxis,
} from "recharts";
import {
expandRangeWhenEqual,
formatPercentageForTick,
formatTimestampForTick,
getGlobalDateRange,
getGlobalValueRange,
hasPercentageSeries,
normalizeSeriesData,
} from "./timeSeriesUtils";
const DEFAULT_Y_AXIS_ID = "default";
const PERCENTAGE_Y_AXIS_ID = "percentage";
const getSelectorId = (testid, name) =>
`${testid}-selector-${name.toLowerCase().replace(/\s+/g, "-")}`;
export default function TimeSeries({
data,
selectors = [],
width = 600,
height = 400,
testid = "time-series",
}) {
const selectorNames = useMemo(() => {
const selectorOptions = Array.isArray(selectors)
? selectors
: selectors === "all"
? ["all"]
: [selectors];
return selectorOptions.includes("all")
? data.map((series) => series.name)
: selectorOptions.filter((selector) =>
data.some((series) => series.name === selector),
);
}, [data, selectors]);
const [selectedSeriesNames, setSelectedSeriesNames] = useState(selectorNames);
useEffect(() => {
setSelectedSeriesNames(selectorNames);
}, [selectorNames]);
const seriesByName = useMemo(
() => new Map(data.map((series) => [series.name, series])),
[data],
);
const selectorSeries = useMemo(
() =>
selectorNames
.map((selectorName) => seriesByName.get(selectorName))
.filter(Boolean),
[selectorNames, seriesByName],
);
const visibleData = data.filter(
(series) =>
!selectorNames.includes(series.name) ||
selectedSeriesNames.includes(series.name),
);
const normalizedData = normalizeSeriesData(visibleData);
const showPercentageAxis = hasPercentageSeries(visibleData);
const { minDate, maxDate } = getGlobalDateRange(visibleData);
const { minValue, maxValue } = getGlobalValueRange(visibleData);
const hasStandardScale = minValue !== null && maxValue !== null;
const selectorControls =
selectorSeries.length > 0 ? (
<div
className="d-flex justify-content-center flex-wrap gap-3 mt-3"
aria-label="Time series selectors"
data-testid={`${testid}-selectors`}
>
{selectorSeries.map(({ name, color }) => {
const selectorId = getSelectorId(testid, name);
const isSelected = selectedSeriesNames.includes(name);
return (
<div
key={name}
className="mb-0"
data-testid={`${selectorId}-wrapper`}
>
<Form.Check
inline
id={selectorId}
type="checkbox"
label={<span style={{ color }}>{name}</span>}
checked={isSelected}
onChange={() =>
setSelectedSeriesNames((currentSelection) =>
currentSelection.includes(name)
? currentSelection.filter(
(selectorName) => selectorName !== name,
)
: [...currentSelection, name],
)
}
style={{ accentColor: color }}
/>
</div>
);
})}
</div>
) : null;
const isEmpty =
normalizedData.length === 0 ||
minDate === null ||
maxDate === null ||
(!hasStandardScale && !showPercentageAxis);
const [xMin, xMax] = !isEmpty
? expandRangeWhenEqual(minDate, maxDate)
: [null, null];
const [yMin, yMax] = hasStandardScale
? expandRangeWhenEqual(minValue, maxValue)
: [null, null];
const chartOrEmpty = isEmpty ? (
<div data-testid={`${testid}-empty`}>
<p>No data to display</p>
</div>
) : (
<LineChart
width={width}
height={height}
margin={{ top: 20, right: 30, left: 20, bottom: 20 }}
data-testid={testid}
>
<CartesianGrid strokeDasharray="3 3" />
<XAxis
type="number"
dataKey="dateMs"
domain={[xMin, xMax]}
tickFormatter={formatTimestampForTick}
/>
{hasStandardScale && (
<YAxis
yAxisId={DEFAULT_Y_AXIS_ID}
type="number"
domain={[yMin, yMax]}
/>
)}
{showPercentageAxis && (
<YAxis
yAxisId={PERCENTAGE_Y_AXIS_ID}
type="number"
orientation="right"
domain={[0, 100]}
ticks={[0, 25, 50, 75, 100]}
tickFormatter={formatPercentageForTick}
allowDecimals={false}
/>
)}
<Tooltip labelFormatter={formatTimestampForTick} />
<Legend />
{normalizedData.map((series) => (
<Line
key={series.name}
name={series.name}
data={series.values}
yAxisId={series.percentage ? PERCENTAGE_Y_AXIS_ID : DEFAULT_Y_AXIS_ID}
dataKey="value"
stroke={series.color}
dot={false}
isAnimationActive={false}
/>
))}
</LineChart>
);
return (
<>
{chartOrEmpty}
{selectorControls}
</>
);
}
|