evcc-io/assets/js/mixins/formatter.js
Michael Geers 89ac4e2830
Some Visualisation Improvements (#1371)
* removed setup animation; added kW to legend on larger screens; readded site title default

* grid feed-in > grid export

* rounded brackets; kW labels for smaller bar-parts
2021-08-18 21:48:04 +02:00

109 lines
3.1 KiB
JavaScript

export default {
data: function () {
return {
fmtLimit: 100,
fmtDigits: 1,
};
},
methods: {
round: function (num, precision) {
var base = 10 ** precision;
return (Math.round(num * base) / base).toFixed(precision);
},
fmt: function (val) {
if (val === undefined || val === null) {
return 0;
}
val = Math.abs(val);
return val >= this.fmtLimit ? this.round(val / 1e3, this.fmtDigits) : this.round(val, 0);
},
fmtKw: function (watt, kw = true, withUnit = true) {
const digits = kw ? 1 : 0;
const value = kw ? watt / 1000 : watt;
let unit = "";
if (withUnit) {
unit = kw ? " kW" : " W";
}
return (
this.$n(value, { minimumFractionDigits: digits, maximumFractionDigits: digits }) + unit
);
},
fmtUnit: function (val) {
return Math.abs(val) >= this.fmtLimit ? "k" : "";
},
fmtDuration: function (d) {
if (d <= 0 || d == null) {
return "—";
}
var seconds = "0" + (d % 60);
var minutes = "0" + (Math.floor(d / 60) % 60);
var hours = "" + Math.floor(d / 3600);
if (hours.length < 2) {
hours = "0" + hours;
}
return hours + ":" + minutes.substr(-2) + ":" + seconds.substr(-2);
},
fmtShortDuration: function (d) {
if (d <= 0 || d == null) {
return "—";
}
var minutes = Math.floor(d / 60) % 60;
var hours = Math.floor(d / 3600);
var tm;
if (hours >= 1) {
minutes = "0" + minutes;
tm = hours + ":" + minutes.substr(-2);
} else {
var seconds = "0" + (d % 60);
tm = minutes + ":" + seconds.substr(-2);
}
return tm;
},
fmtShortDurationUnit: function (d) {
if (d <= 0 || d == null) {
return "";
}
var hours = Math.floor(d / 3600);
if (hours >= 1) {
return "h";
}
return "m";
},
fmtDayString: function (date) {
const YY = `${date.getFullYear()}`;
const MM = `${date.getMonth() + 1}`.padStart(2, "0");
const DD = `${date.getDate()}`.padStart(2, "0");
return `${YY}-${MM}-${DD}`;
},
fmtTimeString: function (date) {
const HH = `${date.getHours()}`.padStart(2, "0");
const mm = `${date.getMinutes()}`.padStart(2, "0");
return `${HH}:${mm}`;
},
fmtAbsoluteDate: function (date) {
return new Intl.DateTimeFormat("default", {
weekday: "short",
hour: "numeric",
minute: "numeric",
}).format(date);
},
fmtTimeAgo: function (date) {
const units = {
year: 24 * 60 * 60 * 1000 * 365,
month: (24 * 60 * 60 * 1000 * 365) / 12,
day: 24 * 60 * 60 * 1000,
hour: 60 * 60 * 1000,
minute: 60 * 1000,
second: 1000,
};
const rtf = new Intl.RelativeTimeFormat("default", { numeric: "auto" });
const elapsed = date - new Date();
// "Math.abs" accounts for both "past" & "future" scenarios
for (var u in units)
if (Math.abs(elapsed) > units[u] || u == "second")
return rtf.format(Math.round(elapsed / units[u]), u);
},
},
};