import React, { PureComponent } from 'react'; import { PanelProps } from '@grafana/data'; import { config } from '@grafana/runtime'; import { LegendDisplayMode, Portal, UPlotChart, UPlotConfigBuilder, VizLayout, VizLegend, VizLegendItem, VizTooltipContainer, } from '@grafana/ui'; import { FacetedData } from '@grafana/ui/src/components/uPlot/types'; import { TooltipView } from './TooltipView'; import { XYChartOptions } from './models.gen'; import { prepData, prepScatter } from './scatter'; import { ScatterHoverEvent, ScatterSeries } from './types'; type Props = PanelProps; type State = { error?: string; series: ScatterSeries[]; builder?: UPlotConfigBuilder; facets?: FacetedData; hover?: ScatterHoverEvent; }; export class XYChartPanel2 extends PureComponent { state: State = { series: [], }; componentDidMount() { this.initSeries(); // also data } componentDidUpdate(oldProps: Props) { const { options, data } = this.props; const configsChanged = options !== oldProps.options || data.structureRev !== oldProps.data.structureRev; if (configsChanged) { this.initSeries(); } else if (data !== oldProps.data) { this.initFacets(); } } scatterHoverCallback = (hover?: ScatterHoverEvent) => { this.setState({ hover }); }; getData = () => { return this.props.data.series; }; initSeries = () => { const { options, data } = this.props; const info: State = prepScatter(options, this.getData, config.theme2, this.scatterHoverCallback); if (info.series.length && data.series) { info.facets = prepData(info, data.series); info.error = undefined; } this.setState(info); }; initFacets = () => { this.setState({ facets: prepData(this.state, this.props.data.series), }); }; renderLegend = () => { const { data } = this.props; const { series } = this.state; const items: VizLegendItem[] = []; for (const s of series) { const frame = s.frame(data.series); if (frame) { for (const item of s.legend(frame)) { items.push(item); } } } return ( ); }; render() { const { width, height, timeRange, data } = this.props; const { error, facets, builder, hover, series } = this.state; if (error || !builder) { return (

{error}

); } return ( <> {(vizWidth: number, vizHeight: number) => ( //
            //   {JSON.stringify(scatterData, null, 2)}
            // 
{/*children ? children(config, alignedFrame) : null*/} )}
{hover && ( )} ); } }