UNPKG

@netdata/charts

Version:

Netdata frontend SDK and chart utilities

115 lines (114 loc) 6.36 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.LateArrivingCustomColors = void 0; var _react = _interopRequireWildcard(require("react")); var _netdataUi = require("@netdata/netdata-ui"); var _color = require("./components/line/dimensions/color"); var _makeDefaultSDK = _interopRequireDefault(require("./makeDefaultSDK")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } 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; } var baseDimensions = ["cpu", "memory", "disk", "network"]; var lateCustomColors = { load: "#FF0000", iops: "#00AA00", latency: "#0000FF", errors: "#FF9900", saturation: "#AA00AA" }; var Swatch = function Swatch(_ref) { var chart = _ref.chart, id = _ref.id; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, { gap: 2, alignItems: "center", children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_color.Color, { bg: chart.selectDimensionColor(id), width: "16px", height: "16px" }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.TextSmall, { children: id }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.TextSmall, { color: "textLite", children: chart.selectDimensionColor(id) })] }); }; var LateArrivingCustomColors = exports.LateArrivingCustomColors = function LateArrivingCustomColors(_ref2) { var theme = _ref2.theme; var chart = (0, _react.useMemo)(function () { var sdk = (0, _makeDefaultSDK["default"])({ attributes: { theme: theme } }); var instance = sdk.makeChart({ attributes: { id: "perDimensionColors", colors: {} } }); sdk.appendChild(instance); baseDimensions.forEach(function (id) { return instance.selectDimensionColor(id); }); return instance; }, [theme]); var _useState = (0, _react.useState)(baseDimensions), _useState2 = _slicedToArray(_useState, 2), dimensions = _useState2[0], setDimensions = _useState2[1]; var bringInCustomDimensions = function bringInCustomDimensions() { chart.updateAttribute("colors", lateCustomColors); setDimensions([].concat(baseDimensions, _toConsumableArray(Object.keys(lateCustomColors)))); }; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_netdataUi.Flex, { column: true, gap: 4, padding: [4], background: "mainBackground", children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.Text, { children: "Base dimensions get auto-assigned palette colors. Click the button to bring in new dimensions that each carry their own custom color, keyed by name." }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.Button, { label: "Bring in custom-colored dimensions", onClick: bringInCustomDimensions }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_netdataUi.Flex, { column: true, gap: 2, children: dimensions.map(function (id) { return /*#__PURE__*/(0, _jsxRuntime.jsx)(Swatch, { chart: chart, id: id }, id); }) })] }); }; var _default = exports["default"] = { title: "Per-dimension colors", component: LateArrivingCustomColors, args: { theme: "default" }, argTypes: { theme: { control: { type: "select" }, options: ["default", "dark"] } } };