Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeSeries: Add option for symmetrical y axes (align 0) #52555

Merged
merged 3 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/grafana-schema/src/schema/mudball.cue
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ AxisConfig: {
axisSoftMax?: number
axisGridShow?: bool
scaleDistribution?: ScaleDistributionConfig
axisCenteredZero?: bool
} @cuetsy(kind="interface")

// TODO docs
Expand Down
1 change: 1 addition & 0 deletions packages/grafana-schema/src/schema/mudball.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export interface ScaleDistributionConfig {
}

export interface AxisConfig {
axisCenteredZero?: boolean;
axisColorMode?: AxisColorMode;
axisGridShow?: boolean;
axisLabel?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/grafana-ui/src/components/TimeSeries/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<{
max: field.config.max,
softMin: customConfig.axisSoftMin,
softMax: customConfig.axisSoftMax,
centeredZero: customConfig.axisCenteredZero,
},
field
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ScaleProps {
orientation: ScaleOrientation;
direction: ScaleDirection;
log?: number;
centeredZero?: boolean;
}

export class UPlotScaleBuilder extends PlotConfigBuilder<ScaleProps, Scale> {
Expand All @@ -26,7 +27,18 @@ export class UPlotScaleBuilder extends PlotConfigBuilder<ScaleProps, Scale> {
}

getConfig(): Scale {
let { isTime, scaleKey, min: hardMin, max: hardMax, softMin, softMax, range, direction, orientation } = this.props;
let {
isTime,
scaleKey,
min: hardMin,
max: hardMax,
softMin,
softMax,
range,
direction,
orientation,
centeredZero,
} = this.props;
const distribution = !isTime
? {
distr:
Expand Down Expand Up @@ -68,6 +80,14 @@ export class UPlotScaleBuilder extends PlotConfigBuilder<ScaleProps, Scale> {
let minMax: uPlot.Range.MinMax = [dataMin, dataMax];

if (scale.distr === 1 || scale.distr === 2) {
if (centeredZero) {
let absMin = Math.abs(dataMin);
let absMax = Math.abs(dataMax);
let max = Math.max(absMin, absMax);
dataMin = -max;
dataMax = max;
}

// @ts-ignore here we may use hardMin / hardMax to make sure any extra padding is computed from a more accurate delta
minMax = uPlot.rangeNum(hardMinOnly ? hardMin : dataMin, hardMaxOnly ? hardMax : dataMax, rangeConfig);
} else if (scale.distr === 3) {
Expand Down
51 changes: 30 additions & 21 deletions packages/grafana-ui/src/options/builder/axis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function addAxisConfig(
hideScale?: boolean
) {
const category = ['Axis'];

// options for axis appearance
builder
.addRadio({
path: 'axisPlacement',
Expand Down Expand Up @@ -51,24 +53,6 @@ export function addAxisConfig(
},
showIf: (c) => c.axisPlacement !== AxisPlacement.Hidden,
})
.addNumberInput({
path: 'axisSoftMin',
name: 'Soft min',
defaultValue: defaultConfig.axisSoftMin,
category,
settings: {
placeholder: 'See: Standard options > Min',
},
})
.addNumberInput({
path: 'axisSoftMax',
name: 'Soft max',
defaultValue: defaultConfig.axisSoftMax,
category,
settings: {
placeholder: 'See: Standard options > Max',
},
})
.addRadio({
path: 'axisGridShow',
name: 'Show grid lines',
Expand All @@ -95,8 +79,9 @@ export function addAxisConfig(
},
});

if (!hideScale) {
builder.addCustomEditor<void, ScaleDistributionConfig>({
// options for scale range
builder
.addCustomEditor<void, ScaleDistributionConfig>({
id: 'scaleDistribution',
path: 'scaleDistribution',
name: 'Scale',
Expand All @@ -106,8 +91,32 @@ export function addAxisConfig(
defaultValue: { type: ScaleDistribution.Linear },
shouldApply: (f) => f.type === FieldType.number,
process: identityOverrideProcessor,
})
.addBooleanSwitch({
path: 'axisCenteredZero',
name: 'Centered zero',
category,
defaultValue: false,
showIf: (c) => c.scaleDistribution?.type !== ScaleDistribution.Log,
})
.addNumberInput({
path: 'axisSoftMin',
name: 'Soft min',
defaultValue: defaultConfig.axisSoftMin,
category,
settings: {
placeholder: 'See: Standard options > Min',
},
})
.addNumberInput({
path: 'axisSoftMax',
name: 'Soft max',
defaultValue: defaultConfig.axisSoftMax,
category,
settings: {
placeholder: 'See: Standard options > Max',
},
});
}
}

const DISTRIBUTION_OPTIONS: Array<SelectableValue<ScaleDistribution>> = [
Expand Down
1 change: 1 addition & 0 deletions public/app/plugins/panel/timeseries/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const defaultGraphConfig: GraphFieldConfig = {
group: 'A',
},
axisGridShow: true,
axisCenteredZero: false,
};

const categoryStyles = ['Graph styles'];
Expand Down