Skip to content

Commit

Permalink
feat: add nice prop on scale type
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Mar 29, 2021
1 parent fe47338 commit acee82b
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 169 deletions.
5 changes: 3 additions & 2 deletions src/chart_types/heatmap/layout/viewmodel/viewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ export function shapeViewModel(
let xValues = xDomain.domain as any[];

const timeScale =
xDomain.scaleType === ScaleType.Time
xDomain.scaleConfig.type === ScaleType.Time
? new ScaleContinuous(
{
type: ScaleType.Time,
domain: xDomain.domain,
range: [0, chartDimensions.width],
nice: false,
},
{
ticks: getTicks(chartDimensions.width, config.xAxisLabel),
Expand Down Expand Up @@ -313,7 +314,7 @@ export function shapeViewModel(
* @param y
*/
const pickHighlightedArea: PickHighlightedArea = (x: Array<string | number>, y: Array<string | number>) => {
if (xDomain.scaleType !== ScaleType.Time) {
if (xDomain.scaleConfig.type !== ScaleType.Time) {
return null;
}
const [startValue, endValue] = x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { getHeatmapTableSelector } from './get_heatmap_table';
export const getXAxisRightOverflow = createCachedSelector(
[getHeatmapConfigSelector, getHeatmapTableSelector],
({ xAxisLabel: { fontSize, fontFamily, padding, formatter, width }, timeZone }, { xDomain }): number => {
if (xDomain.scaleType !== ScaleType.Time) {
if (xDomain.scaleConfig.type !== ScaleType.Time) {
return 0;
}
if (typeof width === 'number') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ChartTypes } from '../..';
import { ScaleType } from '../../../scales/constants';
import { SpecTypes } from '../../../specs/constants';
import { Dimensions } from '../../../utils/dimensions';
import { getXScaleConfig } from '../scales/get_scale_config';
import { computeSeriesDomains } from '../state/utils/utils';
import { computeXScale } from '../utils/scales';
import { BasicSeriesSpec, SeriesTypes } from '../utils/specs';
Expand Down Expand Up @@ -1460,7 +1461,7 @@ describe('Crosshair utils linear scale', () => {
domain: [0.5, 3.5],
isBandScale: true,
minInterval: 1,
scaleType: ScaleType.Linear,
scaleConfig: getXScaleConfig(ScaleType.Linear),
type: 'xDomain',
},
totalBarsInCluster: 1,
Expand Down Expand Up @@ -1491,7 +1492,7 @@ describe('Crosshair utils linear scale', () => {
domain: [-0.5, 2.5],
isBandScale: true,
minInterval: 1,
scaleType: ScaleType.Linear,
scaleConfig: getXScaleConfig(ScaleType.Linear),
type: 'xDomain',
},
totalBarsInCluster: barSeries.length,
Expand Down Expand Up @@ -1522,7 +1523,7 @@ describe('Crosshair utils linear scale', () => {
domain: [0.5, 3.5],
isBandScale: true,
minInterval: 1,
scaleType: ScaleType.Linear,
scaleConfig: getXScaleConfig(ScaleType.Linear),
type: 'xDomain',
},
totalBarsInCluster: 1,
Expand Down Expand Up @@ -1553,7 +1554,7 @@ describe('Crosshair utils linear scale', () => {
domain: [-0.5, 2.5],
isBandScale: true,
minInterval: 1,
scaleType: ScaleType.Linear,
scaleConfig: getXScaleConfig(ScaleType.Linear),
type: 'xDomain',
},
totalBarsInCluster: barSeries.length,
Expand Down
23 changes: 23 additions & 0 deletions src/chart_types/xy_chart/domains/nice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/** @internal */
export function areAllNiceDomain(nice: Array<boolean>) {
return nice.length > 0 && nice.every((d) => d);
}
41 changes: 18 additions & 23 deletions src/chart_types/xy_chart/domains/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,30 @@
*/

import { ScaleContinuousType } from '../../../scales';
import { ScaleType } from '../../../scales/constants';
import { LogScaleOptions } from '../../../scales/scale_continuous';
import { OrdinalDomain, ContinuousDomain } from '../../../utils/domain';
import { GroupId } from '../../../utils/ids';
import { ScaleConfig } from '../scales/get_scale_config';
import { XScaleType } from '../utils/specs';

/** @internal */
export interface BaseDomain {
scaleType: typeof ScaleType.Ordinal | ScaleContinuousType;
export type XDomain = Pick<LogScaleOptions, 'logBase'> & {
type: 'xDomain';
scaleConfig: ScaleConfig<XScaleType>;
/* if the scale needs to be a band scale: used when displaying bars */
isBandScale: boolean;
}
/* the minimum interval of the scale if not-ordinal band-scale */
minInterval: number;
/** if x domain is time, we should also specify the timezone */
timeZone?: string;
domain: OrdinalDomain | ContinuousDomain;
};

/** @internal */
export type XDomain = BaseDomain &
Pick<LogScaleOptions, 'logBase'> & {
type: 'xDomain';
/* the minimum interval of the scale if not-ordinal band-scale */
minInterval: number;
/** if x domain is time, we should also specify the timezone */
timeZone?: string;
domain: OrdinalDomain | ContinuousDomain;
};

/** @internal */
export type YDomain = BaseDomain &
LogScaleOptions & {
type: 'yDomain';
isBandScale: false;
scaleType: ScaleContinuousType;
groupId: GroupId;
domain: ContinuousDomain;
};
export type YDomain = LogScaleOptions & {
type: 'yDomain';
scaleConfig: ScaleConfig<ScaleContinuousType>;
isBandScale: false;
groupId: GroupId;
domain: ContinuousDomain;
};
33 changes: 14 additions & 19 deletions src/chart_types/xy_chart/domains/x_domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { MockSeriesSpecs } from '../../../mocks/specs';
import { ScaleType } from '../../../scales/constants';
import { SpecTypes, Direction, BinAgg } from '../../../specs/constants';
import { Logger } from '../../../utils/logger';
import { getXScaleConfig } from '../scales/get_scale_config';
import { getDataSeriesFromSpecs } from '../utils/series';
import { BasicSeriesSpec, SeriesTypes } from '../utils/specs';
import { convertXScaleTypes, findMinInterval, mergeXDomain } from './x_domain';
Expand All @@ -33,16 +34,10 @@ jest.mock('../../../utils/logger', () => ({
}));

describe('X Domain', () => {
test('Should return null when missing specs or specs types', () => {
test('Should return a default scale when missing specs or specs types', () => {
const seriesSpecs: BasicSeriesSpec[] = [];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toBe(null);
});

test('should throw if we miss calling merge X domain without specs configured', () => {
expect(() => {
mergeXDomain([], new Set());
}).toThrow();
expect(mainXScale).not.toBeNull();
});

test('Should return correct scale type with single bar', () => {
Expand All @@ -54,7 +49,7 @@ describe('X Domain', () => {
];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toEqual({
scaleType: ScaleType.Linear,
scaleConfig: getXScaleConfig(ScaleType.Linear),
isBandScale: true,
});
});
Expand All @@ -68,7 +63,7 @@ describe('X Domain', () => {
];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toEqual({
scaleType: ScaleType.Ordinal,
scaleConfig: getXScaleConfig(ScaleType.Ordinal),
isBandScale: true,
});
});
Expand All @@ -82,7 +77,7 @@ describe('X Domain', () => {
];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toEqual({
scaleType: ScaleType.Linear,
scaleConfig: getXScaleConfig(ScaleType.Linear),
isBandScale: false,
});
});
Expand All @@ -96,7 +91,7 @@ describe('X Domain', () => {
];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toEqual({
scaleType: ScaleType.Time,
scaleConfig: getXScaleConfig(ScaleType.Time),
isBandScale: false,
timeZone: 'utc-3',
});
Expand All @@ -116,7 +111,7 @@ describe('X Domain', () => {
];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toEqual({
scaleType: ScaleType.Time,
scaleConfig: getXScaleConfig(ScaleType.Time),
isBandScale: false,
timeZone: 'utc-3',
});
Expand All @@ -136,7 +131,7 @@ describe('X Domain', () => {
];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toEqual({
scaleType: ScaleType.Time,
scaleConfig: getXScaleConfig(ScaleType.Time),
isBandScale: false,
timeZone: 'utc',
});
Expand All @@ -155,7 +150,7 @@ describe('X Domain', () => {
];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toEqual({
scaleType: ScaleType.Ordinal,
scaleConfig: getXScaleConfig(ScaleType.Ordinal),
isBandScale: false,
});
});
Expand All @@ -172,7 +167,7 @@ describe('X Domain', () => {
];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toEqual({
scaleType: ScaleType.Ordinal,
scaleConfig: getXScaleConfig(ScaleType.Ordinal),
isBandScale: true,
});
});
Expand All @@ -190,7 +185,7 @@ describe('X Domain', () => {
];
const mainXScale = convertXScaleTypes(seriesSpecs);
expect(mainXScale).toEqual({
scaleType: ScaleType.Linear,
scaleConfig: getXScaleConfig(ScaleType.Linear),
isBandScale: true,
});
});
Expand Down Expand Up @@ -497,14 +492,14 @@ describe('X Domain', () => {
],
xValues,
customDomain,
ScaleType.Ordinal,
{ type: ScaleType.Ordinal, nice: false },
);

expect(getResult).not.toThrow();

const mergedDomain = getResult();
expect(mergedDomain.domain).toEqual([0, 'a', 2, 5, 7]);
expect(mergedDomain.scaleType).toEqual(ScaleType.Ordinal);
expect(mergedDomain.scaleConfig.type).toEqual(ScaleType.Ordinal);
});

test('Should merge multi bar/line ordinal series correctly', () => {
Expand Down
Loading

0 comments on commit acee82b

Please sign in to comment.