UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

87 lines (86 loc) 4.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withoutPrefix = exports.useIsHeatmap = exports.useGetColor = exports.makeGetColor = exports.isIncremental = exports.isHeatmap = exports.heatmapTypes = exports.heatmapOrChartType = exports.cropHeatmapZeroEdges = void 0; var _react = require("react"); var _d3Scale = require("d3-scale"); var _provider = require("../components/provider"); var heatmapTypes = exports.heatmapTypes = { "default": "default", disabled: "disabled", incremental: "incremental" }; var isHeatmap = exports.isHeatmap = function isHeatmap(chart) { return (!chart || typeof chart === "string" ? chart : chart.getAttribute("chartType")) === "heatmap"; }; var useIsHeatmap = exports.useIsHeatmap = function useIsHeatmap() { return (0, _provider.useAttributeValue)("chartType") === "heatmap"; }; var isIncremental = exports.isIncremental = function isIncremental(chart) { return isHeatmap(chart) && chart.getAttribute("heatmapType") === heatmapTypes.incremental; }; var regex = /(.+)_(\d+?\.?(\d+)?|\+[Ii]nf)$/; var heatmapOrChartType = exports.heatmapOrChartType = function heatmapOrChartType(ids, chartType) { return Array.isArray(ids) && ids.length && ids.every(function (id) { return id.match(regex); }) ? "heatmap" : chartType; }; var getColors = function getColors(opacity) { return ["rgba(62, 73, 137, ".concat(opacity, ")"), "rgba(49, 104, 142, ".concat(opacity, ")"), "rgba(38, 130, 142, ".concat(opacity, ")"), "rgba(31, 158, 137, ".concat(opacity, ")"), "rgba(53, 183, 121, ".concat(opacity, ")"), "rgba(110, 206, 88, ".concat(opacity, ")"), "rgba(181, 222, 43, ".concat(opacity, ")"), "rgba(253, 231, 37, ".concat(opacity, ")")]; }; var makeGetColor = exports.makeGetColor = function makeGetColor(chart) { var opacity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; var max = Number(chart.getAttribute("max")); var colors = getColors(opacity); if (!Number.isFinite(max) || max <= 0) { return function (value) { return value == null || value === 0 ? "transparent" : colors[0]; }; } var step = max / (colors.length - 1); var getLinearColor = (0, _d3Scale.scaleLinear)().domain(Array.from({ length: colors.length - 1 }, function (_, i) { return i * step; })).range(colors); return function (value) { return value == null || value === 0 ? "transparent" : getLinearColor(value); }; }; var useGetColor = exports.useGetColor = function useGetColor() { var opacity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; var chart = (0, _provider.useChart)(); return (0, _react.useMemo)(function () { return makeGetColor(chart, opacity); }, [opacity]); }; var withoutPrefix = exports.withoutPrefix = function withoutPrefix(label) { return label ? label.replace(/.+_(\d+?\.?(\d+)?|\+[Ii]nf)$/, "$1") : label; }; var cropHeatmapZeroEdges = exports.cropHeatmapZeroEdges = function cropHeatmapZeroEdges(ids, isZeroOnly) { var minVisible = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 5; if (!Array.isArray(ids) || typeof isZeroOnly !== "function" || ids.length <= minVisible) return ids; var zeroOnly = ids.map(isZeroOnly); var firstNonZero = zeroOnly.findIndex(function (isZero) { return !isZero; }); if (firstNonZero === -1) return ids; var lastNonZero = zeroOnly.length - 1; while (lastNonZero >= 0 && zeroOnly[lastNonZero]) lastNonZero--; var first = firstNonZero > 0 ? firstNonZero - 1 : firstNonZero; var last = lastNonZero < ids.length - 1 ? lastNonZero + 1 : lastNonZero; var targetLength = Math.min(minVisible, ids.length); while (last - first + 1 < targetLength) { var bottomDistance = firstNonZero - first; var topDistance = last - lastNonZero; if (first > 0 && (last === ids.length - 1 || bottomDistance <= topDistance)) { first--; } else if (last < ids.length - 1) { last++; } else { break; } } return ids.slice(first, last + 1); };