@netdata/charts
Version:
Netdata frontend SDK and chart utilities
106 lines • 4.84 kB
JavaScript
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; }
import React, { useMemo, useState } from "react";
import { Flex, Text, TextSmall, Button } from "@netdata/netdata-ui";
import { Color } from "./components/line/dimensions/color";
import makeDefaultSDK from "./makeDefaultSDK";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
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__*/_jsxs(Flex, {
gap: 2,
alignItems: "center",
children: [/*#__PURE__*/_jsx(Color, {
bg: chart.selectDimensionColor(id),
width: "16px",
height: "16px"
}), /*#__PURE__*/_jsx(TextSmall, {
children: id
}), /*#__PURE__*/_jsx(TextSmall, {
color: "textLite",
children: chart.selectDimensionColor(id)
})]
});
};
export var LateArrivingCustomColors = function LateArrivingCustomColors(_ref2) {
var theme = _ref2.theme;
var chart = useMemo(function () {
var sdk = makeDefaultSDK({
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 = 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__*/_jsxs(Flex, {
column: true,
gap: 4,
padding: [4],
background: "mainBackground",
children: [/*#__PURE__*/_jsx(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__*/_jsx(Button, {
label: "Bring in custom-colored dimensions",
onClick: bringInCustomDimensions
}), /*#__PURE__*/_jsx(Flex, {
column: true,
gap: 2,
children: dimensions.map(function (id) {
return /*#__PURE__*/_jsx(Swatch, {
chart: chart,
id: id
}, id);
})
})]
});
};
export default {
title: "Per-dimension colors",
component: LateArrivingCustomColors,
args: {
theme: "default"
},
argTypes: {
theme: {
control: {
type: "select"
},
options: ["default", "dark"]
}
}
};