@netdata/charts
Version:
Netdata frontend SDK and chart utilities
196 lines • 11.1 kB
JavaScript
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import { useMemo } from "react";
import html2canvas from "html2canvas";
import jsPDF from "jspdf";
var formatValue = function formatValue(value) {
if (value === null || value === undefined) return "-";
if (_typeof(value) === "object") return JSON.stringify(value);
return value;
};
var convertToCSV = function convertToCSV(data) {
return data.reduce(function (h, row) {
return h + row.map(formatValue).join(",") + "\n";
}, "");
};
var labelTexts = {
time: "timestamp",
ANOMALY_RATE: "anomaly%",
ANNOTATIONS: "annotations"
};
var hideUnits = {
time: true,
ANOMALY_RATE: true,
ANNOTATIONS: true
};
export var useDownloadCsv = function useDownloadCsv(chart) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "raw";
return function () {
var shouldConvert = type === "converted";
var _chart$getPayload = chart.getPayload(),
data = _chart$getPayload.data,
labels = _chart$getPayload.labels;
var tableData = [labels.map(function (l) {
return hideUnits[l] ? labelTexts[l] || chart.getDimensionName(l) : "".concat(labelTexts[l] || chart.getDimensionName(l), " (").concat(chart.getUnitSign({
key: "units",
dimensionId: l,
withoutConversion: !shouldConvert
}), ")");
})];
data.forEach(function (row) {
tableData.push(labels.map(function (l, index) {
return shouldConvert ? index === 0 ? chart.formatTime(row[index]) : chart.getConvertedValue(row[index], {
dimensionId: l
}) : row[index];
}));
});
var url = window.URL.createObjectURL(new Blob([convertToCSV(tableData)], {
type: "text/csv;charset=utf-8;"
}));
var link = document.createElement("a");
link.href = url;
var filename = "".concat(chart.getAttribute("name") || chart.getAttribute("contextScope").join("-").replace(".", "_"), "-").concat(type, ".csv");
link.setAttribute("download", filename);
document.body.appendChild(link);
link.click();
link.remove();
};
};
export var useDownloadImage = function useDownloadImage(chart) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
uiName = _ref.uiName,
_ref$type = _ref.type,
type = _ref$type === void 0 ? "pdf" : _ref$type;
return /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
var element, rect, filename, canvas, imgData, blob, blobUrl, link, isLandscape, pdf, pageWidth, pageHeight, imgWidth, imgHeight, pxToMm, imgWidthMm, imgHeightMm, scale, displayWidth, displayHeight, _t;
return _regenerator().w(function (_context) {
while (1) switch (_context.p = _context.n) {
case 0:
element = chart.getUI(uiName).getElement();
if (element.closest("[data-type='chart']")) {
element = element.closest("[data-type='chart']");
}
if (element) {
_context.n = 1;
break;
}
return _context.a(2);
case 1:
rect = element.getBoundingClientRect();
if (!(rect.width === 0 || rect.height === 0)) {
_context.n = 2;
break;
}
return _context.a(2);
case 2:
document.querySelectorAll("img").forEach(function (img) {
var src = img.currentSrc || img.src;
try {
var url = new URL(src);
if (url.origin !== window.location.origin) return;
} catch (_unused) {
return;
}
});
filename = "".concat(chart.getAttribute("name") || chart.getAttribute("contextScope").join("-").replace(".", "_"));
_context.p = 3;
_context.n = 4;
return html2canvas(element, {
useCORS: true,
foreignObjectRendering: true,
width: element.scrollWidth,
height: element.scrollHeight,
scale: 2,
backgroundColor: chart.getThemeAttribute("themeBackground"),
ignoreElements: function ignoreElements(el) {
return el.hasAttribute("data-noprint");
}
});
case 4:
canvas = _context.v;
imgData = canvas.toDataURL("image/png");
if (!(type === "png")) {
_context.n = 7;
break;
}
_context.n = 5;
return fetch(imgData);
case 5:
_context.n = 6;
return _context.v.blob();
case 6:
blob = _context.v;
blobUrl = URL.createObjectURL(blob);
link = document.createElement("a");
link.href = blobUrl;
link.download = "".concat(filename || "print", ".png");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(blobUrl);
_context.n = 8;
break;
case 7:
isLandscape = element.scrollWidth > element.scrollHeight;
pdf = new jsPDF({
orientation: isLandscape ? "landscape" : "portrait",
unit: "mm",
format: "a4"
});
pageWidth = pdf.internal.pageSize.getWidth();
pageHeight = pdf.internal.pageSize.getHeight();
imgWidth = canvas.width;
imgHeight = canvas.height;
pxToMm = function pxToMm(px) {
return px * 0.2645833333;
};
imgWidthMm = pxToMm(imgWidth);
imgHeightMm = pxToMm(imgHeight);
scale = Math.min(pageWidth / imgWidthMm, pageHeight / imgHeightMm);
displayWidth = imgWidthMm * scale;
displayHeight = imgHeightMm * scale;
pdf.addImage(imgData, "PNG", 0, 0, displayWidth, displayHeight);
pdf.save("".concat(filename || "print", ".pdf"));
case 8:
_context.n = 10;
break;
case 9:
_context.p = 9;
_t = _context.v;
return _context.a(2);
case 10:
return _context.a(2);
}
}, _callee, null, [[3, 9]]);
}));
};
export var useDownloadItems = function useDownloadItems(chart) {
var downloadRawCsv = useDownloadCsv(chart);
var downloadCsv = useDownloadCsv(chart, "converted");
var downloadPdf = useDownloadImage(chart);
var downloadPng = useDownloadImage(chart, {
type: "png"
});
return useMemo(function () {
return [{
onClick: downloadCsv,
label: "Download as CSV",
"data-track": chart.track("download-csv")
}, {
onClick: downloadRawCsv,
label: "Download raw data",
"data-track": chart.track("download-raw-csv")
}, {
onClick: downloadPdf,
label: "Download as PDF",
"data-track": chart.track("download-pdf")
}, {
onClick: downloadPng,
label: "Download as PNG",
"data-track": chart.track("download-png")
}];
}, [chart, chart.getHeatmapType()]);
};