diff --git a/packages/grafana-ui/src/components/TimeSeries/TimeSeries.tsx b/packages/grafana-ui/src/components/TimeSeries/TimeSeries.tsx index 2be4cca998f..9e5eee1d7ff 100644 --- a/packages/grafana-ui/src/components/TimeSeries/TimeSeries.tsx +++ b/packages/grafana-ui/src/components/TimeSeries/TimeSeries.tsx @@ -5,7 +5,7 @@ import { DataFrame, TimeRange } from '@grafana/data'; import { withTheme2 } from '../../themes/ThemeContext'; import { GraphNG, GraphNGProps, PropDiffFn } from '../GraphNG/GraphNG'; import { PanelContext, PanelContextRoot } from '../PanelChrome/PanelContext'; -import { PlotLegend } from '../uPlot/PlotLegend'; +import { hasVisibleLegendSeries, PlotLegend } from '../uPlot/PlotLegend'; import { UPlotConfigBuilder } from '../uPlot/config/UPlotConfigBuilder'; import { preparePlotConfigBuilder } from './utils'; @@ -39,8 +39,7 @@ export class UnthemedTimeSeries extends Component { renderLegend = (config: UPlotConfigBuilder) => { const { legend, frames } = this.props; - //hides and shows the legend ON the uPlot graph - if (!config || (legend && !legend.showLegend)) { + if (!config || (legend && !legend.showLegend) || !hasVisibleLegendSeries(config, frames)) { return null; } diff --git a/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx b/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx index da976de76b8..327af272497 100644 --- a/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx +++ b/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx @@ -27,6 +27,29 @@ interface PlotLegendProps extends VizLegendOptions, Omit { + const fieldIndex = s.props.dataFrameFieldIndex; + + if (!fieldIndex) { + return false; + } + + const field = data[fieldIndex.frameIndex]?.fields[fieldIndex.fieldIndex]; + + if (!field || field.config.custom?.hideFrom?.legend) { + return false; + } + + return true; + }); +} + export const PlotLegend = React.memo( ({ data, config, placement, calcs, displayMode, ...vizLayoutLegendProps }: PlotLegendProps) => { const theme = useTheme2();