@grafana/ui
Version:
Grafana Components Library
87 lines (80 loc) • 2.66 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var uPlot = require('uplot');
var utils = require('./utils.cjs');
require('uplot/dist/uPlot.min.css');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
var uPlot__default = /*#__PURE__*/_interopDefaultCompat(uPlot);
;
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 React.Component {
constructor(props) {
super(props);
this.plotContainer = React.createRef();
this.plotCanvasBBox = React.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()
};
utils.pluginLog("UPlot", false, "Reinitializing plot", config);
const plot = new uPlot__default.default(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__ */ jsxRuntime.jsxs("div", { style: { position: "relative" }, children: [
/* @__PURE__ */ jsxRuntime.jsx("div", { ref: this.plotContainer, "data-testid": "uplot-main-div" }),
this.props.children
] });
}
}
exports.UPlotChart = UPlotChart;
//# sourceMappingURL=Plot.cjs.map