@netdata/charts
Version:
Netdata frontend SDK and chart utilities
101 lines (100 loc) • 5.62 kB
JavaScript
;
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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSparklinePoints = exports.drawSparkline = exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _netdataUi = require("@netdata/netdata-ui");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var defaultWidth = 120;
var defaultHeight = 24;
var linePadding = 4;
var getSparklinePoints = exports.getSparklinePoints = function getSparklinePoints(values, width, height) {
var finiteValues = values.filter(Number.isFinite);
if (!finiteValues.length) return [];
var min = Math.min.apply(Math, _toConsumableArray(finiteValues));
var max = Math.max.apply(Math, _toConsumableArray(finiteValues));
var range = max - min;
var xRange = Math.max(width - linePadding * 2, 1);
var yRange = Math.max(height - linePadding * 2, 1);
var divisor = Math.max(values.length - 1, 1);
return values.map(function (value, index) {
if (!Number.isFinite(value)) return null;
return {
x: linePadding + index / divisor * xRange,
y: range ? linePadding + (max - value) / range * yRange : height / 2
};
});
};
var drawSparkline = exports.drawSparkline = function drawSparkline(context, values, width, height, color) {
var points = getSparklinePoints(values, width, height);
context.clearRect(0, 0, width, height);
if (!points.length) return;
context.beginPath();
var drawing = false;
points.forEach(function (point) {
if (!point) {
drawing = false;
return;
}
if (drawing) context.lineTo(point.x, point.y);else context.moveTo(point.x, point.y);
drawing = true;
});
context.strokeStyle = color;
context.lineWidth = 1;
context.lineJoin = "round";
context.lineCap = "round";
context.stroke();
};
var SparklineCanvas = function SparklineCanvas(_ref) {
var values = _ref.values,
color = _ref.color,
_ref$height = _ref.height,
height = _ref$height === void 0 ? defaultHeight : _ref$height;
var canvasRef = (0, _react.useRef)(null);
var frameRef = (0, _react.useRef)(null);
var draw = (0, _react.useCallback)(function () {
var canvas = canvasRef.current;
if (!canvas) return;
var width = Math.max(Math.round(canvas.getBoundingClientRect().width) || defaultWidth, 1);
var ratio = window.devicePixelRatio || 1;
var pixelWidth = Math.round(width * ratio);
var pixelHeight = Math.round(height * ratio);
if (canvas.width !== pixelWidth) canvas.width = pixelWidth;
if (canvas.height !== pixelHeight) canvas.height = pixelHeight;
var context = canvas.getContext("2d");
if (!context) return;
context.setTransform(ratio, 0, 0, ratio, 0, 0);
drawSparkline(context, values, width, height, color);
}, [color, height, values]);
(0, _react.useLayoutEffect)(function () {
draw();
if (typeof ResizeObserver === "undefined") return;
var observer = new ResizeObserver(function () {
if (frameRef.current) window.cancelAnimationFrame(frameRef.current);
frameRef.current = window.requestAnimationFrame(draw);
});
observer.observe(canvasRef.current);
return function () {
observer.disconnect();
if (frameRef.current) window.cancelAnimationFrame(frameRef.current);
};
}, [draw]);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.Box, {
as: "canvas",
ref: canvasRef,
width: "100%",
height: "".concat(height, "px"),
role: "img",
"aria-label": "Metric trend"
});
};
var _default = exports["default"] = /*#__PURE__*/(0, _react.memo)(SparklineCanvas);