mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
TimeSeries: Implement auto decimals for y axes (#52912)
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
||||
getFieldDisplayName,
|
||||
getDisplayProcessor,
|
||||
FieldColorModeId,
|
||||
DecimalCount,
|
||||
} from '@grafana/data';
|
||||
import {
|
||||
AxisPlacement,
|
||||
@@ -35,7 +36,7 @@ import { UPlotConfigBuilder, UPlotConfigPrepFn } from '../uPlot/config/UPlotConf
|
||||
import { getScaleGradientFn } from '../uPlot/config/gradientFills';
|
||||
import { getStackingGroups, preparePlotData2 } from '../uPlot/utils';
|
||||
|
||||
const defaultFormatter = (v: any) => (v == null ? '-' : v.toFixed(1));
|
||||
const defaultFormatter = (v: any, decimals: DecimalCount = 1) => (v == null ? '-' : v.toFixed(decimals));
|
||||
|
||||
const defaultConfig: GraphFieldConfig = {
|
||||
drawStyle: GraphDrawStyle.Line,
|
||||
@@ -268,7 +269,7 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<{
|
||||
label: customConfig.axisLabel,
|
||||
size: customConfig.axisWidth,
|
||||
placement: customConfig.axisPlacement ?? AxisPlacement.Auto,
|
||||
formatValue: (v) => formattedValueToString(fmt(v)),
|
||||
formatValue: (v, decimals) => formattedValueToString(fmt(v, config.decimals ?? decimals)),
|
||||
theme,
|
||||
grid: { show: customConfig.axisGridShow },
|
||||
show: customConfig.hideFrom?.viz === false,
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
import uPlot, { Axis } from 'uplot';
|
||||
|
||||
import { dateTimeFormat, GrafanaTheme2, isBooleanUnit, systemDateFormats, TimeZone } from '@grafana/data';
|
||||
import {
|
||||
dateTimeFormat,
|
||||
DecimalCount,
|
||||
GrafanaTheme2,
|
||||
guessDecimals,
|
||||
isBooleanUnit,
|
||||
roundDecimals,
|
||||
systemDateFormats,
|
||||
TimeZone,
|
||||
} from '@grafana/data';
|
||||
import { AxisPlacement } from '@grafana/schema';
|
||||
|
||||
import { measureText } from '../../../utils/measureText';
|
||||
@@ -21,7 +30,7 @@ export interface AxisProps {
|
||||
ticks?: Axis.Ticks;
|
||||
filter?: Axis.Filter;
|
||||
space?: Axis.Space;
|
||||
formatValue?: (v: any) => string;
|
||||
formatValue?: (v: any, decimals?: DecimalCount) => string;
|
||||
incrs?: Axis.Incrs;
|
||||
splits?: Axis.Splits;
|
||||
values?: Axis.Values;
|
||||
@@ -176,7 +185,10 @@ export class UPlotAxisBuilder extends PlotConfigBuilder<AxisProps, Axis> {
|
||||
} else if (isTime) {
|
||||
config.values = formatTime;
|
||||
} else if (formatValue) {
|
||||
config.values = (u: uPlot, vals: any[]) => vals.map(formatValue!);
|
||||
config.values = (u: uPlot, splits, axisIdx, tickSpace, tickIncr) => {
|
||||
let decimals = guessDecimals(roundDecimals(tickIncr, 6));
|
||||
return splits.map((v) => formatValue!(v, decimals > 0 ? decimals : undefined));
|
||||
};
|
||||
}
|
||||
|
||||
// store timezone
|
||||
|
||||
Reference in New Issue
Block a user