mirror of
https://github.com/grafana/grafana.git
synced 2026-07-30 00:08:10 -05:00
Legend: Use correct unit for percent and count calculations (#49004)
This commit is contained in:
@@ -2,11 +2,14 @@ import React from 'react';
|
||||
|
||||
import {
|
||||
DataFrame,
|
||||
DisplayProcessor,
|
||||
DisplayValue,
|
||||
fieldReducers,
|
||||
getDisplayProcessor,
|
||||
getFieldDisplayName,
|
||||
getFieldSeriesColor,
|
||||
reduceField,
|
||||
ReducerID,
|
||||
} from '@grafana/data';
|
||||
import { VizLegendOptions, AxisPlacement } from '@grafana/schema';
|
||||
|
||||
@@ -66,6 +69,8 @@ export const PlotLegend: React.FC<PlotLegendProps> = ({
|
||||
}
|
||||
|
||||
const fmt = field.display ?? defaultFormatter;
|
||||
let countFormatter: DisplayProcessor | null = null;
|
||||
|
||||
const fieldCalcs = reduceField({
|
||||
field,
|
||||
reducers: calcs,
|
||||
@@ -73,9 +78,43 @@ export const PlotLegend: React.FC<PlotLegendProps> = ({
|
||||
|
||||
return calcs.map<DisplayValue>((reducerId) => {
|
||||
const fieldReducer = fieldReducers.get(reducerId);
|
||||
let formatter = fmt;
|
||||
|
||||
if (fieldReducer.id === ReducerID.diffperc) {
|
||||
formatter = getDisplayProcessor({
|
||||
field: {
|
||||
...field,
|
||||
config: {
|
||||
...field.config,
|
||||
unit: 'percent',
|
||||
},
|
||||
},
|
||||
theme,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
fieldReducer.id === ReducerID.count ||
|
||||
fieldReducer.id === ReducerID.changeCount ||
|
||||
fieldReducer.id === ReducerID.distinctCount
|
||||
) {
|
||||
if (!countFormatter) {
|
||||
countFormatter = getDisplayProcessor({
|
||||
field: {
|
||||
...field,
|
||||
config: {
|
||||
...field.config,
|
||||
unit: 'none',
|
||||
},
|
||||
},
|
||||
theme,
|
||||
});
|
||||
}
|
||||
formatter = countFormatter;
|
||||
}
|
||||
|
||||
return {
|
||||
...fmt(fieldCalcs[reducerId]),
|
||||
...formatter(fieldCalcs[reducerId]),
|
||||
title: fieldReducer.name,
|
||||
description: fieldReducer.description,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user