UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

81 lines 3.53 kB
import { useMemo } from "react"; import { scaleLinear } from "d3-scale"; import { useChart, useAttributeValue } from "../components/provider"; export var heatmapTypes = { "default": "default", disabled: "disabled", incremental: "incremental" }; export var isHeatmap = function isHeatmap(chart) { return (!chart || typeof chart === "string" ? chart : chart.getAttribute("chartType")) === "heatmap"; }; export var useIsHeatmap = function useIsHeatmap() { return useAttributeValue("chartType") === "heatmap"; }; export var isIncremental = function isIncremental(chart) { return isHeatmap(chart) && chart.getAttribute("heatmapType") === heatmapTypes.incremental; }; var regex = /(.+)_(\d+?\.?(\d+)?|\+[Ii]nf)$/; export var 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, ")")]; }; export var 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 = 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); }; }; export var useGetColor = function useGetColor() { var opacity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; var chart = useChart(); return useMemo(function () { return makeGetColor(chart, opacity); }, [opacity]); }; export var withoutPrefix = function withoutPrefix(label) { return label ? label.replace(/.+_(\d+?\.?(\d+)?|\+[Ii]nf)$/, "$1") : label; }; export var 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); };