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:
@@ -57,6 +57,7 @@ export interface BarsOptions {
|
|||||||
legend?: VizLegendOptions;
|
legend?: VizLegendOptions;
|
||||||
xSpacing?: number;
|
xSpacing?: number;
|
||||||
xTimeAuto?: boolean;
|
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 middleShift = isXHorizontal ? 0 : -Math.round(MIDDLE_BASELINE_SHIFT * fontSize);
|
||||||
let value = rawValue(seriesIdx, dataIdx);
|
let value = rawValue(seriesIdx, dataIdx);
|
||||||
|
|
||||||
|
if (opts.negY?.[seriesIdx] && value != null) {
|
||||||
|
value *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
// Calculate final co-ordinates for text position
|
// Calculate final co-ordinates for text position
|
||||||
const x =
|
const x =
|
||||||
@@ -380,7 +385,7 @@ export function getConfig(opts: BarsOptions, theme: GrafanaTheme2) {
|
|||||||
// Adjust for baseline which is "top" in this case
|
// Adjust for baseline which is "top" in this case
|
||||||
xAdjust = (textMetrics.width * scaleFactor) / 2;
|
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 =
|
yAdjust =
|
||||||
value > 0
|
value > 0
|
||||||
? (textMetrics.actualBoundingBoxAscent + textMetrics.actualBoundingBoxDescent) * scaleFactor
|
? (textMetrics.actualBoundingBoxAscent + textMetrics.actualBoundingBoxDescent) * scaleFactor
|
||||||
@@ -516,7 +521,12 @@ export function getConfig(opts: BarsOptions, theme: GrafanaTheme2) {
|
|||||||
|
|
||||||
for (const sidx in labels[didx]) {
|
for (const sidx in labels[didx]) {
|
||||||
const label = labels[didx][sidx];
|
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 align: CanvasTextAlign = isXHorizontal ? 'center' : value !== null && value < 0 ? 'right' : 'left';
|
||||||
let baseline: CanvasTextBaseline = isXHorizontal
|
let baseline: CanvasTextBaseline = isXHorizontal
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
VizOrientation,
|
VizOrientation,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
import { config } from '@grafana/runtime';
|
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 { graphFieldOptions, commonOptionsBuilder } from '@grafana/ui';
|
||||||
|
|
||||||
import { BarChartPanel } from './BarChartPanel';
|
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.addAxisConfig(builder, cfg, false);
|
||||||
commonOptionsBuilder.addHideFrom(builder);
|
commonOptionsBuilder.addHideFrom(builder);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
import { maybeSortFrame } from '@grafana/data/src/transformations/transformers/joinDataFrames';
|
import { maybeSortFrame } from '@grafana/data/src/transformations/transformers/joinDataFrames';
|
||||||
import {
|
import {
|
||||||
AxisPlacement,
|
AxisPlacement,
|
||||||
|
GraphTransform,
|
||||||
ScaleDirection,
|
ScaleDirection,
|
||||||
ScaleDistribution,
|
ScaleDistribution,
|
||||||
ScaleOrientation,
|
ScaleOrientation,
|
||||||
@@ -107,6 +108,7 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<BarChartOptionsEX> = ({
|
|||||||
legend,
|
legend,
|
||||||
xSpacing: xTickLabelSpacing,
|
xSpacing: xTickLabelSpacing,
|
||||||
xTimeAuto: frame.fields[0]?.type === FieldType.time && !frame.fields[0].config.unit?.startsWith('time:'),
|
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);
|
const config = getConfig(opts, theme);
|
||||||
|
|||||||
Reference in New Issue
Block a user