Skip to content

Commit c142472

Browse files
[Visualize] Horizontal Bar Percentiles Overlapping (#75315) (#75437)
* [Visualize] Horizontal Bar Percentiles Overlapping Closes: #74986 * add tests Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent f572480 commit c142472

File tree

3 files changed

+124
-5
lines changed

3 files changed

+124
-5
lines changed

src/plugins/vis_type_vislib/public/vislib/visualizations/point_series/_point_series.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ export class PointSeries {
5454
}, 0);
5555
}
5656

57-
getGroupedNum(data) {
57+
getGroupedNum(seriesId) {
5858
let i = 0;
5959
const stacks = [];
6060
for (const seri of this.baseChart.chartConfig.series) {
6161
const valueAxis = seri.valueAxis || this.baseChart.handler.valueAxes[0].id;
6262
const isStacked = seri.mode === 'stacked';
6363
if (!isStacked) {
64-
if (seri.data === data) return i;
64+
if (seri.data.rawId === seriesId) return i;
6565
i++;
6666
} else {
6767
if (!(valueAxis in stacks)) stacks[valueAxis] = i++;
68-
if (seri.data === data) return stacks[valueAxis];
68+
if (seri.data.rawId === seriesId) return stacks[valueAxis];
6969
}
7070
}
7171
return 0;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
import { PointSeries } from './_point_series';
20+
21+
describe('Point Series', () => {
22+
describe('getGroupedNum', () => {
23+
let handler;
24+
25+
beforeEach(() => {
26+
handler = {
27+
visConfig: {
28+
get: jest.fn(),
29+
},
30+
pointSeries: {
31+
chartConfig: {
32+
series: [],
33+
},
34+
handler: {
35+
valueAxes: [{ id: 'stackedId' }],
36+
},
37+
},
38+
};
39+
});
40+
41+
describe('normal mode', () => {
42+
let pointSeries;
43+
44+
beforeEach(() => {
45+
handler.pointSeries.chartConfig.series = [
46+
{
47+
mode: 'normal',
48+
data: {
49+
label: '1st',
50+
rawId: 'col-1',
51+
},
52+
},
53+
{
54+
mode: 'normal',
55+
data: {
56+
label: '2nd',
57+
rawId: 'col-2',
58+
},
59+
},
60+
];
61+
62+
pointSeries = new PointSeries(handler, [{}], {}, {});
63+
});
64+
65+
test('should calculate correct group num', () => {
66+
expect(pointSeries.getGroupedNum('col-2')).toBe(1);
67+
});
68+
69+
test('should return "0" for not found id', () => {
70+
expect(pointSeries.getGroupedNum('wrong-id')).toBe(0);
71+
});
72+
});
73+
74+
describe('stacked mode', () => {
75+
let pointSeries;
76+
77+
beforeEach(() => {
78+
handler.pointSeries.chartConfig.series = [
79+
{
80+
mode: 'normal',
81+
data: {
82+
label: '1st',
83+
rawId: 'col-1',
84+
},
85+
},
86+
{
87+
mode: 'stacked',
88+
data: {
89+
label: '3rd',
90+
rawId: 'col-2',
91+
},
92+
},
93+
{
94+
mode: 'stacked',
95+
data: {
96+
label: '2nd',
97+
rawId: 'col-3',
98+
},
99+
},
100+
];
101+
102+
pointSeries = new PointSeries(handler, [{}], {}, {});
103+
});
104+
105+
test('should calculate correct group num', () => {
106+
expect(pointSeries.getGroupedNum('col-2')).toBe(1);
107+
});
108+
109+
test('should return "0" for not found id', () => {
110+
expect(pointSeries.getGroupedNum('wrong-id')).toBe(0);
111+
});
112+
});
113+
});
114+
});

src/plugins/vis_type_vislib/public/vislib/visualizations/point_series/column_chart.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ export class ColumnChart extends PointSeries {
130130
const yMin = yScale.domain()[0];
131131
const gutterSpacingPercentage = 0.15;
132132
const chartData = this.chartData;
133+
const getGroupedNum = this.getGroupedNum.bind(this);
133134
const groupCount = this.getGroupedCount();
134-
const groupNum = this.getGroupedNum(this.chartData);
135+
135136
let barWidth;
136137
let gutterWidth;
137138

@@ -145,6 +146,8 @@ export class ColumnChart extends PointSeries {
145146
}
146147

147148
function x(d, i) {
149+
const groupNum = getGroupedNum(d.seriesId);
150+
148151
if (isTimeScale) {
149152
return (
150153
xScale(d.x) +
@@ -249,12 +252,13 @@ export class ColumnChart extends PointSeries {
249252
const yScale = this.getValueAxis().getScale();
250253
const chartData = this.chartData;
251254
const groupCount = this.getGroupedCount();
252-
const groupNum = this.getGroupedNum(this.chartData);
253255
const gutterSpacingPercentage = 0.15;
254256
const isTimeScale = this.getCategoryAxis().axisConfig.isTimeDomain();
255257
const isHorizontal = this.getCategoryAxis().axisConfig.isHorizontal();
256258
const isLogScale = this.getValueAxis().axisConfig.isLogScale();
257259
const isLabels = this.labelOptions.show;
260+
const getGroupedNum = this.getGroupedNum.bind(this);
261+
258262
let barWidth;
259263
let gutterWidth;
260264

@@ -268,6 +272,7 @@ export class ColumnChart extends PointSeries {
268272
}
269273

270274
function x(d, i) {
275+
const groupNum = getGroupedNum(d.seriesId);
271276
if (isTimeScale) {
272277
return (
273278
xScale(d.x) +

0 commit comments

Comments
 (0)