@grafana/ui
Version:
Grafana Components Library
182 lines (177 loc) • 5.83 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var data = require('@grafana/data');
var i18n = require('@grafana/i18n');
var schema = require('@grafana/schema');
var UPlotConfigBuilder = require('../uPlot/config/UPlotConfigBuilder.cjs');
;
function preparePlotFrame(sparkline, config) {
var _a, _b, _c;
const length = sparkline.y.values.length;
const yFieldConfig = {
...sparkline.y.config,
...config
};
const xField = (_a = sparkline.x) != null ? _a : {
name: "",
values: [...Array(length).keys()],
type: data.FieldType.number,
config: {}
};
let frame = {
refId: "sparkline",
fields: [
xField,
{
...sparkline.y,
config: yFieldConfig
}
],
length
};
if (!data.isLikelyAscendingVector(xField.values)) {
frame = data.sortDataFrame(frame, 0);
}
return data.applyNullInsertThreshold({
frame,
refFieldPseudoMin: (_b = sparkline.timeRange) == null ? void 0 : _b.from.valueOf(),
refFieldPseudoMax: (_c = sparkline.timeRange) == null ? void 0 : _c.to.valueOf()
});
}
function getYRange(alignedFrame) {
var _a, _b, _c, _d, _e;
const field = alignedFrame.fields[1];
let { min, max } = (_a = field.state) == null ? void 0 : _a.range;
min = Math.min(min, (_b = field.config.min) != null ? _b : Infinity);
max = Math.max(max, (_c = field.config.max) != null ? _c : -Infinity);
const noValue = +((_d = field.config) == null ? void 0 : _d.noValue);
if (!Number.isNaN(noValue)) {
min = Math.min(min, noValue);
max = Math.max(max, noValue);
}
const decimals = (_e = field.config.decimals) != null ? _e : Math.max(data.guessDecimals(min), data.guessDecimals(max));
let roundedMin = data.roundDecimals(min, decimals);
let roundedMax = data.roundDecimals(max, decimals);
if (roundedMin !== roundedMax) {
return [min, max];
}
if (roundedMin === 0) {
roundedMax = 1;
} else if (roundedMin < 0) {
roundedMin *= 2;
} else {
roundedMax *= 2;
}
return [roundedMin, roundedMax];
}
const HIGHLIGHT_IDX_POINT_SIZE = 6;
const defaultConfig = {
drawStyle: schema.GraphDrawStyle.Line,
showPoints: schema.VisibilityMode.Auto,
axisPlacement: schema.AxisPlacement.Hidden,
pointSize: 0
};
const prepareSeries = (sparkline, _theme, fieldConfig, _showHighlights) => {
const frame = data.nullToValue(preparePlotFrame(sparkline, fieldConfig));
if (frame.fields.some((f) => f.values.length <= 1)) {
return {
warning: i18n.t(
"grafana-ui.components.sparkline.warning.too-few-values",
"Sparkline requires at least two values to render."
),
frame
};
}
return { frame };
};
const prepareConfig = (sparkline, dataFrame, theme, showHighlights) => {
var _a;
const builder = new UPlotConfigBuilder.UPlotConfigBuilder();
const rangePad = HIGHLIGHT_IDX_POINT_SIZE / 2;
builder.setCursor({
show: false,
x: false,
// no crosshairs
y: false
});
const xField = dataFrame.fields[0];
builder.addScale({
scaleKey: "x",
orientation: schema.ScaleOrientation.Horizontal,
direction: schema.ScaleDirection.Right,
isTime: false,
// xField.type === FieldType.time,
range: () => {
if (sparkline.x) {
if (sparkline.timeRange && sparkline.x.type === data.FieldType.time) {
return [sparkline.timeRange.from.valueOf(), sparkline.timeRange.to.valueOf()];
}
const vals = sparkline.x.values;
return [vals[0], vals[vals.length - 1]];
}
return [0, sparkline.y.values.length - 1];
}
});
builder.addAxis({
scaleKey: "x",
theme,
placement: schema.AxisPlacement.Hidden,
show: false
});
for (let i = 0; i < dataFrame.fields.length; i++) {
const field = dataFrame.fields[i];
const config = field.config;
const customConfig = {
...defaultConfig,
...config.custom
};
if (field === xField || field.type !== data.FieldType.number) {
continue;
}
const scaleKey = config.unit || "__fixed";
builder.addScale({
scaleKey,
orientation: schema.ScaleOrientation.Vertical,
direction: schema.ScaleDirection.Up,
range: () => getYRange(dataFrame)
});
builder.addAxis({
scaleKey,
theme,
placement: schema.AxisPlacement.Hidden,
show: false
});
const colorMode = data.getFieldColorModeForField(field);
const seriesColor = colorMode.getCalculator(field, theme)(0, 0);
const hasHighlightIndex = showHighlights && typeof sparkline.highlightIndex === "number";
if (hasHighlightIndex) {
builder.setPadding([rangePad, rangePad, rangePad, rangePad]);
}
const pointsMode = customConfig.drawStyle === schema.GraphDrawStyle.Points || hasHighlightIndex ? schema.VisibilityMode.Always : customConfig.showPoints;
builder.addSeries({
pxAlign: false,
scaleKey,
theme,
colorMode,
thresholds: config.thresholds,
drawStyle: customConfig.drawStyle,
lineColor: (_a = customConfig.lineColor) != null ? _a : seriesColor,
lineWidth: customConfig.lineWidth,
lineInterpolation: customConfig.lineInterpolation,
showPoints: pointsMode,
pointSize: hasHighlightIndex ? HIGHLIGHT_IDX_POINT_SIZE : customConfig.pointSize,
pointsFilter: hasHighlightIndex ? [sparkline.highlightIndex] : void 0,
fillOpacity: customConfig.fillOpacity,
fillColor: customConfig.fillColor,
lineStyle: customConfig.lineStyle,
gradientMode: customConfig.gradientMode,
spanNulls: customConfig.spanNulls
});
}
return builder;
};
exports.getYRange = getYRange;
exports.prepareConfig = prepareConfig;
exports.preparePlotFrame = preparePlotFrame;
exports.prepareSeries = prepareSeries;
//# sourceMappingURL=utils.cjs.map