1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/static/js/charts.js
catborise 7408a575d6 fix(ui): sizes in the units the forms ask in, percentages as the language writes them
- binary_size filter (common_tags): every size on the pages is B/KiB/MiB/GiB/TiB
  with the language's decimal mark; Django's filesizeformat said GB for GiB, so a
  page showed "512,0 MB / 3,8 GB" beside "0,4 / 3,8 GiB" and a disk "20,0 GB"
  next to a GiB field
- percent filter and Intl percent in charts.js, the job panel and the new-VM
  dialog: 13% in English, %13 in Turkish (pages had "13 %", "%11" and "13%")
- the Jobs page and panel say in whole sentences that the job worker is not
  running and that jobs wait for it, instead of a sentence glued to a date
- job state badges start with a capital, as the VM state badges (CSS, so the
  label stays lowercase inside sentences)
- profile card shows the username under the name only when there is a full name
- Turkish: hostname, revert snapshot, snapshot count, API token and worker texts
2026-11-22 21:03:46 +03:00

122 lines
5.4 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;
// a percentage as the language writes it: 13% in English, %13 in Turkish
function formatPercent(value) {
return new Intl.NumberFormat(PAGE_LANGUAGE, {style: "percent", maximumFractionDigits: 1}).format(value / 100);
}
(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 || {};
// a reading with its unit: a percentage as the language writes it
function withUnit(value, text) {
return unit === "%" ? formatPercent(value) : text + " " + unit;
}
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 (value) {
return withUnit(value, Chart.Ticks.formatters.numeric.apply(this, arguments));
}}},
},
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 + ": " + withUnit(context.parsed.y, context.formattedValue);
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();
}