Skip to content

Commit

Permalink
refactor: renamed function to be more domain specific
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Nov 24, 2020
1 parent 7ed4ac0 commit 38f8b61
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 43 deletions.
46 changes: 8 additions & 38 deletions src/chart_types/xy_chart/domains/y_domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { identity } from '../../../utils/commons';
import { computeContinuousDataDomain } from '../../../utils/domain';
import { GroupId } from '../../../utils/ids';
import { Logger } from '../../../utils/logger';
import { getSpecGroupId } from '../state/utils/spec';
import { getSpecDomainGroupId } from '../state/utils/spec';
import { isCompleteBound, isLowerBound, isUpperBound } from '../utils/axis_type_utils';
import { groupBy } from '../utils/group_data_series';
import { DataSeries } from '../utils/series';
Expand All @@ -38,17 +38,19 @@ export type YBasicSeriesSpec = Pick<

/** @internal */
export function mergeYDomain(dataSeries: DataSeries[], domainsByGroupId: Map<GroupId, YDomainRange>): YDomain[] {
const dataSeriesByGroupId = groupBy(dataSeries, ({ spec }) => getSpecGroupId(spec), true);
const dataSeriesByGroupId = groupBy(dataSeries, ({ spec }) => getSpecDomainGroupId(spec), true);

return dataSeriesByGroupId.reduce<YDomain[]>((acc, groupedDataSeries) => {
const groupId = getSpecGroupId(groupedDataSeries[0].spec);
const [{ spec }] = groupedDataSeries;
const groupId = getSpecDomainGroupId(spec);

const stacked = groupedDataSeries.filter(({ isStacked }) => isStacked);
const nonStacked = groupedDataSeries.filter(({ isStacked }) => !isStacked);
const customDomain = domainsByGroupId.get(groupId);
const hasNonZeroBaselineTypes = groupedDataSeries.some(
({ seriesType }) => seriesType === SeriesTypes.Bar || seriesType === SeriesTypes.Area,
);
const domain = mergeYDomainForGroup(stacked, nonStacked, hasNonZeroBaselineTypes, groupId, customDomain);
const domain = mergeYDomainForGroup(stacked, nonStacked, hasNonZeroBaselineTypes, customDomain);
if (!domain) {
return acc;
}
Expand All @@ -60,7 +62,6 @@ function mergeYDomainForGroup(
stacked: DataSeries[],
nonStacked: DataSeries[],
hasZeroBaselineSpecs: boolean,
groupId: string,
customDomain?: YDomainRange,
): YDomain | null {
const dataSeries = [...stacked, ...nonStacked];
Expand All @@ -71,7 +72,8 @@ function mergeYDomainForGroup(
yScaleType,
}));
const groupYScaleType = coerceYScaleTypes(yScaleTypes);
const [{ stackMode }] = dataSeries;
const [{ stackMode, spec }] = dataSeries;
const groupId = getSpecDomainGroupId(spec);

let domain: number[];
if (stackMode === StackMode.Percentage) {
Expand Down Expand Up @@ -195,28 +197,6 @@ export function isStackedSpec(spec: YBasicSeriesSpec, histogramEnabled: boolean)
return isBarAndHistogram || hasStackAccessors;
}

/**
* Get the stack mode for every groupId
* @param specs
* @internal
*/
export function getStackModeForYGroup(specs: YBasicSeriesSpec[]) {
return specs.reduce<Record<GroupId, StackMode | undefined>>((acc, { groupId, stackMode }) => {
if (!acc[groupId]) {
acc[groupId] = undefined;
}

if (acc[groupId] === undefined && stackMode !== undefined) {
acc[groupId] = stackMode;
}
if (stackMode !== undefined && acc[groupId] !== stackMode) {
Logger.warn(`Is not possible to mix different stackModes, please align all stackMode on the same GroupId
to the same mode. The default behaviour will be to use the first encountered stackMode on the series`);
}
return acc;
}, {});
}

/**
* Coerce the scale types of a set of specification to a generic one.
* If there is at least one bar series type, than the response will specity
Expand Down Expand Up @@ -244,13 +224,3 @@ function coerceYScale(scaleTypes: Set<ScaleContinuousType>): ScaleContinuousType
}
return ScaleType.Linear;
}

export function getYScaleTypeByGroupId(specs: BasicSeriesSpec[]): Map<GroupId, ScaleContinuousType> {
const groups = groupBy(specs, ['groupId'], true);
return groups.reduce((acc, group) => {
const scaleType = coerceYScaleTypes(group);
const [{ groupId }] = group;
acc.set(groupId, scaleType);
return acc;
}, new Map());
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { Point } from '../../../../utils/point';
import { isPointOnGeometry } from '../../rendering/utils';
import { formatTooltip } from '../../tooltip/tooltip';
import { BasicSeriesSpec, AxisSpec } from '../../utils/specs';
import { getAxesSpecForSpecId, getSpecGroupId, getSpecsById } from '../utils/spec';
import { getAxesSpecForSpecId, getSpecDomainGroupId, getSpecsById } from '../utils/spec';
import { ComputedScales } from '../utils/types';
import { getComputedScalesSelector } from './get_computed_scales';
import { getElementAtCursorPositionSelector } from './get_elements_at_cursor_pos';
Expand Down Expand Up @@ -146,7 +146,7 @@ function getTooltipAndHighlightFromValue(
const { xAxis, yAxis } = getAxesSpecForSpecId(axesSpecs, spec.groupId);

// yScales is ensured by the enclosing if
const yScale = scales.yScales.get(getSpecGroupId(spec));
const yScale = scales.yScales.get(getSpecDomainGroupId(spec));
if (!yScale) {
return acc;
}
Expand Down
2 changes: 1 addition & 1 deletion src/chart_types/xy_chart/state/utils/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function getAxesSpecForSpecId(axesSpecs: AxisSpec[], groupId: GroupId) {
}

/** @internal */
export function getSpecGroupId(spec: BasicSeriesSpec): string {
export function getSpecDomainGroupId(spec: BasicSeriesSpec): string {
if (!spec.useDefaultGroupDomain) {
return spec.groupId;
}
Expand Down
4 changes: 2 additions & 2 deletions src/chart_types/xy_chart/state/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import {
} from '../../utils/specs';
import { SmallMultipleScales } from '../selectors/compute_small_multiple_scales';
import { isHorizontalRotation } from './common';
import { getSpecsById, getAxesSpecForSpecId, getSpecGroupId } from './spec';
import { getSpecsById, getAxesSpecForSpecId, getSpecDomainGroupId } from './spec';
import { SeriesDomainsAndData, ComputedGeometries, GeometriesCounts, Transform, LastValues } from './types';

/**
Expand Down Expand Up @@ -425,7 +425,7 @@ function renderGeometries(
continue;
}
// compute the y scale
const yScale = yScales.get(getSpecGroupId(ds.spec));
const yScale = yScales.get(getSpecDomainGroupId(ds.spec));
if (!yScale) {
continue;
}
Expand Down

0 comments on commit 38f8b61

Please sign in to comment.