[XY] Fixes the scale type on a terms aggregation on a number field (#115729)

This commit is contained in:
Stratoula Kalafateli 2021-10-21 11:31:07 +03:00 committed by GitHub
parent 5499bd168d
commit 1eb0f4aecd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 2 deletions

View file

@ -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');
});
});

View file

@ -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'],
@ -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
)