From 38f8b61088ff2de7b292e6b28f634ddb875b6c83 Mon Sep 17 00:00:00 2001 From: Marco Vettorello Date: Tue, 24 Nov 2020 11:32:04 +0100 Subject: [PATCH] refactor: renamed function to be more domain specific --- src/chart_types/xy_chart/domains/y_domain.ts | 46 ++++--------------- .../get_tooltip_values_highlighted_geoms.ts | 4 +- src/chart_types/xy_chart/state/utils/spec.ts | 2 +- src/chart_types/xy_chart/state/utils/utils.ts | 4 +- 4 files changed, 13 insertions(+), 43 deletions(-) diff --git a/src/chart_types/xy_chart/domains/y_domain.ts b/src/chart_types/xy_chart/domains/y_domain.ts index 2e40a03eba..d11bba770d 100644 --- a/src/chart_types/xy_chart/domains/y_domain.ts +++ b/src/chart_types/xy_chart/domains/y_domain.ts @@ -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'; @@ -38,17 +38,19 @@ export type YBasicSeriesSpec = Pick< /** @internal */ export function mergeYDomain(dataSeries: DataSeries[], domainsByGroupId: Map): YDomain[] { - const dataSeriesByGroupId = groupBy(dataSeries, ({ spec }) => getSpecGroupId(spec), true); + const dataSeriesByGroupId = groupBy(dataSeries, ({ spec }) => getSpecDomainGroupId(spec), true); return dataSeriesByGroupId.reduce((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; } @@ -60,7 +62,6 @@ function mergeYDomainForGroup( stacked: DataSeries[], nonStacked: DataSeries[], hasZeroBaselineSpecs: boolean, - groupId: string, customDomain?: YDomainRange, ): YDomain | null { const dataSeries = [...stacked, ...nonStacked]; @@ -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) { @@ -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>((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 @@ -244,13 +224,3 @@ function coerceYScale(scaleTypes: Set): ScaleContinuousType } return ScaleType.Linear; } - -export function getYScaleTypeByGroupId(specs: BasicSeriesSpec[]): Map { - 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()); -} diff --git a/src/chart_types/xy_chart/state/selectors/get_tooltip_values_highlighted_geoms.ts b/src/chart_types/xy_chart/state/selectors/get_tooltip_values_highlighted_geoms.ts index 98a4cb37b9..a11c877874 100644 --- a/src/chart_types/xy_chart/state/selectors/get_tooltip_values_highlighted_geoms.ts +++ b/src/chart_types/xy_chart/state/selectors/get_tooltip_values_highlighted_geoms.ts @@ -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'; @@ -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; } diff --git a/src/chart_types/xy_chart/state/utils/spec.ts b/src/chart_types/xy_chart/state/utils/spec.ts index 1141682de1..27fac8a0ae 100644 --- a/src/chart_types/xy_chart/state/utils/spec.ts +++ b/src/chart_types/xy_chart/state/utils/spec.ts @@ -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; } diff --git a/src/chart_types/xy_chart/state/utils/utils.ts b/src/chart_types/xy_chart/state/utils/utils.ts index 48cd1ee163..a4df37f610 100644 --- a/src/chart_types/xy_chart/state/utils/utils.ts +++ b/src/chart_types/xy_chart/state/utils/utils.ts @@ -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'; /** @@ -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; }