Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: order ordinal values by sum #814

Merged
merged 9 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions src/chart_types/xy_chart/domains/x_domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { ChartTypes } from '../..';
import { MockSeriesSpecs } from '../../../mocks/specs';
import { ScaleType } from '../../../scales/constants';
import { SpecTypes } from '../../../specs/constants';
import { getDataSeriesBySpecId } from '../utils/series';
Expand Down Expand Up @@ -837,4 +838,71 @@ describe('X Domain', () => {
expect(attemptToMerge).toThrowError(expectedError);
});
});

describe('orderOrdinalBucketsBySum', () => {
const ordinalSpecs = MockSeriesSpecs.fromPartialSpecs([
{
id: 'ordinal1',
seriesType: SeriesTypes.Bar,
xScaleType: ScaleType.Ordinal,
data: [
{ x: 'a', y: 2 },
{ x: 'b', y: 4 },
{ x: 'c', y: 8 },
{ x: 'd', y: 6 },
],
},
{
id: 'ordinal2',
seriesType: SeriesTypes.Bar,
xScaleType: ScaleType.Ordinal,
data: [
{ x: 'a', y: 4 },
{ x: 'b', y: 8 },
{ x: 'c', y: 16 },
{ x: 'd', y: 12 },
],
},
]);

const linearSpecs = MockSeriesSpecs.fromPartialSpecs([
{
id: 'linear1',
seriesType: SeriesTypes.Bar,
xScaleType: ScaleType.Linear,
data: [
{ x: 1, y: 2 },
{ x: 2, y: 4 },
{ x: 3, y: 8 },
{ x: 4, y: 6 },
],
},
{
id: 'linear2',
seriesType: SeriesTypes.Bar,
xScaleType: ScaleType.Linear,
data: [
{ x: 1, y: 4 },
{ x: 2, y: 8 },
{ x: 3, y: 16 },
{ x: 4, y: 12 },
],
},
]);

it('should sort ordinal xValues by decreasing sum when true', () => {
const { xValues } = getDataSeriesBySpecId(ordinalSpecs, [], true);
expect(xValues).toEqual(new Set(['c', 'd', 'b', 'a']));
});

it('should NOT sort ordinal xValues by decreasing sum when false', () => {
const { xValues } = getDataSeriesBySpecId(ordinalSpecs, [], false);
expect(xValues).toEqual(new Set(['a', 'b', 'c', 'd']));
});

it('should NOT sort linear xValue by decreasing sum when true', () => {
const { xValues } = getDataSeriesBySpecId(linearSpecs, [], true);
expect(xValues).toEqual(new Set([1, 2, 3, 4]));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const computeSeriesDomainsSelector = createCachedSelector(
customYDomainsByGroupId,
deselectedDataSeries,
settingsSpec.xDomain,
settingsSpec.orderOrdinalBucketsBySum,
);
return domains;
},
Expand Down
2 changes: 2 additions & 0 deletions src/chart_types/xy_chart/state/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,12 @@ export function computeSeriesDomains(
customYDomainsByGroupId: Map<GroupId, YDomainRange> = new Map(),
deselectedDataSeries: SeriesIdentifier[] = [],
customXDomain?: DomainRange | Domain,
orderOrdinalBucketsBySum?: boolean,
): SeriesDomainsAndData {
const { dataSeriesBySpecId, xValues, seriesCollection, fallbackScale } = getDataSeriesBySpecId(
seriesSpecs,
deselectedDataSeries,
orderOrdinalBucketsBySum,
);

// compute the x domain merging any custom domain
Expand Down
156 changes: 101 additions & 55 deletions src/chart_types/xy_chart/utils/series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,64 +48,104 @@ const dg = new SeededDataGenerator();

describe('Series', () => {
test('Can split dataset into 1Y0G series', () => {
const splittedSeries = splitSeriesDataByAccessors({
id: 'spec1',
data: TestDataset.BARCHART_1Y0G,
xAccessor: 'x',
yAccessors: ['y1'],
splitSeriesAccessors: ['y'],
});
const splittedSeries = splitSeriesDataByAccessors(
monfera marked this conversation as resolved.
Show resolved Hide resolved
{
id: 'spec1',
data: TestDataset.BARCHART_1Y0G,
xAccessor: 'x',
yAccessors: ['y1'],
splitSeriesAccessors: ['y'],
},
new Map(),
);

expect([...splittedSeries.dataSeries.values()]).toMatchSnapshot();
});
test('Can split dataset into 1Y1G series', () => {
const splittedSeries = splitSeriesDataByAccessors({
id: 'spec1',
data: TestDataset.BARCHART_1Y1G,
xAccessor: 'x',
yAccessors: ['y'],
});
const splittedSeries = splitSeriesDataByAccessors(
{
id: 'spec1',
data: TestDataset.BARCHART_1Y1G,
xAccessor: 'x',
yAccessors: ['y'],
},
new Map(),
);
expect([...splittedSeries.dataSeries.values()]).toMatchSnapshot();
});
test('Can split dataset into 1Y2G series', () => {
const splittedSeries = splitSeriesDataByAccessors({
id: 'spec1',
data: TestDataset.BARCHART_1Y2G,
xAccessor: 'x',
yAccessors: ['y'],
splitSeriesAccessors: ['g1', 'g2'],
});
const splittedSeries = splitSeriesDataByAccessors(
{
id: 'spec1',
data: TestDataset.BARCHART_1Y2G,
xAccessor: 'x',
yAccessors: ['y'],
splitSeriesAccessors: ['g1', 'g2'],
},
new Map(),
);
expect([...splittedSeries.dataSeries.values()]).toMatchSnapshot();
});
test('Can split dataset into 2Y0G series', () => {
const splittedSeries = splitSeriesDataByAccessors({
id: 'spec1',
data: TestDataset.BARCHART_2Y0G,
xAccessor: 'x',
yAccessors: ['y1', 'y2'],
});
const splittedSeries = splitSeriesDataByAccessors(
{
id: 'spec1',
data: TestDataset.BARCHART_2Y0G,
xAccessor: 'x',
yAccessors: ['y1', 'y2'],
},
new Map(),
);
expect([...splittedSeries.dataSeries.values()]).toMatchSnapshot();
});
test('Can split dataset into 2Y1G series', () => {
const splittedSeries = splitSeriesDataByAccessors({
id: 'spec1',
data: TestDataset.BARCHART_2Y1G,
xAccessor: 'x',
yAccessors: ['y1', 'y2'],
splitSeriesAccessors: ['g'],
});
const splittedSeries = splitSeriesDataByAccessors(
{
id: 'spec1',
data: TestDataset.BARCHART_2Y1G,
xAccessor: 'x',
yAccessors: ['y1', 'y2'],
splitSeriesAccessors: ['g'],
},
new Map(),
);
expect([...splittedSeries.dataSeries.values()]).toMatchSnapshot();
});
test('Can split dataset into 2Y2G series', () => {
const splittedSeries = splitSeriesDataByAccessors({
id: 'spec1',
data: TestDataset.BARCHART_2Y2G,
xAccessor: 'x',
yAccessors: ['y1', 'y2'],
splitSeriesAccessors: ['g1', 'g2'],
});
const splittedSeries = splitSeriesDataByAccessors(
{
id: 'spec1',
data: TestDataset.BARCHART_2Y2G,
xAccessor: 'x',
yAccessors: ['y1', 'y2'],
splitSeriesAccessors: ['g1', 'g2'],
},
new Map(),
);
expect([...splittedSeries.dataSeries.values()]).toMatchSnapshot();
});
it('should get sum of all xValues', () => {
const xValueSums = new Map();
splitSeriesDataByAccessors(
{
id: 'spec1',
data: TestDataset.BARCHART_1Y1G_ORDINAL,
xAccessor: 'x',
yAccessors: ['y'],
splitSeriesAccessors: ['g'],
},
xValueSums,
);
expect(xValueSums).toEqual(
new Map([
['a', 3],
['b', 5],
['c', 3],
['d', 4],
['e', 9],
]),
);
});
test('Can stack simple dataseries', () => {
const store = MockStore.default();
MockStore.addSpecs(
Expand Down Expand Up @@ -875,25 +915,31 @@ describe('Series', () => {
describe('functional accessors', () => {
test('Can split dataset into 2Y2G series', () => {
const xAccessor: AccessorFn = (d) => d.x;
const splittedSeries = splitSeriesDataByAccessors({
id: 'spec1',
data: TestDataset.BARCHART_2Y2G,
xAccessor,
yAccessors: ['y1', 'y2'],
splitSeriesAccessors: ['g1', 'g2'],
});
const splittedSeries = splitSeriesDataByAccessors(
{
id: 'spec1',
data: TestDataset.BARCHART_2Y2G,
xAccessor,
yAccessors: ['y1', 'y2'],
splitSeriesAccessors: ['g1', 'g2'],
},
new Map(),
);
expect([...splittedSeries.dataSeries.values()].length).toBe(8);
expect([...splittedSeries.dataSeries.values()]).toMatchSnapshot();
});

test('Can split dataset with custom _all xAccessor', () => {
const xAccessor: AccessorFn = () => '_all';
const splittedSeries = splitSeriesDataByAccessors({
id: 'spec1',
data: TestDataset.BARCHART_2Y2G,
xAccessor,
yAccessors: ['y1'],
});
const splittedSeries = splitSeriesDataByAccessors(
{
id: 'spec1',
data: TestDataset.BARCHART_2Y2G,
xAccessor,
yAccessors: ['y1'],
},
new Map(),
);
expect([...splittedSeries.dataSeries.values()].length).toBe(1);
expect([...splittedSeries.dataSeries.values()]).toMatchSnapshot();
});
Expand All @@ -915,7 +961,7 @@ describe('Series', () => {
yAccessors: [1],
splitSeriesAccessors: [2],
});
const splittedSeries = splitSeriesDataByAccessors(spec);
const splittedSeries = splitSeriesDataByAccessors(spec, new Map());
expect([...splittedSeries.dataSeries.values()].length).toBe(2);
expect([...splittedSeries.dataSeries.values()]).toMatchSnapshot();
});
Expand All @@ -930,7 +976,7 @@ describe('Series', () => {
yAccessors: [1],
splitSeriesAccessors: [2],
});
const splittedSeries = splitSeriesDataByAccessors(spec);
const splittedSeries = splitSeriesDataByAccessors(spec, new Map());
expect([...splittedSeries.dataSeries.values()].length).toBe(0);
});
});
Expand Down
Loading