@grafana/ui
Version:
Grafana Components Library
213 lines (210 loc) • 6.67 kB
JavaScript
import uPlot from 'uplot';
import { incrRoundDn, incrRoundUp, isBooleanUnit } from '@grafana/data';
import { ScaleDistribution, StackingMode } from '@grafana/schema';
import { PlotConfigBuilder } from '../types.mjs';
;
const isValidLogBase = (v) => v === 2 || v === 10;
const FALLBACK_LOG = 10;
const DISTR_MAP = {
[ScaleDistribution.Linear]: 1,
[ScaleDistribution.Ordinal]: 2,
[ScaleDistribution.Log]: 3,
[ScaleDistribution.Symlog]: 4
// custom is 100 in uPlot, but we don't support it so not mapping here
};
class UPlotScaleBuilder extends PlotConfigBuilder {
merge(props) {
this.props.min = optMinMax("min", this.props.min, props.min);
this.props.max = optMinMax("max", this.props.max, props.max);
}
getConfig() {
var _a;
let {
isTime,
auto,
scaleKey,
min: hardMin,
max: hardMax,
softMin,
softMax,
range,
direction,
orientation,
centeredZero,
decimals,
stackingMode,
padMinBy = 0.1,
padMaxBy = 0.1
} = this.props;
if (stackingMode === StackingMode.Percent) {
if (hardMin == null && softMin == null) {
softMin = 0;
}
if (hardMax == null && softMax == null) {
softMax = 1;
}
}
const distr = this.props.distribution;
const safeLog = isValidLogBase(this.props.log) ? this.props.log : FALLBACK_LOG;
const distribution = !isTime ? {
distr: distr ? DISTR_MAP[distr] : DISTR_MAP[ScaleDistribution.Linear],
log: distr === ScaleDistribution.Log || distr === ScaleDistribution.Symlog ? safeLog : void 0,
asinh: distr === ScaleDistribution.Symlog ? (_a = this.props.linearThreshold) != null ? _a : 1 : void 0
} : {};
if (distr === ScaleDistribution.Log) {
const logFn = safeLog === 2 ? Math.log2 : Math.log10;
if (hardMin != null) {
if (hardMin <= 0) {
hardMin = null;
} else {
hardMin = safeLog ** Math.floor(logFn(hardMin));
}
}
if (hardMax != null) {
if (hardMax <= 0) {
hardMax = null;
} else {
hardMax = safeLog ** Math.ceil(logFn(hardMax));
}
}
if (softMin != null) {
if (softMin <= 0) {
softMin = null;
} else {
softMin = safeLog ** Math.floor(logFn(softMin));
}
}
if (softMax != null) {
if (softMax <= 0) {
softMax = null;
} else {
softMax = safeLog ** Math.ceil(logFn(softMax));
}
}
}
const softMinMode = softMin == null ? 3 : 1;
const softMaxMode = softMax == null ? 3 : 1;
const rangeConfig = {
min: {
pad: padMinBy,
hard: hardMin != null ? hardMin : -Infinity,
soft: softMin || 0,
mode: softMinMode
},
max: {
pad: padMaxBy,
hard: hardMax != null ? hardMax : Infinity,
soft: softMax || 0,
mode: softMaxMode
}
};
const hardMinOnly = softMin == null && hardMin != null;
const hardMaxOnly = softMax == null && hardMax != null;
const hasFixedRange = hardMinOnly && hardMaxOnly;
const rangeFn = (u, dataMin, dataMax, scaleKey2) => {
var _a2;
const scale = u.scales[scaleKey2];
let minMax = [dataMin, dataMax];
if (!hasFixedRange && dataMin == null && dataMax == null) {
return minMax;
}
const logBase = (_a2 = scale.log) != null ? _a2 : FALLBACK_LOG;
if (scale.distr === DISTR_MAP[ScaleDistribution.Linear] || scale.distr === DISTR_MAP[ScaleDistribution.Ordinal] || scale.distr === DISTR_MAP[ScaleDistribution.Symlog]) {
if (centeredZero) {
const absMin = Math.abs(dataMin);
const absMax = Math.abs(dataMax);
let max = Math.max(absMin, absMax);
if (max === 0) {
max = 80;
}
dataMin = -max;
dataMax = max;
}
if (scale.distr === 4) {
minMax = uPlot.rangeAsinh(dataMin, dataMax, logBase, true);
} else {
minMax = uPlot.rangeNum(hardMinOnly ? hardMin : dataMin, hardMaxOnly ? hardMax : dataMax, rangeConfig);
}
} else if (scale.distr === DISTR_MAP[ScaleDistribution.Log]) {
minMax = uPlot.rangeLog(hardMin != null ? hardMin : dataMin, hardMax != null ? hardMax : dataMax, logBase, true);
}
if (decimals === 0) {
if (scale.distr === DISTR_MAP[ScaleDistribution.Linear] || scale.distr === DISTR_MAP[ScaleDistribution.Ordinal]) {
minMax[0] = incrRoundDn(minMax[0], 1);
minMax[1] = incrRoundUp(minMax[1], 1);
} else if (scale.distr === DISTR_MAP[ScaleDistribution.Log]) {
const logFn = scale.log === 2 ? Math.log2 : Math.log10;
if (minMax[0] <= 1) {
minMax[0] = 1;
} else {
const minExp = Math.floor(logFn(minMax[0]));
minMax[0] = logBase ** minExp;
}
const maxExp = Math.ceil(logFn(minMax[1]));
minMax[1] = logBase ** maxExp;
if (minMax[0] === minMax[1]) {
minMax[1] *= logBase;
}
} else if (scale.distr === 4) {
minMax[0] = incrRoundDn(minMax[0], 1);
minMax[1] = incrRoundUp(minMax[1], 1);
}
}
if (scale.distr === 1 || scale.distr === 4) {
if (hardMinOnly) {
minMax[0] = hardMin;
}
if (hardMaxOnly) {
minMax[1] = hardMax;
}
}
if (minMax[0] === minMax[1] && minMax[0] !== 0) {
if (scale.distr === 1) {
if (minMax[0] < 0) {
minMax[0] *= 2;
minMax[1] = 0;
} else {
minMax[1] *= 2;
minMax[0] = 0;
}
}
}
if (minMax[0] >= minMax[1]) {
minMax[0] = scale.distr === DISTR_MAP[ScaleDistribution.Log] ? 1 : 0;
minMax[1] = 100;
}
return minMax;
};
auto != null ? auto : auto = !isTime && !hasFixedRange;
if (isBooleanUnit(scaleKey)) {
auto = false;
range = [0, 1];
}
return {
[scaleKey]: {
time: isTime,
auto,
range: range != null ? range : rangeFn,
dir: direction,
ori: orientation,
...distribution
}
};
}
}
function optMinMax(minmax, a, b) {
const hasA = !(a === void 0 || a === null);
const hasB = !(b === void 0 || b === null);
if (hasA) {
if (!hasB) {
return a;
}
if (minmax === "min") {
return a < b ? a : b;
}
return a > b ? a : b;
}
return b;
}
export { UPlotScaleBuilder, optMinMax };
//# sourceMappingURL=UPlotScaleBuilder.mjs.map