From b79ba4dc07cc8de2148d80a640e1b3ea62c954d4 Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Mon, 25 Nov 2024 13:26:47 -0600 Subject: [PATCH] fix Histogram --- .../src/components/uPlot/PlotLegend.tsx | 9 +++---- .../core/components/TimeSeries/TimeSeries.tsx | 2 +- .../app/plugins/panel/histogram/Histogram.tsx | 26 ++++++++++++------- .../panel/histogram/HistogramPanel.tsx | 1 - 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx b/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx index 67cccc070f4..28272928672 100644 --- a/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx +++ b/packages/grafana-ui/src/components/uPlot/PlotLegend.tsx @@ -1,6 +1,6 @@ import { memo } from 'react'; -import { DataFrame, getFieldDisplayName, getFieldSeriesColor } from '@grafana/data'; +import { DataFrame, getFieldSeriesColor } from '@grafana/data'; import { VizLegendOptions, AxisPlacement } from '@grafana/schema'; import { useTheme2 } from '../../themes'; @@ -12,7 +12,7 @@ import { UPlotConfigBuilder } from './config/UPlotConfigBuilder'; import { getDisplayValuesForCalcs } from './utils'; interface PlotLegendProps extends VizLegendOptions, Omit { - data: DataFrame[]; + frame: DataFrame; config: UPlotConfigBuilder; } @@ -40,13 +40,12 @@ export function hasVisibleLegendSeries(config: UPlotConfigBuilder, data: DataFra } export const PlotLegend = memo( - ({ data, config, placement, calcs, displayMode, ...vizLayoutLegendProps }: PlotLegendProps) => { + ({ frame, config, placement, calcs, displayMode, ...vizLayoutLegendProps }: PlotLegendProps) => { const theme = useTheme2(); - const alignedFrame = data[0]!; const cfgSeries = config.getSeries(); - const legendItems: VizLegendItem[] = alignedFrame.fields + const legendItems: VizLegendItem[] = frame.fields .map((field, i) => { if (i === 0 || field.config.custom?.hideFrom.legend) { return undefined; diff --git a/public/app/core/components/TimeSeries/TimeSeries.tsx b/public/app/core/components/TimeSeries/TimeSeries.tsx index ea26af31ebd..4482bdfcdd6 100644 --- a/public/app/core/components/TimeSeries/TimeSeries.tsx +++ b/public/app/core/components/TimeSeries/TimeSeries.tsx @@ -38,7 +38,7 @@ export class UnthemedTimeSeries extends Component { return null; } - return ; + return ; }; render() { diff --git a/public/app/plugins/panel/histogram/Histogram.tsx b/public/app/plugins/panel/histogram/Histogram.tsx index 794fb6a7212..c146e5ffd7e 100644 --- a/public/app/plugins/panel/histogram/Histogram.tsx +++ b/public/app/plugins/panel/histogram/Histogram.tsx @@ -45,7 +45,6 @@ export interface HistogramProps extends Themeable2 { height: number; structureRev?: number; // a number that will change when the frames[] structure changes legend: VizLegendOptions; - rawSeries?: DataFrame[]; children?: (builder: UPlotConfigBuilder, frame: DataFrame, xMinOnlyFrame: DataFrame) => React.ReactNode; } @@ -280,8 +279,11 @@ const preparePlotData = (builder: UPlotConfigBuilder, xMinOnlyFrame: DataFrame) }; interface State { - alignedData: AlignedData; + // includes fields hidden from viz + alignedFrameLegend: DataFrame; + // excludes fields hidden from viz alignedFrame: DataFrame; + alignedData: AlignedData; config?: UPlotConfigBuilder; xMinOnlyFrame: DataFrame; } @@ -299,24 +301,30 @@ export class Histogram extends React.Component { const xMinOnly = xMinOnlyFrame(alignedFrame); const alignedData = preparePlotData(config, xMinOnly); + let alignedFrameLegend = { + ...alignedFrame, + fields: alignedFrame.fields.filter((field) => field.name !== 'xMin' && field.name !== 'xMax'), + }; + + // console.log(alignedFrame.fields); + return { alignedFrame, + alignedFrameLegend, alignedData, config, xMinOnlyFrame: xMinOnly, }; } - renderLegend(config: UPlotConfigBuilder) { + renderLegend(config: UPlotConfigBuilder, alignedFrame: DataFrame) { const { legend } = this.props; if (!config || legend.showLegend === false) { return null; } - const frames = this.props.options.combine ? [this.props.alignedFrame] : this.props.rawSeries!; - - return ; + return ; } componentDidUpdate(prevProps: HistogramProps) { @@ -339,15 +347,15 @@ export class Histogram extends React.Component { } render() { - const { width, height, children, alignedFrame } = this.props; - const { config } = this.state; + const { width, height, children } = this.props; + const { config, alignedFrame, alignedFrameLegend } = this.state; if (!config) { return null; } return ( - + {(vizWidth: number, vizHeight: number) => ( {children ? children(config, alignedFrame, this.state.xMinOnlyFrame) : null} diff --git a/public/app/plugins/panel/histogram/HistogramPanel.tsx b/public/app/plugins/panel/histogram/HistogramPanel.tsx index 2c2805069a8..bd958ef5075 100644 --- a/public/app/plugins/panel/histogram/HistogramPanel.tsx +++ b/public/app/plugins/panel/histogram/HistogramPanel.tsx @@ -63,7 +63,6 @@ export const HistogramPanel = ({ data, options, width, height }: Props) => { options={options} theme={theme} legend={options.legend} - rawSeries={data.series} structureRev={data.structureRev} width={width} height={height}