Skip to content

Commit 751236b

Browse files
committed
fix(monitors): table trend chart x axis ticks overlap
1 parent 74a3655 commit 751236b

1 file changed

Lines changed: 44 additions & 5 deletions

File tree

testgen/ui/components/frontend/js/pages/table_monitoring_trends.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,50 @@ const TableMonitoringTrend = (props) => {
194194

195195
const rawTimeline = [...new Set(allTimes)].sort();
196196
const dateRange = { min: rawTimeline[0] ?? (new Date()).getTime(), max: rawTimeline[rawTimeline.length - 1] ?? (new Date()).getTime() + 1 * 24 * 60 * 60 * 1000 };
197-
const timeline = ([
198-
new Date(dateRange.min),
199-
...getAdaptiveTimeTicksV2(rawTimeline.slice(2, rawTimeline.length - 2).map(time => new Date(time)), chartsWidth, tickWidth),
200-
new Date(dateRange.max),
201-
]).filter((t) => !!t);
197+
const toPixelX = (date) => scale(date.getTime(), { old: dateRange, new: { min: origin.x, max: end.x } }, origin.x);
198+
const xTickMinSpacing = 65;
199+
const timeline = (() => {
200+
const adaptiveTicks = getAdaptiveTimeTicksV2(
201+
rawTimeline.map(time => new Date(time)),
202+
end.x - origin.x,
203+
xTickMinSpacing,
204+
);
205+
206+
const seen = new Set();
207+
const candidates = [];
208+
for (const date of [new Date(dateRange.min), ...adaptiveTicks, new Date(dateRange.max)]) {
209+
if (!date) continue;
210+
const t = date.getTime();
211+
if (!seen.has(t)) {
212+
seen.add(t);
213+
candidates.push(date);
214+
}
215+
}
216+
candidates.sort((a, b) => a.getTime() - b.getTime());
217+
218+
if (candidates.length <= 2) return candidates;
219+
220+
const first = candidates[0];
221+
const last = candidates[candidates.length - 1];
222+
const firstPx = toPixelX(first);
223+
const lastPx = toPixelX(last);
224+
225+
if (lastPx - firstPx < xTickMinSpacing) return [first];
226+
227+
const result = [first];
228+
let prevPx = firstPx;
229+
230+
for (let i = 1; i < candidates.length - 1; i++) {
231+
const px = toPixelX(candidates[i]);
232+
if (px - prevPx >= xTickMinSpacing && lastPx - px >= xTickMinSpacing) {
233+
result.push(candidates[i]);
234+
prevPx = px;
235+
}
236+
}
237+
238+
result.push(last);
239+
return result;
240+
})();
202241

203242
const parsedFreshnessEvents = freshnessEvents.map((e) => ({
204243
changed: e.changed,

0 commit comments

Comments
 (0)