@grafana/ui
Version:
Grafana Components Library
62 lines (59 loc) • 2.13 kB
JavaScript
import { ThresholdsMode, GAUGE_DEFAULT_MINIMUM, GAUGE_DEFAULT_MAXIMUM, getActiveThreshold } from '@grafana/data';
import { VizOrientation } from '@grafana/schema';
;
const DEFAULT_THRESHOLDS = {
mode: ThresholdsMode.Absolute,
steps: [
{ value: -Infinity, color: "green" },
{ value: 80, color: "red" }
]
};
function calculateGaugeAutoProps(width, height, title, orientation) {
const showLabel = title !== null && title !== void 0;
const titleFontSizeDimension = orientation === VizOrientation.Vertical ? height : width;
const titleFontSize = Math.min(titleFontSizeDimension * 0.15 / 1.5, 20);
const titleHeight = titleFontSize * 1.5;
const availableHeight = showLabel ? height - titleHeight : height;
const gaugeHeight = Math.min(availableHeight, width);
return {
showLabel,
gaugeHeight,
titleFontSize
};
}
function getFormattedThresholds(decimals, field, theme) {
var _a, _b, _c;
const thresholds = (_a = field.thresholds) != null ? _a : DEFAULT_THRESHOLDS;
const isPercent = thresholds.mode === ThresholdsMode.Percentage;
const steps = thresholds.steps;
let min = (_b = field.min) != null ? _b : GAUGE_DEFAULT_MINIMUM;
let max = (_c = field.max) != null ? _c : GAUGE_DEFAULT_MAXIMUM;
if (isPercent) {
min = 0;
max = 100;
}
const first = getActiveThreshold(min, steps);
const last = getActiveThreshold(max, steps);
const formatted = [
{ value: +min.toFixed(decimals), color: theme.visualization.getColorByName(first.color) }
];
let skip = true;
for (let i = 0; i < steps.length; i++) {
const step = steps[i];
if (skip) {
if (first === step) {
skip = false;
}
continue;
}
const prev = steps[i - 1];
formatted.push({ value: step.value, color: theme.visualization.getColorByName(prev.color) });
if (step === last) {
break;
}
}
formatted.push({ value: +max.toFixed(decimals), color: theme.visualization.getColorByName(last.color) });
return formatted;
}
export { DEFAULT_THRESHOLDS, calculateGaugeAutoProps, getFormattedThresholds };
//# sourceMappingURL=utils.mjs.map