Skip to content

Commit c1b59f2

Browse files
authored
fix(xy): consider useDefaultGroupDomain on scale config (#1119)
The #1087 PR introduced a regression where the useDefaultGroupDomain was not considered when computing the scale configs.
1 parent 51f54db commit c1b59f2

File tree

5 files changed

+151
-2
lines changed

5 files changed

+151
-2
lines changed
Loading
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { MockGlobalSpec, MockSeriesSpec } from '../../../../mocks/specs/specs';
21+
import { MockStore } from '../../../../mocks/store/store';
22+
import { DEFAULT_GLOBAL_ID } from '../../utils/specs';
23+
import { computeSeriesGeometriesSelector } from './compute_series_geometries';
24+
import { getScaleConfigsFromSpecsSelector } from './get_api_scale_configs';
25+
26+
describe('GroupIds and useDefaultGroupId', () => {
27+
it('use the specified useDefaultGroupId to compute scale configs', () => {
28+
const store = MockStore.default();
29+
MockStore.addSpecs(
30+
[
31+
MockSeriesSpec.bar({
32+
groupId: 'other',
33+
useDefaultGroupDomain: 'a different one',
34+
}),
35+
],
36+
store,
37+
);
38+
const scaleConfigs = getScaleConfigsFromSpecsSelector(store.getState());
39+
expect(scaleConfigs.y['a different one']).toBeDefined();
40+
});
41+
42+
it('have 2 different y domains with 2 groups', () => {
43+
const store = MockStore.default();
44+
MockStore.addSpecs(
45+
[
46+
MockSeriesSpec.bar({ id: 'one' }),
47+
MockSeriesSpec.bar({
48+
id: 'two',
49+
groupId: 'other',
50+
useDefaultGroupDomain: 'a different one',
51+
}),
52+
],
53+
store,
54+
);
55+
const scaleConfigs = getScaleConfigsFromSpecsSelector(store.getState());
56+
expect(Object.keys(scaleConfigs.y)).toHaveLength(2);
57+
expect(scaleConfigs.y['a different one']).toBeDefined();
58+
expect(scaleConfigs.y[DEFAULT_GLOBAL_ID]).toBeDefined();
59+
});
60+
61+
it('have 2 different y domains with 3 groups', () => {
62+
const store = MockStore.default({ width: 120, height: 100, left: 0, top: 0 });
63+
MockStore.addSpecs(
64+
[
65+
MockGlobalSpec.settingsNoMargins(),
66+
MockSeriesSpec.bar({ id: 'one', data: [{ x: 1, y: 10 }] }),
67+
MockSeriesSpec.bar({
68+
id: 'two',
69+
groupId: 'other',
70+
useDefaultGroupDomain: 'a different one',
71+
data: [{ x: 1, y: 10 }],
72+
}),
73+
MockSeriesSpec.bar({
74+
id: 'three',
75+
groupId: 'another again',
76+
useDefaultGroupDomain: 'a different one',
77+
data: [{ x: 1, y: 10 }],
78+
}),
79+
],
80+
store,
81+
);
82+
const scaleConfigs = getScaleConfigsFromSpecsSelector(store.getState());
83+
expect(Object.keys(scaleConfigs.y)).toHaveLength(2);
84+
expect(scaleConfigs.y['a different one']).toBeDefined();
85+
expect(scaleConfigs.y[DEFAULT_GLOBAL_ID]).toBeDefined();
86+
87+
const geoms = computeSeriesGeometriesSelector(store.getState());
88+
const { bars } = geoms.geometries;
89+
expect(bars).toHaveLength(3);
90+
expect(bars[0].value[0].width).toBe(40);
91+
expect(bars[1].value[0].width).toBe(40);
92+
expect(bars[2].value[0].width).toBe(40);
93+
});
94+
});

src/chart_types/xy_chart/state/selectors/get_api_scale_configs.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { isHorizontalAxis, isVerticalAxis } from '../../utils/axis_type_utils';
3333
import { groupBy } from '../../utils/group_data_series';
3434
import { AxisSpec, BasicSeriesSpec, CustomXDomain, XScaleType, YDomainRange } from '../../utils/specs';
3535
import { isHorizontalRotation } from '../utils/common';
36+
import { getSpecDomainGroupId } from '../utils/spec';
3637
import { getAxisSpecsSelector, getSeriesSpecsSelector } from './get_specs';
3738
import { mergeYCustomDomainsByGroupId } from './merge_y_custom_domains';
3839

@@ -83,14 +84,15 @@ export function getScaleConfigsFromSpecs(
8384
};
8485

8586
// y axes
86-
const scaleConfigsByGroupId = groupBy(seriesSpecs, ['groupId'], true).reduce<
87+
const scaleConfigsByGroupId = groupBy(seriesSpecs, getSpecDomainGroupId, true).reduce<
8788
Record<GroupId, { nice: boolean; type: ScaleContinuousType }>
8889
>((acc, series) => {
8990
const yScaleTypes = series.map(({ yScaleType, yNice }) => ({
9091
nice: getYNiceFromSpec(yNice),
9192
type: getYScaleTypeFromSpec(yScaleType),
9293
}));
93-
acc[series[0].groupId] = coerceYScaleTypes(yScaleTypes);
94+
const groupId = getSpecDomainGroupId(series[0]);
95+
acc[groupId] = coerceYScaleTypes(yScaleTypes);
9496
return acc;
9597
}, {});
9698

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import React from 'react';
21+
22+
import { Axis, BarSeries, Chart, Position, ScaleType } from '../../src';
23+
24+
export const Example = () => {
25+
return (
26+
<Chart className="story-chart">
27+
<Axis id="bottom" position={Position.Bottom} />
28+
<Axis id="right" groupId="mainGroup" position={Position.Left} ticks={5} />
29+
30+
<BarSeries
31+
id="groupB"
32+
groupId="other"
33+
useDefaultGroupDomain="mainGroup"
34+
xScaleType={ScaleType.Ordinal}
35+
yScaleType={ScaleType.Linear}
36+
yNice
37+
xAccessor="x"
38+
yAccessors={['y']}
39+
splitSeriesAccessors={['g']}
40+
stackAccessors={['g']}
41+
data={[
42+
{ x: 'A', y: 10, g: 'ga' },
43+
{ x: 'A', y: 10, g: 'gb' },
44+
{ x: 'A', y: 10, g: 'gc' },
45+
{ x: 'B', y: 10, g: 'ga' },
46+
{ x: 'B', y: 10, g: 'gb' },
47+
{ x: 'B', y: 10, g: 'gc' },
48+
]}
49+
/>
50+
</Chart>
51+
);
52+
};

stories/bar/bars.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ export { Example as testMinHeightPositiveAndNegativeValues } from './46_test_min
8585
export { Example as testTooltipAndRotation } from './48_test_tooltip';
8686
export { Example as tooltipBoundary } from './55_tooltip_boundary';
8787
export { Example as testDualYAxis } from './49_test_dual_axis';
88+
export { Example as testUseDefaultGroupDomain } from './56_test_use_dfl_gdomain';

0 commit comments

Comments
 (0)