UNPKG

@grafana/ui

Version:
79 lines (76 loc) 2.35 kB
import { jsxs, jsx } from 'react/jsx-runtime'; import { Component, createRef } from 'react'; import uPlot from 'uplot'; import { pluginLog } from './utils.mjs'; import 'uplot/dist/uPlot.min.css'; "use strict"; function sameDims(prevProps, nextProps) { return nextProps.width === prevProps.width && nextProps.height === prevProps.height; } function sameData(prevProps, nextProps) { return nextProps.data === prevProps.data; } function sameConfig(prevProps, nextProps) { return nextProps.config === prevProps.config; } class UPlotChart extends Component { constructor(props) { super(props); this.plotContainer = createRef(); this.plotCanvasBBox = createRef(); this.plotInstance = null; } reinitPlot() { var _a; let { width, height, plotRef } = this.props; (_a = this.plotInstance) == null ? void 0 : _a.destroy(); if (width === 0 && height === 0) { return; } this.props.config.addHook("setSize", (u) => { const canvas = u.over; if (!canvas) { return; } }); const config = { width: Math.floor(this.props.width), height: Math.floor(this.props.height), ...this.props.config.getConfig() }; pluginLog("UPlot", false, "Reinitializing plot", config); const plot = new uPlot(config, this.props.data, this.plotContainer.current); if (plotRef) { plotRef(plot); } this.plotInstance = plot; } componentDidMount() { this.reinitPlot(); } componentWillUnmount() { var _a; (_a = this.plotInstance) == null ? void 0 : _a.destroy(); } componentDidUpdate(prevProps) { var _a, _b; if (!sameDims(prevProps, this.props)) { (_a = this.plotInstance) == null ? void 0 : _a.setSize({ width: Math.floor(this.props.width), height: Math.floor(this.props.height) }); } else if (!sameConfig(prevProps, this.props)) { this.reinitPlot(); } else if (!sameData(prevProps, this.props)) { (_b = this.plotInstance) == null ? void 0 : _b.setData(this.props.data); } } render() { return /* @__PURE__ */ jsxs("div", { style: { position: "relative" }, children: [ /* @__PURE__ */ jsx("div", { ref: this.plotContainer, "data-testid": "uplot-main-div" }), this.props.children ] }); } } export { UPlotChart }; //# sourceMappingURL=Plot.mjs.map