Commit ed69edbb by Torkel Ödegaard Committed by GitHub

GraphNG: Hide grid for right-y axis if left x-axis exists (#30195)

* GraphNG: Hide grid for right-y axis

* Correct fix so it works if you just a right x-axis

* Removed import
parent 50b649a8
......@@ -4,6 +4,7 @@ import { UPlotConfigBuilder } from './UPlotConfigBuilder';
import { GrafanaTheme } from '@grafana/data';
import { expect } from '../../../../../../public/test/lib/common';
import { FillGradientMode, AxisPlacement, DrawStyle, PointVisibility, ScaleDistribution } from '../config';
import darkTheme from '../../../themes/dark';
describe('UPlotConfigBuilder', () => {
describe('default config', () => {
......@@ -34,17 +35,21 @@ describe('UPlotConfigBuilder', () => {
`);
});
});
describe('scales config', () => {
it('allows scales configuration', () => {
const builder = new UPlotConfigBuilder();
builder.addScale({
scaleKey: 'scale-x',
isTime: true,
});
builder.addScale({
scaleKey: 'scale-y',
isTime: false,
});
expect(builder.getConfig()).toMatchInlineSnapshot(`
Object {
"axes": Array [],
......@@ -85,10 +90,12 @@ describe('UPlotConfigBuilder', () => {
it('prevents duplicate scales', () => {
const builder = new UPlotConfigBuilder();
builder.addScale({
scaleKey: 'scale-x',
isTime: true,
});
builder.addScale({
scaleKey: 'scale-x',
isTime: false,
......@@ -229,6 +236,7 @@ describe('UPlotConfigBuilder', () => {
it('allows axes configuration', () => {
const builder = new UPlotConfigBuilder();
builder.addAxis({
scaleKey: 'scale-x',
label: 'test label',
......@@ -294,19 +302,22 @@ describe('UPlotConfigBuilder', () => {
it('Handles auto axis placement', () => {
const builder = new UPlotConfigBuilder();
builder.addAxis({
scaleKey: 'y1',
placement: AxisPlacement.Auto,
theme: { isDark: true, palette: { gray25: '#ffffff' } } as GrafanaTheme,
theme: darkTheme,
});
builder.addAxis({
scaleKey: 'y2',
placement: AxisPlacement.Auto,
theme: { isDark: true, palette: { gray25: '#ffffff' } } as GrafanaTheme,
theme: darkTheme,
});
expect(builder.getAxisPlacement('y1')).toBe(AxisPlacement.Left);
expect(builder.getAxisPlacement('y2')).toBe(AxisPlacement.Right);
expect(builder.getConfig().axes![1].grid!.show).toBe(false);
});
it('When fillColor is not set fill', () => {
......
......@@ -11,8 +11,8 @@ export class UPlotConfigBuilder {
private axes: Record<string, UPlotAxisBuilder> = {};
private scales: UPlotScaleBuilder[] = [];
private cursor: Cursor | undefined;
hasLeftAxis = false;
private hasLeftAxis = false;
private hasBottomAxis = false;
addAxis(props: AxisProps) {
props.placement = props.placement ?? AxisPlacement.Auto;
......@@ -27,8 +27,13 @@ export class UPlotConfigBuilder {
props.placement = this.hasLeftAxis ? AxisPlacement.Right : AxisPlacement.Left;
}
if (props.placement === AxisPlacement.Left) {
this.hasLeftAxis = true;
switch (props.placement) {
case AxisPlacement.Left:
this.hasLeftAxis = true;
break;
case AxisPlacement.Bottom:
this.hasBottomAxis = true;
break;
}
if (props.placement === AxisPlacement.Hidden) {
......@@ -64,7 +69,7 @@ export class UPlotConfigBuilder {
getConfig() {
const config: PlotSeriesConfig = { series: [{}] };
config.axes = Object.values(this.axes).map(a => a.getConfig());
config.axes = this.ensureNonOverlappingAxes(Object.values(this.axes)).map(a => a.getConfig());
config.series = [...config.series, ...this.series.map(s => s.getConfig())];
config.scales = this.scales.reduce((acc, s) => {
return { ...acc, ...s.getConfig() };
......@@ -94,4 +99,17 @@ export class UPlotConfigBuilder {
return config;
}
private ensureNonOverlappingAxes(axes: UPlotAxisBuilder[]): UPlotAxisBuilder[] {
for (const axis of axes) {
if (axis.props.placement === AxisPlacement.Right && this.hasLeftAxis) {
axis.props.grid = false;
}
if (axis.props.placement === AxisPlacement.Top && this.hasBottomAxis) {
axis.props.grid = false;
}
}
return axes;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment