Skip to content

Commit

Permalink
[XY] Fixes the scale type on a terms aggregation on a number field (#…
Browse files Browse the repository at this point in the history
…115729) (#115894)

Co-authored-by: Stratoula Kalafateli <[email protected]>
  • Loading branch information
kibanamachine and stratoula authored Oct 21, 2021
1 parent 9e7cc15 commit 6ed83ae
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
78 changes: 78 additions & 0 deletions src/plugins/vis_types/xy/public/config/get_axis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { getScale } from './get_axis';
import type { Scale } from '../types';

describe('getScale', () => {
const axisScale = {
type: 'linear',
mode: 'normal',
scaleType: 'linear',
} as Scale;

it('returns linear type for a number', () => {
const format = { id: 'number' };
const scale = getScale(axisScale, {}, format, true);
expect(scale.type).toBe('linear');
});

it('returns ordinal type for a terms aggregation on a number field', () => {
const format = {
id: 'terms',
params: {
id: 'number',
otherBucketLabel: 'Other',
missingBucketLabel: 'Missing',
},
};
const scale = getScale(axisScale, {}, format, true);
expect(scale.type).toBe('ordinal');
});

it('returns ordinal type for a terms aggregation on a string field', () => {
const format = {
id: 'terms',
params: {
id: 'string',
otherBucketLabel: 'Other',
missingBucketLabel: 'Missing',
},
};
const scale = getScale(axisScale, {}, format, true);
expect(scale.type).toBe('ordinal');
});

it('returns ordinal type for a range aggregation on a number field', () => {
const format = {
id: 'range',
params: {
id: 'number',
},
};
const scale = getScale(axisScale, {}, format, true);
expect(scale.type).toBe('ordinal');
});

it('returns time type for a date histogram aggregation', () => {
const format = {
id: 'date',
params: {
pattern: 'HH:mm',
},
};
const scale = getScale(axisScale, { date: true }, format, true);
expect(scale.type).toBe('time');
});

it('returns linear type for an histogram aggregation', () => {
const format = { id: 'number' };
const scale = getScale(axisScale, { interval: 1 }, format, true);
expect(scale.type).toBe('linear');
});
});
7 changes: 5 additions & 2 deletions src/plugins/vis_types/xy/public/config/get_axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function getScaleType(
return type;
}

function getScale<S extends XScaleType | YScaleType>(
export function getScale<S extends XScaleType | YScaleType>(
scale: Scale,
params: Aspect['params'],
format: Aspect['format'],
Expand All @@ -130,7 +130,10 @@ function getScale<S extends XScaleType | YScaleType>(
isCategoryAxis
? getScaleType(
scale,
format?.id === 'number' || (format?.params?.id === 'number' && format?.id !== 'range'),
format?.id === 'number' ||
(format?.params?.id === 'number' &&
format?.id !== BUCKET_TYPES.RANGE &&
format?.id !== BUCKET_TYPES.TERMS),
'date' in params,
'interval' in params
)
Expand Down

0 comments on commit 6ed83ae

Please sign in to comment.