UNPKG

@grafana/ui

Version:
182 lines (175 loc) 6.07 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var jsxRuntime = require('react/jsx-runtime'); var React = require('react'); var rxjs = require('rxjs'); var operators = require('rxjs/operators'); var data = require('@grafana/data'); var PanelContext = require('../../components/PanelChrome/PanelContext.cjs'); var VizLayout = require('../../components/VizLayout/VizLayout.cjs'); var Plot = require('../../components/uPlot/Plot.cjs'); var utils$1 = require('../../components/uPlot/utils.cjs'); var utils = require('./utils.cjs'); function _interopNamespaceCompat(e) { if (e && typeof e === 'object' && 'default' in e) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React); "use strict"; function sameProps(prevProps, nextProps, propsToDiff = []) { for (const propName of propsToDiff) { if (typeof propName === "function") { if (!propName(prevProps, nextProps)) { return false; } } else if (nextProps[propName] !== prevProps[propName]) { return false; } } return true; } class GraphNG extends React.Component { constructor(props) { super(props); this.panelContext = {}; this.subscription = new rxjs.Subscription(); this.getTimeRange = () => this.props.timeRange; let state = this.prepState(props); state.alignedData = state.config.prepData([state.alignedFrame]); this.state = state; this.plotInstance = React__namespace.createRef(); } prepState(props, withConfig = true) { var _a; let state = null; const { frames, fields, preparePlotFrame } = props; const preparePlotFrameFn = preparePlotFrame || utils.preparePlotFrame; const alignedFrame = preparePlotFrameFn( frames, fields || { x: data.fieldMatchers.get(data.FieldMatcherID.firstTimeField).get({}), y: data.fieldMatchers.get(data.FieldMatcherID.byTypes).get(/* @__PURE__ */ new Set([data.FieldType.number, data.FieldType.enum])) }, props.timeRange ); utils$1.pluginLog("GraphNG", false, "data aligned", alignedFrame); if (alignedFrame) { let config = (_a = this.state) == null ? void 0 : _a.config; if (withConfig) { config = props.prepConfig(alignedFrame, this.props.frames, this.getTimeRange); utils$1.pluginLog("GraphNG", false, "config prepared", config); } state = { alignedFrame, config }; utils$1.pluginLog("GraphNG", false, "data prepared", state.alignedData); } return state; } handleCursorUpdate(evt) { var _a, _b; const time = (_b = (_a = evt.payload) == null ? void 0 : _a.point) == null ? void 0 : _b.time; const u = this.plotInstance.current; if (u && time) { const left = u.valToPos(time, "x"); let top; if (left) { top = utils$1.findMidPointYPosition(u, u.posToIdx(left)); } if (!top || !left) { return; } u.setCursor({ left, top }); } } componentDidMount() { this.panelContext = this.context; const { eventBus } = this.panelContext; this.subscription.add( eventBus.getStream(data.DataHoverEvent).pipe(operators.throttleTime(50)).subscribe({ next: (evt) => { if (eventBus === evt.origin) { return; } this.handleCursorUpdate(evt); } }) ); this.subscription.add( eventBus.getStream(data.LegacyGraphHoverEvent).pipe(operators.throttleTime(50)).subscribe({ next: (evt) => this.handleCursorUpdate(evt) }) ); this.subscription.add( eventBus.getStream(data.DataHoverClearEvent).pipe(operators.throttleTime(50)).subscribe({ next: () => { var _a; const u = (_a = this.plotInstance) == null ? void 0 : _a.current; if (u && !u.cursor._lock) { u.setCursor({ left: -10, top: -10 }); } } }) ); } componentDidUpdate(prevProps) { const { frames, structureRev, timeZone, propsToDiff } = this.props; const propsChanged = !sameProps(prevProps, this.props, propsToDiff); if (frames !== prevProps.frames || propsChanged || timeZone !== prevProps.timeZone) { let newState = this.prepState(this.props, false); if (newState) { const shouldReconfig = this.state.config === void 0 || timeZone !== prevProps.timeZone || structureRev !== prevProps.structureRev || !structureRev || propsChanged; if (shouldReconfig) { newState.config = this.props.prepConfig(newState.alignedFrame, this.props.frames, this.getTimeRange); utils$1.pluginLog("GraphNG", false, "config recreated", newState.config); } newState.alignedData = newState.config.prepData([newState.alignedFrame]); this.setState(newState); } } } componentWillUnmount() { this.subscription.unsubscribe(); } render() { const { width, height, children, renderLegend } = this.props; const { config, alignedFrame, alignedData } = this.state; if (!config) { return null; } return /* @__PURE__ */ jsxRuntime.jsx(VizLayout.VizLayout, { width, height, legend: renderLegend(config), children: (vizWidth, vizHeight) => /* @__PURE__ */ jsxRuntime.jsx( Plot.UPlotChart, { config, data: alignedData, width: vizWidth, height: vizHeight, plotRef: (u) => this.plotInstance.current = u, children: children ? children(config, alignedFrame) : null } ) }); } } GraphNG.contextType = PanelContext.PanelContextRoot; exports.GraphNG = GraphNG; //# sourceMappingURL=GraphNG.cjs.map