1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

fix(computes): a chart places each reading at the time it came

The CPU, memory, disk and network charts of a host and of a VM put their
readings at equal steps: a reading that took 20 s looked like one 5 s
after the one before. The x axis now counts seconds, a reading standing
at the time it came; each tick is at a reading and named with the
panel's time of it, as before. Checked with Chart.js in a browser: a
20 s step is four times as wide as a 5 s one.
This commit is contained in:
catborise 2026-11-13 16:17:42 +03:00
parent be4616ee85
commit 275bf11aed
3 changed files with 93 additions and 13 deletions

View file

@ -1,7 +1,11 @@
// Live line charts of the host overview and the VM page (Chart.js 4): the
// last CHART_POINTS readings, a gap where a reading is missing, and the
// colours of the selected Bootstrap theme.
// colours of the selected Bootstrap theme. A reading is placed at the time
// it came: one that took longer stands farther from the one before. Each
// is labelled with the time the panel gave it.
var CHART_POINTS = 10;
// the pixels a tick's label needs from the next one
var TICK_ROOM = 60;
(function () {
var style = getComputedStyle(document.body);
@ -36,13 +40,33 @@ function lineChart(canvas, unit, labels, options) {
animation: false,
maintainAspectRatio: false,
scales: {
x: {ticks: {maxTicksLimit: CHART_POINTS, maxRotation: 0}},
x: {
type: "linear",
// a tick at each reading, named by its label
afterBuildTicks: function (axis) {
axis.ticks = axis.chart.data.datasets[0].data.map(function (point) { return {value: point.x}; });
},
// a reading's label, unless the next one stands too close for it to be read (a
// long step, after the page was hidden, leaves the others packed together)
ticks: {
maxRotation: 0,
autoSkip: false,
callback: function (value, index, ticks) {
var next = ticks[index + 1];
var room = next && this.getPixelForValue(next.value) - this.getPixelForValue(value);
return next && room < TICK_ROOM ? "" : labelAt(this.chart, value);
},
},
},
y: {min: 0, max: options.max, ticks: {callback: function (value) { return value + " " + unit; }}},
},
plugins: {
legend: {display: labels.length > 1},
tooltip: {
callbacks: {
title: function (items) {
return items.length ? labelAt(items[0].chart, items[0].parsed.x) : "";
},
label: function (context) {
var text = context.dataset.label + ": " + context.formattedValue + " " + unit;
return options.tooltip ? options.tooltip(context, text) : text;
@ -54,17 +78,29 @@ function lineChart(canvas, unit, labels, options) {
});
}
// values: one per line, null for a reading that is missing
function addPoint(chart, label, values) {
chart.data.labels.push(label);
// the label of the reading at x (seconds) of chart: the panel's time of it
function labelAt(chart, x) {
return (chart.timeLabels || {})[x] || "";
}
// values: one per line, null for a reading that is missing; at, the
// reading's time in seconds, now if not given
function addPoint(chart, label, values, at) {
var x = at === undefined ? Date.now() / 1000 : at;
chart.timeLabels = chart.timeLabels || {}; // a reading's x: its label
chart.timeLabels[x] = label;
chart.data.datasets.forEach(function (set, i) {
set.data.push(values[i] == null ? null : values[i]);
set.data.push({x: x, y: values[i] == null ? null : values[i]});
});
if (chart.data.labels.length > CHART_POINTS) {
chart.data.labels.shift();
if (chart.data.datasets[0].data.length > CHART_POINTS) {
var gone = chart.data.datasets[0].data[0].x;
chart.data.datasets.forEach(function (set) {
set.data.shift();
});
// the label goes with its last reading: two may have come at one time
if (!chart.data.datasets[0].data.some(function (point) { return point.x === gone; })) {
delete chart.timeLabels[gone];
}
}
chart.update();
}