mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
BarChart: add negY transform to fieldConfig overrides (#55930)
This commit is contained in:
parent
fc09d5a8a7
commit
16c9c858b8
@ -57,6 +57,7 @@ export interface BarsOptions {
|
||||
legend?: VizLegendOptions;
|
||||
xSpacing?: number;
|
||||
xTimeAuto?: boolean;
|
||||
negY?: boolean[];
|
||||
}
|
||||
|
||||
/**
|
||||
@ -352,6 +353,10 @@ export function getConfig(opts: BarsOptions, theme: GrafanaTheme2) {
|
||||
let middleShift = isXHorizontal ? 0 : -Math.round(MIDDLE_BASELINE_SHIFT * fontSize);
|
||||
let value = rawValue(seriesIdx, dataIdx);
|
||||
|
||||
if (opts.negY?.[seriesIdx] && value != null) {
|
||||
value *= -1;
|
||||
}
|
||||
|
||||
if (value != null) {
|
||||
// Calculate final co-ordinates for text position
|
||||
const x =
|
||||
@ -380,7 +385,7 @@ export function getConfig(opts: BarsOptions, theme: GrafanaTheme2) {
|
||||
// Adjust for baseline which is "top" in this case
|
||||
xAdjust = (textMetrics.width * scaleFactor) / 2;
|
||||
|
||||
// yAdjust only matters when when the value isn't negative
|
||||
// yAdjust only matters when the value isn't negative
|
||||
yAdjust =
|
||||
value > 0
|
||||
? (textMetrics.actualBoundingBoxAscent + textMetrics.actualBoundingBoxDescent) * scaleFactor
|
||||
@ -516,7 +521,12 @@ export function getConfig(opts: BarsOptions, theme: GrafanaTheme2) {
|
||||
|
||||
for (const sidx in labels[didx]) {
|
||||
const label = labels[didx][sidx];
|
||||
const { text, value, x = 0, y = 0 } = label;
|
||||
const { text, x = 0, y = 0 } = label;
|
||||
let { value } = label;
|
||||
|
||||
if (opts.negY?.[sidx] && value != null) {
|
||||
value *= -1;
|
||||
}
|
||||
|
||||
let align: CanvasTextAlign = isXHorizontal ? 'center' : value !== null && value < 0 ? 'right' : 'left';
|
||||
let baseline: CanvasTextBaseline = isXHorizontal
|
||||
|
@ -8,7 +8,7 @@ import {
|
||||
VizOrientation,
|
||||
} from '@grafana/data';
|
||||
import { config } from '@grafana/runtime';
|
||||
import { StackingMode, VisibilityMode } from '@grafana/schema';
|
||||
import { GraphTransform, StackingMode, VisibilityMode } from '@grafana/schema';
|
||||
import { graphFieldOptions, commonOptionsBuilder } from '@grafana/ui';
|
||||
|
||||
import { BarChartPanel } from './BarChartPanel';
|
||||
@ -63,6 +63,28 @@ export const plugin = new PanelPlugin<PanelOptions, PanelFieldConfig>(BarChartPa
|
||||
},
|
||||
});
|
||||
|
||||
builder.addSelect({
|
||||
category: ['Graph styles'],
|
||||
name: 'Transform',
|
||||
path: 'transform',
|
||||
settings: {
|
||||
options: [
|
||||
{
|
||||
label: 'Constant',
|
||||
value: GraphTransform.Constant,
|
||||
description: 'The first value will be shown as a constant line',
|
||||
},
|
||||
{
|
||||
label: 'Negative Y',
|
||||
value: GraphTransform.NegativeY,
|
||||
description: 'Flip the results to negative values on the y axis',
|
||||
},
|
||||
],
|
||||
isClearable: true,
|
||||
},
|
||||
hideFromDefaults: true,
|
||||
});
|
||||
|
||||
commonOptionsBuilder.addAxisConfig(builder, cfg, false);
|
||||
commonOptionsBuilder.addHideFrom(builder);
|
||||
},
|
||||
|
@ -18,6 +18,7 @@ import {
|
||||
import { maybeSortFrame } from '@grafana/data/src/transformations/transformers/joinDataFrames';
|
||||
import {
|
||||
AxisPlacement,
|
||||
GraphTransform,
|
||||
ScaleDirection,
|
||||
ScaleDistribution,
|
||||
ScaleOrientation,
|
||||
@ -107,6 +108,7 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<BarChartOptionsEX> = ({
|
||||
legend,
|
||||
xSpacing: xTickLabelSpacing,
|
||||
xTimeAuto: frame.fields[0]?.type === FieldType.time && !frame.fields[0].config.unit?.startsWith('time:'),
|
||||
negY: frame.fields.map((f) => f.config.custom?.transform === GraphTransform.NegativeY),
|
||||
};
|
||||
|
||||
const config = getConfig(opts, theme);
|
||||
|
Loading…
Reference in New Issue
Block a user