Skip to content

Commit 59f0f6e

Browse files
markov00nickofthyme
andcommitted
fix: remove series with undefined splitSeriesAccessor values (#627)
This commit removes every series generated from a partial datum where all the values accessed by the splitSeriesAccessors array are null or undefined. Co-authored-by: nickofthyme <[email protected]>
1 parent 5669178 commit 59f0f6e

20 files changed

+162
-30
lines changed

integration/tests/interactions.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import { common } from '../page_objects';
2020

21-
describe.only('Tooltips', () => {
21+
describe('Tooltips', () => {
2222
describe('rotation 0', () => {
2323
it('shows tooltip on first x value - top', async () => {
2424
await common.expectChartWithMouseAtUrlToMatchScreenshot(
@@ -104,4 +104,13 @@ describe.only('Tooltips', () => {
104104
);
105105
});
106106
});
107+
it('should render corrent tooltip for split and y accessors', async () => {
108+
await common.expectChartWithMouseAtUrlToMatchScreenshot(
109+
'http://localhost:9001/iframe.html?id=bar-chart--bar-chart-2-y-2-g',
110+
{
111+
x: 330,
112+
y: 40,
113+
},
114+
);
115+
});
107116
});

src/chart_types/xy_chart/state/utils.test.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ describe('Chart State utils', () => {
362362

363363
describe('series collection is not empty', () => {
364364
it('it should return an empty map if no color', () => {
365-
const barSpec1 = MockSeriesSpec.bar({ id: specId1, data });
366-
const barSpec2 = MockSeriesSpec.bar({ id: specId2, data });
365+
const barSpec1 = MockSeriesSpec.bar({ id: specId1, data, splitSeriesAccessors: ['g'] });
366+
const barSpec2 = MockSeriesSpec.bar({ id: specId2, data, splitSeriesAccessors: ['g'] });
367367
const barSeriesSpecs = MockSeriesSpecs.fromSpecs([barSpec1, barSpec2]);
368368
const barSeriesCollection = MockSeriesCollection.fromSpecs(barSeriesSpecs);
369369
const actual = getCustomSeriesColors(barSeriesSpecs, barSeriesCollection);
@@ -384,8 +384,13 @@ describe('Chart State utils', () => {
384384

385385
describe('with customSeriesColors array', () => {
386386
const customSeriesColors = ['red', 'blue', 'green'];
387-
const barSpec1 = MockSeriesSpec.bar({ id: specId1, data, color: customSeriesColors });
388-
const barSpec2 = MockSeriesSpec.bar({ id: specId2, data });
387+
const barSpec1 = MockSeriesSpec.bar({
388+
id: specId1,
389+
data,
390+
color: customSeriesColors,
391+
splitSeriesAccessors: ['g'],
392+
});
393+
const barSpec2 = MockSeriesSpec.bar({ id: specId2, data, splitSeriesAccessors: ['g'] });
389394
const barSeriesSpecs = MockSeriesSpecs.fromSpecs([barSpec1, barSpec2]);
390395
const barSeriesCollection = MockSeriesCollection.fromSpecs(barSeriesSpecs);
391396

@@ -412,8 +417,14 @@ describe('Chart State utils', () => {
412417

413418
return null;
414419
};
415-
const barSpec1 = MockSeriesSpec.bar({ id: specId1, yAccessors: ['y'], data, color });
416-
const barSpec2 = MockSeriesSpec.bar({ id: specId2, data });
420+
const barSpec1 = MockSeriesSpec.bar({
421+
id: specId1,
422+
yAccessors: ['y'],
423+
data,
424+
color,
425+
splitSeriesAccessors: ['g'],
426+
});
427+
const barSpec2 = MockSeriesSpec.bar({ id: specId2, data, splitSeriesAccessors: ['g'] });
417428
const barSeriesSpecs = MockSeriesSpecs.fromSpecs([barSpec1, barSpec2]);
418429
const barSeriesCollection = MockSeriesCollection.fromSpecs(barSeriesSpecs);
419430

src/chart_types/xy_chart/utils/__snapshots__/series.test.ts.snap

+93
Original file line numberDiff line numberDiff line change
@@ -19144,6 +19144,99 @@ Array [
1914419144
]
1914519145
`;
1914619146

19147+
exports[`Series functional accessors Shall ignore undefined values on splitSeriesAccessors 1`] = `
19148+
Array [
19149+
Object {
19150+
"data": Array [
19151+
Object {
19152+
"datum": Array [
19153+
0,
19154+
1,
19155+
"a",
19156+
],
19157+
"x": 0,
19158+
"y0": null,
19159+
"y1": 1,
19160+
},
19161+
Object {
19162+
"datum": Array [
19163+
1,
19164+
1,
19165+
"a",
19166+
],
19167+
"x": 1,
19168+
"y0": null,
19169+
"y1": 1,
19170+
},
19171+
Object {
19172+
"datum": Array [
19173+
2,
19174+
1,
19175+
"a",
19176+
],
19177+
"x": 2,
19178+
"y0": null,
19179+
"y1": 1,
19180+
},
19181+
],
19182+
"key": "spec{spec1}yAccessor{1}splitAccessors{2-a}",
19183+
"seriesKeys": Array [
19184+
"a",
19185+
1,
19186+
],
19187+
"specId": "spec1",
19188+
"splitAccessors": Map {
19189+
2 => "a",
19190+
},
19191+
"yAccessor": 1,
19192+
},
19193+
Object {
19194+
"data": Array [
19195+
Object {
19196+
"datum": Array [
19197+
0,
19198+
1,
19199+
"b",
19200+
],
19201+
"x": 0,
19202+
"y0": null,
19203+
"y1": 1,
19204+
},
19205+
Object {
19206+
"datum": Array [
19207+
1,
19208+
1,
19209+
"b",
19210+
],
19211+
"x": 1,
19212+
"y0": null,
19213+
"y1": 1,
19214+
},
19215+
Object {
19216+
"datum": Array [
19217+
2,
19218+
1,
19219+
"b",
19220+
],
19221+
"x": 2,
19222+
"y0": null,
19223+
"y1": 1,
19224+
},
19225+
],
19226+
"key": "spec{spec1}yAccessor{1}splitAccessors{2-b}",
19227+
"seriesKeys": Array [
19228+
"b",
19229+
1,
19230+
],
19231+
"specId": "spec1",
19232+
"splitAccessors": Map {
19233+
2 => "b",
19234+
},
19235+
"yAccessor": 1,
19236+
},
19237+
]
19238+
`;
19239+
1914719240
exports[`Series should compute data series for stacked specs 1`] = `
1914819241
Array [
1914919242
Object {

src/chart_types/xy_chart/utils/series.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -947,5 +947,41 @@ describe('Series', () => {
947947
expect(splittedSeries.rawDataSeries.length).toBe(1);
948948
expect(splittedSeries.rawDataSeries).toMatchSnapshot();
949949
});
950+
951+
test('Shall ignore undefined values on splitSeriesAccessors', () => {
952+
const spec = MockSeriesSpec.bar({
953+
data: [
954+
[0, 1, 'a'],
955+
[1, 1, 'a'],
956+
[2, 1, 'a'],
957+
[0, 1, 'b'],
958+
[1, 1, 'b'],
959+
[2, 1, 'b'],
960+
[0, 1],
961+
[1, 1],
962+
[2, 1],
963+
],
964+
xAccessor: 0,
965+
yAccessors: [1],
966+
splitSeriesAccessors: [2],
967+
});
968+
const splittedSeries = splitSeries(spec);
969+
expect(splittedSeries.rawDataSeries.length).toBe(2);
970+
expect(splittedSeries.rawDataSeries).toMatchSnapshot();
971+
});
972+
test('Should ignore series if splitSeriesAccessors are defined but not contained in any datum', () => {
973+
const spec = MockSeriesSpec.bar({
974+
data: [
975+
[0, 1],
976+
[1, 1],
977+
[2, 1],
978+
],
979+
xAccessor: 0,
980+
yAccessors: [1],
981+
splitSeriesAccessors: [2],
982+
});
983+
const splittedSeries = splitSeries(spec);
984+
expect(splittedSeries.rawDataSeries.length).toBe(0);
985+
});
950986
});
951987
});

src/chart_types/xy_chart/utils/series.ts

+5
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ export function splitSeries({
144144

145145
data.forEach((datum) => {
146146
const splitAccessors = getSplitAccessors(datum, splitSeriesAccessors);
147+
// if splitSeriesAccessors are defined we should have at least one split value to include datum
148+
if (splitSeriesAccessors.length > 0 && splitAccessors.size < 1) {
149+
return;
150+
}
151+
147152
if (isMultipleY) {
148153
yAccessors.forEach((accessor, index) => {
149154
const cleanedDatum = cleanDatum(datum, xAccessor, accessor, y0Accessors && y0Accessors[index]);

src/mocks/specs/specs.ts

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export class MockSeriesSpec {
4545
yScaleType: ScaleType.Linear,
4646
xAccessor: 'x',
4747
yAccessors: ['y'],
48-
splitSeriesAccessors: ['g'],
4948
yScaleToDataExtent: false,
5049
hideInLegend: false,
5150
enableHistogramMode: false,

stories/axes/5_multi_axis_bar_lines.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export const example = () => {
5555
xAccessor="x"
5656
yAccessors={['y']}
5757
stackAccessors={['x']}
58-
splitSeriesAccessors={['g']}
5958
data={[
6059
{ x: 0, y: 3 },
6160
{ x: 1, y: 2 },

stories/axes/6_different_tooltip.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export const example = () => {
4848
xAccessor="x"
4949
yAccessors={['y']}
5050
stackAccessors={['x']}
51-
splitSeriesAccessors={['g']}
5251
data={[
5352
{ x: 0, y: 5 },
5453
{ x: 1, y: 4 },
@@ -64,7 +63,6 @@ export const example = () => {
6463
xAccessor="x"
6564
yAccessors={['y']}
6665
stackAccessors={['x']}
67-
splitSeriesAccessors={['g']}
6866
data={[
6967
{ x: 0, y: 2 },
7068
{ x: 1, y: 3 },

stories/axes/9_custom_mixed_domain.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export const example = () => {
8282
xAccessor="x"
8383
yAccessors={['y']}
8484
stackAccessors={['x']}
85-
splitSeriesAccessors={['g']}
8685
data={[
8786
{ x: 0, y: 3 },
8887
{ x: 1, y: 2 },

stories/bar/23_bar_chart_2y2g.tsx

+1-8
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@
1818

1919
import React from 'react';
2020

21-
import { Axis, BarSeries, Chart, Position, ScaleType, Settings, FilterPredicate } from '../../src';
21+
import { Axis, BarSeries, Chart, Position, ScaleType, Settings } from '../../src';
2222
import * as TestDatasets from '../../src/utils/data_samples/test_dataset';
2323
import { SB_SOURCE_PANEL } from '../utils/storybook';
2424

2525
export const example = () => {
26-
const isVisibleFunction: FilterPredicate = (series) => {
27-
return series.splitAccessors.size > 0
28-
? series.specId === 'bars1' && series.yAccessor === 'y1' && series.splitAccessors.get('g1') === 'cloudflare.com'
29-
: series.specId === 'bars1' && series.yAccessor === 'y1';
30-
};
31-
3226
return (
3327
<Chart className="story-chart">
3428
<Settings showLegend showLegendExtra legendPosition={Position.Right} />
@@ -43,7 +37,6 @@ export const example = () => {
4337
yAccessors={['y1', 'y2']}
4438
splitSeriesAccessors={['g1', 'g2', 'g3']}
4539
data={TestDatasets.BARCHART_2Y2G}
46-
filterSeriesInTooltip={isVisibleFunction}
4740
/>
4841
</Chart>
4942
);

stories/bar/26_single_data_linear.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export const example = () => {
5252
yScaleType={ScaleType.Linear}
5353
xAccessor="x"
5454
yAccessors={['y']}
55-
splitSeriesAccessors={['g']}
5655
data={[{ x: 1, y: 10 }]}
5756
/>
5857
</Chart>

stories/bar/31_negative_and_positive_x_values.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export const example = () => {
3333
yScaleType={ScaleType.Linear}
3434
xAccessor="x"
3535
yAccessors={['y']}
36-
splitSeriesAccessors={['g']}
3736
stackAccessors={['x']}
3837
data={[
3938
{ x: -3, y: 1 },

stories/bar/32_scale_to_extent.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export const example = () => {
6363
yScaleType={ScaleType.Linear}
6464
xAccessor="x"
6565
yAccessors={['y']}
66-
splitSeriesAccessors={['g']}
6766
stackAccessors={['x']}
6867
data={data}
6968
yScaleToDataExtent={yScaleToDataExtent}

stories/bar/33_band_bar.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ export const example = () => {
6262
yAccessors={['max']}
6363
y0Accessors={['min']}
6464
data={data}
65-
// this is a temporary hack to display names for min and max values
66-
splitSeriesAccessors={['fake']}
6765
yScaleToDataExtent={scaleToDataExtent}
6866
/>
6967

stories/grids/2_multiple_axes.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ export const example = () => {
119119
xAccessor="x"
120120
yAccessors={['y']}
121121
stackAccessors={['x']}
122-
splitSeriesAccessors={['g']}
123122
data={[
124123
{ x: 0, y: 3 },
125124
{ x: 1, y: 2 },

stories/interactions/4_line_area_bar_clicks.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export const example = () => {
3939
yScaleType={ScaleType.Linear}
4040
xAccessor="x"
4141
yAccessors={['y']}
42-
splitSeriesAccessors={['g']}
4342
data={[
4443
{ x: 0, y: 2.3 },
4544
{ x: 1, y: 2 },

stories/interactions/8_clicks_legend_items_mixed.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export const example = () => {
5454
xAccessor="x"
5555
yAccessors={['y']}
5656
stackAccessors={['x']}
57-
splitSeriesAccessors={['g']}
5857
data={[
5958
{ x: 0, y: 3 },
6059
{ x: 1, y: 2 },

stories/legend/6_hide_legend.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export const example = () => {
5151
xAccessor="x"
5252
yAccessors={['y']}
5353
stackAccessors={['x']}
54-
splitSeriesAccessors={['g']}
5554
data={[
5655
{ x: 0, y: 3 },
5756
{ x: 1, y: 2 },

stories/mixed/3_areas_and_bars.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const example = () => {
3434
yScaleType={ScaleType.Linear}
3535
xAccessor="x"
3636
yAccessors={['y']}
37-
splitSeriesAccessors={['g']}
3837
stackAccessors={['x']}
3938
data={[
4039
{ x: 0, y: 2 },

0 commit comments

Comments
 (0)