diff --git a/packages/grafana-ui/src/components/GraphNG/GraphNG.tsx b/packages/grafana-ui/src/components/GraphNG/GraphNG.tsx index 51e6bcf9f4d..5ec89b7d7b6 100755 --- a/packages/grafana-ui/src/components/GraphNG/GraphNG.tsx +++ b/packages/grafana-ui/src/components/GraphNG/GraphNG.tsx @@ -41,7 +41,7 @@ export interface GraphNGProps extends Themeable2 { prepConfig: (alignedFrame: DataFrame, getTimeRange: () => TimeRange) => UPlotConfigBuilder; propsToDiff?: string[]; preparePlotFrame?: (frames: DataFrame[], dimFields: XYFieldMatchers) => DataFrame; - renderLegend: (config: UPlotConfigBuilder) => React.ReactElement; + renderLegend: (config: UPlotConfigBuilder) => React.ReactElement | null; } function sameProps(prevProps: any, nextProps: any, propsToDiff: string[] = []) { diff --git a/packages/grafana-ui/src/components/TimeSeries/TimeSeries.tsx b/packages/grafana-ui/src/components/TimeSeries/TimeSeries.tsx index 1dba6045dd9..8b509d3bb77 100644 --- a/packages/grafana-ui/src/components/TimeSeries/TimeSeries.tsx +++ b/packages/grafana-ui/src/components/TimeSeries/TimeSeries.tsx @@ -23,22 +23,13 @@ export class UnthemedTimeSeries extends React.Component { }; renderLegend = (config: UPlotConfigBuilder) => { - const { legend, onLegendClick, frames } = this.props; + const { legend, frames } = this.props; if (!config || (legend && legend.displayMode === LegendDisplayMode.Hidden)) { - return; + return null; } - return ( - - ); + return ; }; render() { diff --git a/packages/grafana-ui/src/components/VizLayout/VizLayout.tsx b/packages/grafana-ui/src/components/VizLayout/VizLayout.tsx index 966bfcb2293..1ef59b16c23 100644 --- a/packages/grafana-ui/src/components/VizLayout/VizLayout.tsx +++ b/packages/grafana-ui/src/components/VizLayout/VizLayout.tsx @@ -9,7 +9,7 @@ import { LegendPlacement } from '..'; export interface VizLayoutProps { width: number; height: number; - legend?: React.ReactElement; + legend?: React.ReactElement | null; children: (width: number, height: number) => React.ReactNode; } diff --git a/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx b/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx index ccb445600a4..42c2cae8011 100644 --- a/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx +++ b/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx @@ -1,4 +1,4 @@ -import React, { useCallback } from 'react'; +import React from 'react'; import { DataFrame, DisplayValue, fieldReducers, getFieldDisplayName, reduceField } from '@grafana/data'; import { UPlotConfigBuilder } from './config/UPlotConfigBuilder'; import { VizLegendItem } from '../VizLegend/types'; @@ -6,42 +6,22 @@ import { VizLegendOptions } from '../VizLegend/models.gen'; import { AxisPlacement } from './config'; import { VizLayout, VizLayoutLegendProps } from '../VizLayout/VizLayout'; import { VizLegend } from '../VizLegend/VizLegend'; -import { GraphNGLegendEvent } from '..'; -import { mapMouseEventToMode } from '../VizLegend/utils'; const defaultFormatter = (v: any) => (v == null ? '-' : v.toFixed(1)); interface PlotLegendProps extends VizLegendOptions, Omit { data: DataFrame[]; config: UPlotConfigBuilder; - onLegendClick?: (event: GraphNGLegendEvent) => void; } export const PlotLegend: React.FC = ({ data, config, - onLegendClick, placement, calcs, displayMode, ...vizLayoutLegendProps }) => { - const onLegendLabelClick = useCallback( - (legend: VizLegendItem, event: React.MouseEvent) => { - const { fieldIndex } = legend; - - if (!onLegendClick || !fieldIndex) { - return; - } - - onLegendClick({ - fieldIndex, - mode: mapMouseEventToMode(event), - }); - }, - [onLegendClick] - ); - const legendItems = config .getSeries() .map((s) => { @@ -92,12 +72,7 @@ export const PlotLegend: React.FC = ({ return ( - + ); }; diff --git a/public/app/plugins/panel/barchart/BarChart.tsx b/public/app/plugins/panel/barchart/BarChart.tsx index cd65b1cab34..34c916c0911 100644 --- a/public/app/plugins/panel/barchart/BarChart.tsx +++ b/public/app/plugins/panel/barchart/BarChart.tsx @@ -1,6 +1,14 @@ import React from 'react'; import { DataFrame, TimeRange } from '@grafana/data'; -import { GraphNG, GraphNGProps, LegendDisplayMode, PlotLegend, UPlotConfigBuilder, withTheme2 } from '@grafana/ui'; +import { + GraphNG, + GraphNGProps, + LegendDisplayMode, + PlotLegend, + UPlotConfigBuilder, + usePanelContext, + useTheme2, +} from '@grafana/ui'; import { BarChartOptions } from './types'; import { preparePlotConfigBuilder, preparePlotFrame } from './utils'; @@ -9,14 +17,24 @@ import { preparePlotConfigBuilder, preparePlotFrame } from './utils'; */ export interface BarChartProps extends BarChartOptions, - Omit {} + Omit {} const propsToDiff: string[] = ['orientation', 'barWidth', 'groupWidth', 'showValue']; -class UnthemedBarChart extends React.Component { - prepConfig = (alignedFrame: DataFrame, getTimeRange: () => TimeRange) => { - const { eventBus } = this.context; - const { theme, timeZone, orientation, barWidth, showValue, groupWidth, stacking, legend, tooltip } = this.props; +export const BarChart: React.FC = (props) => { + const theme = useTheme2(); + const { eventBus } = usePanelContext(); + + const renderLegend = (config: UPlotConfigBuilder) => { + if (!config || props.legend.displayMode === LegendDisplayMode.Hidden) { + return null; + } + + return ; + }; + + const prepConfig = (alignedFrame: DataFrame, getTimeRange: () => TimeRange) => { + const { timeZone, orientation, barWidth, showValue, groupWidth, stacking, legend, tooltip } = props; return preparePlotConfigBuilder({ frame: alignedFrame, getTimeRange, @@ -33,38 +51,16 @@ class UnthemedBarChart extends React.Component { }); }; - renderLegend = (config: UPlotConfigBuilder) => { - const { legend, onLegendClick, frames } = this.props; - - if (!config || legend.displayMode === LegendDisplayMode.Hidden) { - return; - } - - return ( - - ); - }; - - render() { - return ( - - ); - } -} - -export const BarChart = withTheme2(UnthemedBarChart); + return ( + + ); +}; BarChart.displayName = 'BarChart'; diff --git a/public/app/plugins/panel/barchart/BarChartPanel.tsx b/public/app/plugins/panel/barchart/BarChartPanel.tsx index e6e0c3a24db..885e07ad6f9 100755 --- a/public/app/plugins/panel/barchart/BarChartPanel.tsx +++ b/public/app/plugins/panel/barchart/BarChartPanel.tsx @@ -1,7 +1,6 @@ -import React, { useCallback, useMemo } from 'react'; +import React, { useMemo } from 'react'; import { FieldType, PanelProps, TimeRange, VizOrientation } from '@grafana/data'; -import { GraphNGLegendEvent, TooltipPlugin } from '@grafana/ui'; -import { hideSeriesConfigFactory } from '../timeseries/overrides/hideSeriesConfigFactory'; +import { TooltipPlugin } from '@grafana/ui'; import { BarChartOptions } from './types'; import { BarChart } from './BarChart'; @@ -10,15 +9,7 @@ interface Props extends PanelProps {} /** * @alpha */ -export const BarChartPanel: React.FunctionComponent = ({ - data, - options, - width, - height, - fieldConfig, - timeZone, - onFieldConfigChange, -}) => { +export const BarChartPanel: React.FunctionComponent = ({ data, options, width, height, timeZone }) => { const orientation = useMemo(() => { if (!options.orientation || options.orientation === VizOrientation.Auto) { return width < height ? VizOrientation.Horizontal : VizOrientation.Vertical; @@ -27,13 +18,6 @@ export const BarChartPanel: React.FunctionComponent = ({ return options.orientation; }, [width, height, options.orientation]); - const onLegendClick = useCallback( - (event: GraphNGLegendEvent) => { - onFieldConfigChange(hideSeriesConfigFactory(event, fieldConfig, data.series)); - }, - [fieldConfig, onFieldConfigChange, data.series] - ); - if (!data || !data.series?.length) { return (
@@ -66,7 +50,6 @@ export const BarChartPanel: React.FunctionComponent = ({ structureRev={data.structureRev} width={width} height={height} - onLegendClick={onLegendClick} {...options} orientation={orientation} > diff --git a/public/app/plugins/panel/barchart/utils.ts b/public/app/plugins/panel/barchart/utils.ts index 60c1ff6f907..b5a65f088f7 100644 --- a/public/app/plugins/panel/barchart/utils.ts +++ b/public/app/plugins/panel/barchart/utils.ts @@ -18,8 +18,8 @@ import { ScaleDistribution, ScaleOrientation, UPlotConfigBuilder, + UPlotConfigPrepFn, } from '@grafana/ui'; -import { UPlotConfigPrepFn } from '@grafana/ui/src/components/uPlot/config/UPlotConfigBuilder'; /** @alpha */ function getBarCharScaleOrientation(orientation: VizOrientation) { diff --git a/public/app/plugins/panel/timeline/TimelineChart.tsx b/public/app/plugins/panel/timeline/TimelineChart.tsx index 6d8cca1fa9e..2af6f15aac1 100755 --- a/public/app/plugins/panel/timeline/TimelineChart.tsx +++ b/public/app/plugins/panel/timeline/TimelineChart.tsx @@ -1,12 +1,5 @@ import React from 'react'; -import { - PanelContext, - PanelContextRoot, - UPlotConfigBuilder, - GraphNG, - GraphNGProps, - BarValueVisibility, -} from '@grafana/ui'; +import { PanelContext, PanelContextRoot, GraphNG, GraphNGProps, BarValueVisibility } from '@grafana/ui'; import { DataFrame, FieldType, TimeRange } from '@grafana/data'; import { preparePlotConfigBuilder } from './utils'; import { TimelineMode } from './types'; @@ -39,9 +32,7 @@ export class TimelineChart extends React.Component { }); }; - renderLegend = (config: UPlotConfigBuilder) => { - return; - }; + renderLegend = () => null; render() { return ( @@ -53,7 +44,7 @@ export class TimelineChart extends React.Component { }} prepConfig={this.prepConfig} propsToDiff={propsToDiff} - renderLegend={this.renderLegend as any} + renderLegend={this.renderLegend} /> ); } diff --git a/public/app/plugins/panel/timeline/TimelinePanel.tsx b/public/app/plugins/panel/timeline/TimelinePanel.tsx index 931495c0718..5c8a0d72b4a 100755 --- a/public/app/plugins/panel/timeline/TimelinePanel.tsx +++ b/public/app/plugins/panel/timeline/TimelinePanel.tsx @@ -1,7 +1,6 @@ -import React, { useCallback } from 'react'; +import React from 'react'; import { PanelProps } from '@grafana/data'; -import { GraphNGLegendEvent, useTheme2 } from '@grafana/ui'; -import { hideSeriesConfigFactory } from '../timeseries/overrides/hideSeriesConfigFactory'; +import { useTheme2 } from '@grafana/ui'; import { TimelineOptions } from './types'; import { TimelineChart } from './TimelineChart'; @@ -10,25 +9,9 @@ interface TimelinePanelProps extends PanelProps {} /** * @alpha */ -export const TimelinePanel: React.FC = ({ - data, - timeRange, - timeZone, - options, - width, - height, - fieldConfig, - onFieldConfigChange, -}) => { +export const TimelinePanel: React.FC = ({ data, timeRange, timeZone, options, width, height }) => { const theme = useTheme2(); - const onLegendClick = useCallback( - (event: GraphNGLegendEvent) => { - onFieldConfigChange(hideSeriesConfigFactory(event, fieldConfig, data.series)); - }, - [fieldConfig, onFieldConfigChange, data.series] - ); - if (!data || !data.series?.length) { return (
@@ -46,7 +29,6 @@ export const TimelinePanel: React.FC = ({ timeZone={timeZone} width={width} height={height} - onLegendClick={onLegendClick} {...options} /> );