UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

98 lines 5.25 kB
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); } var _excluded = ["bg"], _excluded2 = ["value", "min", "max", "valueKey", "bg", "styleDimension"], _excluded3 = ["id", "partIndex", "valueKey"], _excluded4 = ["id", "partIndex"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } import React, { useLayoutEffect, useRef } from "react"; import styled from "styled-components"; import { Flex } from "@netdata/netdata-ui"; import { useChart, useLatestValue } from "../../provider"; import { useIsHeatmap, useGetColor } from "../../../helpers/heatmap"; import { jsx as _jsx } from "react/jsx-runtime"; export var Color = styled(Flex).attrs(function (_ref) { var bg = _ref.bg, rest = _objectWithoutProperties(_ref, _excluded); return _objectSpread({ width: "4px", round: true, "data-testid": "chartDimensions-color", flex: false, background: bg }, rest); }).withConfig({ displayName: "color__Color", componentId: "sc-1ooxdoy-0" })(["pointer-events:none;"]); export var BaseColorBar = function BaseColorBar(_ref2) { var value = _ref2.value, min = _ref2.min, max = _ref2.max, valueKey = _ref2.valueKey, bg = _ref2.bg, _ref2$styleDimension = _ref2.styleDimension, styleDimension = _ref2$styleDimension === void 0 ? "width" : _ref2$styleDimension, rest = _objectWithoutProperties(_ref2, _excluded2); var ref = useRef(); useLayoutEffect(function () { if (!ref.current) return; var animateStyle = function animateStyle() { return ref.current && (ref.current.style[styleDimension] = value ? "".concat((Math.abs(value) - min) * 100 / (max - min), "%") : 0); }; requestAnimationFrame(animateStyle); }, [value, valueKey, min, max]); var isHeatmap = useIsHeatmap(); var getHeatmapColor = useGetColor(0.4); if (!bg) return null; return /*#__PURE__*/_jsx(Color, _objectSpread({ ref: ref, width: { min: 1 }, bg: bg, sx: { background: isHeatmap ? getHeatmapColor(value) : undefined } }, rest)); }; export var ColorBar = function ColorBar(_ref3) { var id = _ref3.id, partIndex = _ref3.partIndex, valueKey = _ref3.valueKey, rest = _objectWithoutProperties(_ref3, _excluded3); var chart = useChart(); var bg = valueKey === "arp" ? "anomalyTextLite" : chart.selectDimensionColor(id, partIndex); var min = valueKey === "arp" ? 0 : chart.getAttribute("min"); var max = valueKey === "arp" ? 100 : chart.getAttribute("max"); var minAbs = Math.abs(min); var maxAbs = Math.abs(max); var percentMin = max > 0 ? min < 0 ? 0 : min : maxAbs; var value = useLatestValue(id, { valueKey: valueKey }) || 0; return /*#__PURE__*/_jsx(BaseColorBar, _objectSpread({ value: value, min: percentMin, max: maxAbs > minAbs ? maxAbs : minAbs, valueKey: valueKey, bg: bg }, rest)); }; var ColorValue = function ColorValue(_ref4) { var id = _ref4.id, partIndex = _ref4.partIndex, rest = _objectWithoutProperties(_ref4, _excluded4); var chart = useChart(); var bg = chart.selectDimensionColor(id, partIndex); if (!bg) return null; return /*#__PURE__*/_jsx(Color, _objectSpread({ bg: bg }, rest)); }; export default ColorValue;