@netdata/charts
Version:
Netdata frontend SDK and chart utilities
50 lines • 2.24 kB
JavaScript
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 = chart.getAttribute("max");
var colors = getColors(opacity);
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 ? "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;
};