Legend: Use correct unit for percent and count calculations (#49004)

This commit is contained in:
Dominik Prokop
2022-05-18 12:22:17 +02:00
committed by GitHub
parent b9e45fafe5
commit cdc6344a96
@@ -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,
};