UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

211 lines 8.44 kB
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } import { isBinary } from "../units"; var decimalMultipliers = [1, 2, 2.5, 5, 10]; var binaryMultipliers = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]; var durationUnitsInSeconds = { ns: 1e-9, us: 1e-6, ms: 1e-3, s: 1, min: 60, h: 3600, d: 86400, wk: 604800, mo: 2592000, a: 31536000 }; var durationStepsInSeconds = [1e-9, 2e-9, 5e-9, 10e-9, 20e-9, 50e-9, 100e-9, 200e-9, 500e-9, 1e-6, 2e-6, 5e-6, 10e-6, 20e-6, 50e-6, 100e-6, 200e-6, 500e-6, 1e-3, 2e-3, 5e-3, 10e-3, 20e-3, 50e-3, 100e-3, 200e-3, 500e-3, 1, 2, 5, 10, 15, 30, 60, 120, 300, 600, 900, 1800, 2700, 3600, 5400, 7200, 10800, 14400, 21600, 43200, 86400, 172800, 604800, 1209600, 2592000, 7776000, 15552000, 31536000]; var getMaxTicks = function getMaxTicks(_ref) { var pixels = _ref.pixels, pixelsPerTick = _ref.pixelsPerTick, _ref$minPixelsPerTick = _ref.minPixelsPerTick, minPixelsPerTick = _ref$minPixelsPerTick === void 0 ? pixelsPerTick : _ref$minPixelsPerTick; return Math.max(2, Math.ceil(pixels / Math.max(pixelsPerTick || 1, minPixelsPerTick || 1))); }; var getPrecision = function getPrecision(step) { if (!isFinite(step) || step === 0) return 0; var exponent = Math.floor(Math.log10(Math.abs(step))); return Math.max(0, -exponent + 6); }; var roundToStepPrecision = function roundToStepPrecision(value, step) { var precision = getPrecision(step); var multiplier = Math.pow(10, precision); return Math.round(value * multiplier) / multiplier; }; var getTickCount = function getTickCount(_ref2) { var min = _ref2.min, max = _ref2.max, step = _ref2.step; if (!isFinite(min) || !isFinite(max) || !isFinite(step) || step === 0) return 0; var low = Math.min(min, max); var high = Math.max(min, max); var start = Math.floor(low / step) * step; var end = Math.ceil(high / step) * step; var count = Math.round((end - start) / step); return count + 1; }; var makeTicks = function makeTicks(_ref3) { var min = _ref3.min, max = _ref3.max, step = _ref3.step; if (!isFinite(min) || !isFinite(max) || !isFinite(step) || step === 0) return []; var reversed = max < min; var low = Math.min(min, max); var high = Math.max(min, max); var start = Math.floor(low / step) * step; var end = Math.ceil(high / step) * step; var count = Math.round((end - start) / step); var ticks = []; if (count > 1000) return ticks; for (var index = 0; index <= count; index++) { ticks.push(roundToStepPrecision(start + index * step, step)); } return reversed ? ticks.reverse() : ticks; }; var makeNumericStep = function makeNumericStep(_ref4) { var range = _ref4.range, maxTicks = _ref4.maxTicks, base = _ref4.base, multipliers = _ref4.multipliers; if (!isFinite(range) || range <= 0) return 1; var minimumStep = range / maxTicks; var basePower = Math.floor(Math.log(minimumStep) / Math.log(base)); var _loop = function _loop() { var baseScale = Math.pow(base, power); var step = multipliers.find(function (multiplier) { return multiplier * baseScale >= minimumStep; }); if (step) return { v: step * baseScale }; }, _ret; for (var power = basePower; power <= basePower + 2; power++) { _ret = _loop(); if (_ret) return _ret.v; } return Math.pow(base, basePower + 2); }; var makeNumericTicks = function makeNumericTicks(_ref5) { var min = _ref5.min, max = _ref5.max, pixels = _ref5.pixels, pixelsPerTick = _ref5.pixelsPerTick, units = _ref5.units; var binary = isBinary(units === null || units === void 0 ? void 0 : units[0]); var maxTicks = getMaxTicks({ pixels: pixels, pixelsPerTick: pixelsPerTick }); var range = Math.abs(max - min); var step = makeNumericStep({ range: range, maxTicks: maxTicks, base: binary ? 1024 : 10, multipliers: binary ? binaryMultipliers : decimalMultipliers }); return makeTicks({ min: min, max: max, step: step }); }; var getDurationUnit = function getDurationUnit(units) { if (!(units !== null && units !== void 0 && units.length)) return null; var durationUnits = units.filter(function (unit) { return durationUnitsInSeconds[unit]; }); if (durationUnits.length !== units.length) return null; var _durationUnits = _slicedToArray(durationUnits, 1), firstUnit = _durationUnits[0]; return durationUnits.every(function (unit) { return unit === firstUnit; }) ? firstUnit : null; }; export var isDurationAxis = function isDurationAxis() { var _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref6$secondsAsTime = _ref6.secondsAsTime, secondsAsTime = _ref6$secondsAsTime === void 0 ? true : _ref6$secondsAsTime, _ref6$units = _ref6.units, units = _ref6$units === void 0 ? [] : _ref6$units; return Boolean(secondsAsTime && getDurationUnit(units)); }; var makeDurationTicks = function makeDurationTicks(_ref7) { var min = _ref7.min, max = _ref7.max, pixels = _ref7.pixels, pixelsPerTick = _ref7.pixelsPerTick, units = _ref7.units; var unit = getDurationUnit(units); if (!unit) return null; var maxTicks = getMaxTicks({ pixels: pixels, pixelsPerTick: pixelsPerTick, minPixelsPerTick: 40 }); var unitSeconds = durationUnitsInSeconds[unit]; var rangeInSeconds = Math.abs(max - min) * unitSeconds; var yearInSeconds = 365 * 86400; var durationSteps = rangeInSeconds >= yearInSeconds ? durationStepsInSeconds.filter(function (step) { return step >= yearInSeconds; }) : durationStepsInSeconds; var steps = durationSteps.map(function (step) { return step / unitSeconds; }); var fallbackStep = makeNumericStep({ range: Math.abs(max - min), maxTicks: maxTicks, base: 10, multipliers: decimalMultipliers }); var step = steps.find(function (candidate) { return getTickCount({ min: min, max: max, step: candidate }) <= maxTicks; }) || fallbackStep; return makeTicks({ min: min, max: max, step: step }); }; export var makeAxisTicks = function makeAxisTicks() { var _ref8 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, min = _ref8.min, max = _ref8.max, pixels = _ref8.pixels, pixelsPerTick = _ref8.pixelsPerTick, _ref8$units = _ref8.units, units = _ref8$units === void 0 ? [] : _ref8$units, _ref8$secondsAsTime = _ref8.secondsAsTime, secondsAsTime = _ref8$secondsAsTime === void 0 ? true : _ref8$secondsAsTime; if (!isFinite(min) || !isFinite(max)) return []; if (min === max) return [{ v: min }]; var values = secondsAsTime && getDurationUnit(units) ? makeDurationTicks({ min: min, max: max, pixels: pixels, pixelsPerTick: pixelsPerTick, units: units }) : makeNumericTicks({ min: min, max: max, pixels: pixels, pixelsPerTick: pixelsPerTick, units: units }); return values.map(function (v) { return { v: v }; }); };