UNPKG

@grafana/ui

Version:
212 lines (205 loc) • 6.88 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'); var gradientFills = require('./gradientFills.cjs'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; } var uPlot__default = /*#__PURE__*/_interopDefaultCompat(uPlot); "use strict"; class UPlotSeriesBuilder extends types.PlotConfigBuilder { getConfig() { var _a; const { facets, drawStyle, pathBuilder, pointsBuilder, pointsFilter, lineInterpolation, lineWidth, lineStyle, barAlignment, barWidthFactor, barMaxWidth, showPoints, pointSize, scaleKey, pxAlign, spanNulls, show = true } = this.props; let lineConfig = {}; let lineColor = this.getLineColor(); lineConfig.stroke = lineColor; lineConfig.width = lineWidth; if (lineStyle && lineStyle.fill !== "solid") { if (lineStyle.fill === "dot") { lineConfig.cap = "round"; } lineConfig.dash = (_a = lineStyle.dash) != null ? _a : [10, 10]; } if (pathBuilder != null) { lineConfig.paths = pathBuilder; } else if (drawStyle === schema.GraphDrawStyle.Points) { lineConfig.paths = () => null; } else if (drawStyle != null) { lineConfig.paths = (self, seriesIdx, idx0, idx1) => { let pathsBuilder = mapDrawStyleToPathBuilder( drawStyle, lineInterpolation, barAlignment, barWidthFactor, barMaxWidth ); return pathsBuilder(self, seriesIdx, idx0, idx1); }; } const useColor = ( // @ts-ignore typeof lineColor === "string" ? lineColor : (u, seriesIdx) => u.series[seriesIdx]._stroke ); const pointsConfig = { points: { stroke: useColor, fill: useColor, size: !pointSize || pointSize < lineWidth ? void 0 : pointSize, filter: pointsFilter } }; if (pointsBuilder != null) { pointsConfig.points.show = pointsBuilder; } else { if (drawStyle === schema.GraphDrawStyle.Points) { pointsConfig.points.show = true; } else { if (showPoints === schema.VisibilityMode.Auto) { if (drawStyle === schema.GraphDrawStyle.Bars) { pointsConfig.points.show = false; } } else if (showPoints === schema.VisibilityMode.Never) { pointsConfig.points.show = false; } else if (showPoints === schema.VisibilityMode.Always) { pointsConfig.points.show = true; } } } return { scale: scaleKey, facets, spanGaps: typeof spanNulls === "number" ? false : spanNulls, value: () => "", pxAlign, show, fill: this.getFill(), ...lineConfig, ...pointsConfig }; } getLineColor() { const { lineColor, gradientMode, colorMode, thresholds, theme, hardMin, hardMax, softMin, softMax, dynamicSeriesColor } = this.props; if (gradientMode === schema.GraphGradientMode.None && dynamicSeriesColor) { return (plot, seriesIdx) => { var _a, _b; return (_b = (_a = dynamicSeriesColor(seriesIdx)) != null ? _a : lineColor) != null ? _b : data.FALLBACK_COLOR; }; } if (gradientMode === schema.GraphGradientMode.Scheme && (colorMode == null ? void 0 : colorMode.id) !== data.FieldColorModeId.Fixed) { return gradientFills.getScaleGradientFn(1, theme, colorMode, thresholds, hardMin, hardMax, softMin, softMax); } if (gradientMode === schema.GraphGradientMode.Hue) { return gradientFills.getHueGradientFn(lineColor != null ? lineColor : data.FALLBACK_COLOR, 1, theme); } return lineColor != null ? lineColor : data.FALLBACK_COLOR; } getFill() { const { lineColor, fillColor, gradientMode, fillOpacity, colorMode, thresholds, theme, hardMin, hardMax, softMin, softMax, dynamicSeriesColor } = this.props; if (fillColor) { return fillColor; } const mode = gradientMode != null ? gradientMode : schema.GraphGradientMode.None; const opacityPercent = (fillOpacity != null ? fillOpacity : 0) / 100; if (mode === schema.GraphGradientMode.None && dynamicSeriesColor && opacityPercent > 0) { return (u, seriesIdx) => { let lineColor2 = u.series[seriesIdx]._stroke; return data.colorManipulator.alpha(lineColor2 != null ? lineColor2 : "", opacityPercent); }; } switch (mode) { case schema.GraphGradientMode.Opacity: return gradientFills.getOpacityGradientFn(fillColor != null ? fillColor : lineColor, opacityPercent); case schema.GraphGradientMode.Hue: return gradientFills.getHueGradientFn(fillColor != null ? fillColor : lineColor, opacityPercent, theme); case schema.GraphGradientMode.Scheme: if ((colorMode == null ? void 0 : colorMode.id) !== data.FieldColorModeId.Fixed) { return gradientFills.getScaleGradientFn(opacityPercent, theme, colorMode, thresholds, hardMin, hardMax, softMin, softMax); } // intentional fall-through to handle Scheme with Fixed color default: if (opacityPercent > 0) { return data.colorManipulator.alpha(lineColor != null ? lineColor : "", opacityPercent); } } return void 0; } } let builders = void 0; function mapDrawStyleToPathBuilder(style, lineInterpolation, barAlignment = schema.BarAlignment.Center, barWidthFactor = 0.6, barMaxWidth = 200) { const pathBuilders = uPlot__default.default.paths; if (!builders) { builders = { linear: pathBuilders.linear(), smooth: pathBuilders.spline(), stepBefore: pathBuilders.stepped({ align: -1 }), stepAfter: pathBuilders.stepped({ align: 1 }) }; } if (style === schema.GraphDrawStyle.Bars) { let barsCfgKey = `bars|${barAlignment}|${barWidthFactor}|${barMaxWidth}`; if (!builders[barsCfgKey]) { builders[barsCfgKey] = pathBuilders.bars({ size: [barWidthFactor, barMaxWidth], align: barAlignment }); } return builders[barsCfgKey]; } else if (style === schema.GraphDrawStyle.Line) { if (lineInterpolation === schema.LineInterpolation.StepBefore) { return builders.stepBefore; } if (lineInterpolation === schema.LineInterpolation.StepAfter) { return builders.stepAfter; } if (lineInterpolation === schema.LineInterpolation.Smooth) { return builders.smooth; } } return builders.linear; } exports.UPlotSeriesBuilder = UPlotSeriesBuilder; //# sourceMappingURL=UPlotSeriesBuilder.cjs.map