1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00
webvirtcloud/static/js/charts.js
catborise a2da2ab394 fix(ui): a host's pages on a phone: one row of sections, tables that fit
On a phone the host's section menu wrapped to three lines, each new line
starting with a divider. It is now one row that scrolls sideways, with the
current section in sight (scroll_rows.js, which also takes over the VM page's
tab rows from instance_page.js).

The filter list, a filter's rules and the PCI and USB device tables were wider
than a phone and hid their actions and users off screen; on a phone they now
keep the name, the key column and the actions, as the other lists do.

The overview's live CPU and memory figures, the chart axes and tooltips, and
the VM page's stats now write numbers as the page's language does (a decimal
comma in Turkish), like the cards beside them.

The capacity cards said "0 bytes running"; they now say what they count:
"0 bytes in running VMs".

A pool's type is written as libvirt names it on both the list and its page,
and the device search sits where the interfaces search does.
2026-11-22 14:59:17 +03:00

112 lines
5 KiB
JavaScript

// 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. 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;
// a reading as the page's language writes it (a decimal comma in Turkish), one decimal at most;
// no thousands separator, as the panel's own pages
var PAGE_LANGUAGE = document.documentElement.lang || undefined;
var formatNumber = new Intl.NumberFormat(PAGE_LANGUAGE, {maximumFractionDigits: 1, useGrouping: false}).format;
(function () {
var style = getComputedStyle(document.body);
Chart.defaults.color = style.getPropertyValue("--bs-body-color").trim();
Chart.defaults.borderColor = style.getPropertyValue("--bs-border-color").trim();
Chart.defaults.locale = PAGE_LANGUAGE; // the tooltips' numbers
})();
// labels: one per line. options: max (the y axis top), tooltip(context, text)
// to add to a point's tooltip text.
function lineChart(canvas, unit, labels, options) {
var style = getComputedStyle(document.body);
var colours = ["--bs-primary-rgb", "--bs-info-rgb"].map(function (name) {
return style.getPropertyValue(name).trim();
});
options = options || {};
return new Chart(canvas, {
type: "line",
data: {
labels: [],
datasets: labels.map(function (label, i) {
return {
label: label,
data: [],
borderColor: "rgb(" + colours[i] + ")",
backgroundColor: "rgba(" + colours[i] + ", 0.3)",
fill: true,
pointRadius: 2,
};
}),
},
options: {
animation: false,
maintainAspectRatio: false,
scales: {
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);
},
},
},
// the axis as Chart.js writes it (its precision, the page language), with the unit
y: {min: 0, max: options.max, ticks: {callback: function () { return Chart.Ticks.formatters.numeric.apply(this, arguments) + " " + 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;
},
},
},
},
},
});
}
// 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({x: x, y: values[i] == null ? null : values[i]});
});
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();
}