Skip to content

Commit

Permalink
fix(xy): dataIndex keeps original data order on small multiples (#1597)
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 authored Feb 17, 2022
1 parent 1f73eec commit 9e2566c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 223 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions packages/charts/src/common/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export function getPredicateFn<T>(predicate: Predicate, accessor?: keyof T): (a:
const bValue = Number(accessor ? b[accessor] : b);
return bValue - aValue;
};
default:
case 'dataIndex':
case 'numAsc':
return (a: T, b: T) => {
const aValue = Number(accessor ? a[accessor] : a);
const bValue = Number(accessor ? b[accessor] : b);
return aValue - bValue;
};
case 'dataIndex':
return () => 0;
}
}
221 changes: 0 additions & 221 deletions storybook/stories/small_multiples/1_grid.story.tsx

This file was deleted.

78 changes: 78 additions & 0 deletions storybook/stories/small_multiples/8_sorting.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { select } from '@storybook/addon-knobs';
import React from 'react';

import {
ScaleType,
Position,
Chart,
Axis,
GroupBy,
SmallMultiples,
Settings,
BarSeries,
Predicate,
} from '@elastic/charts';

/**
* This story is used on VRTs to test the sorting logic of the dataIndex sort predicate
*/
export const Example = () => {
const data: [string, number][] = [
['3', 100],
['5', 80],
['1', 50],
['2', 30],
['6', 12],
['4', 10],
['7', 5],
];

return (
<Chart>
<Settings rotation={90} />
<Axis id="time" title="Day of week" position={Position.Left} />
<Axis id="y" title="Count of logins" position={Position.Bottom} />

<GroupBy
id="h_split"
by={(spec, datum) => {
return datum[0];
}}
sort={select(
'Chart sorting',
{
...Predicate,
},
Predicate.DataIndex,
)}
/>
<SmallMultiples splitVertically="h_split" />

<BarSeries
id="Login count"
name={(si) => {
return `#logins day ${si.smVerticalAccessorValue}`;
}}
xScaleType={ScaleType.Ordinal}
yScaleType={ScaleType.Linear}
timeZone="local"
xAccessor={() => ''}
yAccessors={[1]}
data={data}
/>
</Chart>
);
};
Example.parameters = {
markdown: `You can sort your small multiples by the \`GroupBy.by\` value in ascending/descending order.
If the sort is configured with the \`dataIndex\` predicate the small multiples
will keep the order of the passed data array.`,
};
2 changes: 2 additions & 0 deletions storybook/stories/small_multiples/small_multiples.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export { Example as gridLines } from './3_grid_lines.story';
export { Example as histogramBars } from './5_histogram_bars.story';
export { Example as heterogeneous } from './6_heterogeneous_cartesians.story';
export { Example as sunbursts } from './7_sunbursts.story';

export { Example as sorting } from './8_sorting.story';

0 comments on commit 9e2566c

Please sign in to comment.