UNPKG

@grafana/ui

Version:
222 lines (215 loc) • 7.1 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var uPlot = require('uplot'); var data = require('@grafana/data'); var schema = require('@grafana/schema'); var types = require('../types.cjs'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; } var uPlot__default = /*#__PURE__*/_interopDefaultCompat(uPlot); "use strict"; const isValidLogBase = (v) => v === 2 || v === 10; const FALLBACK_LOG = 10; const DISTR_MAP = { [schema.ScaleDistribution.Linear]: 1, [schema.ScaleDistribution.Ordinal]: 2, [schema.ScaleDistribution.Log]: 3, [schema.ScaleDistribution.Symlog]: 4 // custom is 100 in uPlot, but we don't support it so not mapping here }; class UPlotScaleBuilder extends types.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 === schema.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[schema.ScaleDistribution.Linear], log: distr === schema.ScaleDistribution.Log || distr === schema.ScaleDistribution.Symlog ? safeLog : void 0, asinh: distr === schema.ScaleDistribution.Symlog ? (_a = this.props.linearThreshold) != null ? _a : 1 : void 0 } : {}; if (distr === schema.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[schema.ScaleDistribution.Linear] || scale.distr === DISTR_MAP[schema.ScaleDistribution.Ordinal] || scale.distr === DISTR_MAP[schema.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__default.default.rangeAsinh(dataMin, dataMax, logBase, true); } else { minMax = uPlot__default.default.rangeNum(hardMinOnly ? hardMin : dataMin, hardMaxOnly ? hardMax : dataMax, rangeConfig); } } else if (scale.distr === DISTR_MAP[schema.ScaleDistribution.Log]) { minMax = uPlot__default.default.rangeLog(hardMin != null ? hardMin : dataMin, hardMax != null ? hardMax : dataMax, logBase, true); } if (decimals === 0) { if (scale.distr === DISTR_MAP[schema.ScaleDistribution.Linear] || scale.distr === DISTR_MAP[schema.ScaleDistribution.Ordinal]) { minMax[0] = data.incrRoundDn(minMax[0], 1); minMax[1] = data.incrRoundUp(minMax[1], 1); } else if (scale.distr === DISTR_MAP[schema.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] = data.incrRoundDn(minMax[0], 1); minMax[1] = data.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[schema.ScaleDistribution.Log] ? 1 : 0; minMax[1] = 100; } return minMax; }; auto != null ? auto : auto = !isTime && !hasFixedRange; if (data.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; } exports.UPlotScaleBuilder = UPlotScaleBuilder; exports.optMinMax = optMinMax; //# sourceMappingURL=UPlotScaleBuilder.cjs.map