Skip to content

Commit

Permalink
refactor: compute scale API config separately
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Mar 31, 2021
1 parent acee82b commit 521a15f
Show file tree
Hide file tree
Showing 35 changed files with 733 additions and 601 deletions.
2 changes: 1 addition & 1 deletion .playground/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import React from 'react';

import { Example } from '../stories/small_multiples/7_sunbursts';
import { Example } from '../stories/line/2_w_axis';

export class Playground extends React.Component {
render() {
Expand Down
4 changes: 2 additions & 2 deletions src/chart_types/heatmap/layout/viewmodel/viewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function shapeViewModel(
let xValues = xDomain.domain as any[];

const timeScale =
xDomain.scaleConfig.type === ScaleType.Time
xDomain.type === ScaleType.Time
? new ScaleContinuous(
{
type: ScaleType.Time,
Expand Down Expand Up @@ -314,7 +314,7 @@ export function shapeViewModel(
* @param y
*/
const pickHighlightedArea: PickHighlightedArea = (x: Array<string | number>, y: Array<string | number>) => {
if (xDomain.scaleConfig.type !== ScaleType.Time) {
if (xDomain.type !== ScaleType.Time) {
return null;
}
const [startValue, endValue] = x;
Expand Down
6 changes: 5 additions & 1 deletion src/chart_types/heatmap/state/selectors/get_heatmap_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getChartIdSelector } from '../../../../state/selectors/get_chart_id';
import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs';
import { getAccessorValue } from '../../../../utils/accessor';
import { mergeXDomain } from '../../../xy_chart/domains/x_domain';
import { getXAPIScale } from '../../../xy_chart/scales/get_api_scales';
import { HeatmapTable } from './compute_chart_dimensions';
import { getHeatmapSpecSelector } from './get_heatmap_spec';

Expand Down Expand Up @@ -72,7 +73,10 @@ export const getHeatmapTableSelector = createCachedSelector(
},
);

resultData.xDomain = mergeXDomain([{ xScaleType: spec.xScaleType }], resultData.xValues, xDomain);
resultData.xDomain = mergeXDomain(
{ ...getXAPIScale(spec.xScaleType), isBandScale: false, ticks: 10, customDomain: xDomain },
resultData.xValues,
);

// sort values by their predicates
if (spec.xScaleType === ScaleType.Ordinal) {
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.scaleConfig.type !== ScaleType.Time) {
if (xDomain.type !== ScaleType.Time) {
return 0;
}
if (typeof width === 'number') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
*/

import { ChartTypes } from '../..';
import { MockGlobalSpec } from '../../../mocks/specs/specs';
import { MockXDomain } from '../../../mocks/xy/domains';
import { ScaleType } from '../../../scales/constants';
import { SpecTypes } from '../../../specs/constants';
import { Dimensions } from '../../../utils/dimensions';
import { getXScaleConfig } from '../scales/get_scale_config';
import { getAPIScaleConfigs } from '../state/selectors/get_api_scale_configs';
import { computeSeriesDomains } from '../state/utils/utils';
import { computeXScale } from '../utils/scales';
import { BasicSeriesSpec, SeriesTypes } from '../utils/specs';
Expand Down Expand Up @@ -98,22 +100,35 @@ describe('Crosshair utils linear scale', () => {
yScaleType: ScaleType.Linear,
};

const domainGroup = new Map([['group1', { fit: true }]]);

const barSeries = [barSeries1];
const barSeriesDomains = computeSeriesDomains(barSeries, domainGroup);
const barSeriesDomains = computeSeriesDomains(
barSeries,
getAPIScaleConfigs([], barSeries, MockGlobalSpec.settings()),
);

const multiBarSeries = [barSeries1, barSeries2];
const multiBarSeriesDomains = computeSeriesDomains(multiBarSeries, domainGroup);
const multiBarSeriesDomains = computeSeriesDomains(
multiBarSeries,
getAPIScaleConfigs([], multiBarSeries, MockGlobalSpec.settings()),
);

const lineSeries = [lineSeries1];
const lineSeriesDomains = computeSeriesDomains(lineSeries, domainGroup);
const lineSeriesDomains = computeSeriesDomains(
lineSeries,
getAPIScaleConfigs([], lineSeries, MockGlobalSpec.settings()),
);

const multiLineSeries = [lineSeries1, lineSeries2];
const multiLineSeriesDomains = computeSeriesDomains(multiLineSeries, domainGroup);
const multiLineSeriesDomains = computeSeriesDomains(
multiLineSeries,
getAPIScaleConfigs([], multiLineSeries, MockGlobalSpec.settings()),
);

const mixedLinesBars = [lineSeries1, lineSeries2, barSeries1, barSeries2];
const mixedLinesBarsSeriesDomains = computeSeriesDomains(mixedLinesBars, domainGroup);
const mixedLinesBarsSeriesDomains = computeSeriesDomains(
mixedLinesBars,
getAPIScaleConfigs([], mixedLinesBars, MockGlobalSpec.settings()),
);

const barSeriesScale = computeXScale({
xDomain: barSeriesDomains.xDomain,
Expand Down Expand Up @@ -1457,13 +1472,11 @@ describe('Crosshair utils linear scale', () => {
const chartDimensions: Dimensions = { top: 0, left: 0, width: 120, height: 120 };
test('cursor at begin of domain', () => {
const barSeriesScaleLimited = computeXScale({
xDomain: {
xDomain: MockXDomain.fromScaleType(ScaleType.Linear, {
domain: [0.5, 3.5],
isBandScale: true,
minInterval: 1,
scaleConfig: getXScaleConfig(ScaleType.Linear),
type: 'xDomain',
},
}),
totalBarsInCluster: 1,
range: [0, 120],
});
Expand All @@ -1488,13 +1501,11 @@ describe('Crosshair utils linear scale', () => {
});
test('cursor at end of domain', () => {
const barSeriesScaleLimited = computeXScale({
xDomain: {
xDomain: MockXDomain.fromScaleType(ScaleType.Linear, {
domain: [-0.5, 2.5],
isBandScale: true,
minInterval: 1,
scaleConfig: getXScaleConfig(ScaleType.Linear),
type: 'xDomain',
},
}),
totalBarsInCluster: barSeries.length,
range: [0, 120],
});
Expand All @@ -1519,13 +1530,11 @@ describe('Crosshair utils linear scale', () => {
});
test('cursor at top begin of domain', () => {
const barSeriesScaleLimited = computeXScale({
xDomain: {
xDomain: MockXDomain.fromScaleType(ScaleType.Linear, {
domain: [0.5, 3.5],
isBandScale: true,
minInterval: 1,
scaleConfig: getXScaleConfig(ScaleType.Linear),
type: 'xDomain',
},
}),
totalBarsInCluster: 1,
range: [0, 120],
});
Expand All @@ -1550,13 +1559,11 @@ describe('Crosshair utils linear scale', () => {
});
test('cursor at top end of domain', () => {
const barSeriesScaleLimited = computeXScale({
xDomain: {
xDomain: MockXDomain.fromScaleType(ScaleType.Linear, {
domain: [-0.5, 2.5],
isBandScale: true,
minInterval: 1,
scaleConfig: getXScaleConfig(ScaleType.Linear),
type: 'xDomain',
},
}),
totalBarsInCluster: barSeries.length,
range: [0, 120],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/

import { ChartTypes } from '../..';
import { MockGlobalSpec } from '../../../mocks/specs/specs';
import { ScaleType } from '../../../scales/constants';
import { SpecTypes } from '../../../specs/constants';
import { getAPIScaleConfigs } from '../state/selectors/get_api_scale_configs';
import { computeSeriesDomains } from '../state/utils/utils';
import { computeXScale } from '../utils/scales';
import { BasicSeriesSpec, SeriesTypes } from '../utils/specs';
Expand Down Expand Up @@ -96,22 +98,36 @@ describe('Crosshair utils ordinal scales', () => {
yScaleType: ScaleType.Linear,
};

const domainGroup = new Map([['group1', { fit: true }]]);

const barSeries = [barSeries1];
const barSeriesDomains = computeSeriesDomains(barSeries, domainGroup);

const barSeriesDomains = computeSeriesDomains(
barSeries,
getAPIScaleConfigs([], barSeries, MockGlobalSpec.settings()),
);

const multiBarSeries = [barSeries1, barSeries2];
const multiBarSeriesDomains = computeSeriesDomains(multiBarSeries, domainGroup);
const multiBarSeriesDomains = computeSeriesDomains(
multiBarSeries,
getAPIScaleConfigs([], multiBarSeries, MockGlobalSpec.settings()),
);

const lineSeries = [lineSeries1];
const lineSeriesDomains = computeSeriesDomains(lineSeries, domainGroup);
const lineSeriesDomains = computeSeriesDomains(
lineSeries,
getAPIScaleConfigs([], lineSeries, MockGlobalSpec.settings()),
);

const multiLineSeries = [lineSeries1, lineSeries2];
const multiLineSeriesDomains = computeSeriesDomains(multiLineSeries, domainGroup);
const multiLineSeriesDomains = computeSeriesDomains(
multiLineSeries,
getAPIScaleConfigs([], multiLineSeries, MockGlobalSpec.settings()),
);

const mixedLinesBars = [lineSeries1, lineSeries2, barSeries1, barSeries2];
const mixedLinesBarsSeriesDomains = computeSeriesDomains(mixedLinesBars, domainGroup);
const mixedLinesBarsSeriesDomains = computeSeriesDomains(
mixedLinesBars,
getAPIScaleConfigs([], mixedLinesBars, MockGlobalSpec.settings()),
);

const barSeriesScale = computeXScale({
xDomain: barSeriesDomains.xDomain,
Expand Down
38 changes: 19 additions & 19 deletions src/chart_types/xy_chart/domains/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ import { ScaleContinuousType } from '../../../scales';
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 { APIScale } from '../scales/types';
import { XScaleType } from '../utils/specs';

/** @internal */
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;
};
export type XDomain = Pick<LogScaleOptions, 'logBase'> &
APIScale<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;
ticks: number;
};

/** @internal */
export type YDomain = LogScaleOptions & {
type: 'yDomain';
scaleConfig: ScaleConfig<ScaleContinuousType>;
isBandScale: false;
groupId: GroupId;
domain: ContinuousDomain;
};
export type YDomain = LogScaleOptions &
APIScale<ScaleContinuousType> & {
isBandScale: false;
groupId: GroupId;
domain: ContinuousDomain;
ticks: number;
};
Loading

0 comments on commit 521a15f

Please sign in to comment.