Skip to content

Commit

Permalink
fix(transform): sort x only sort data in specified domain (#4932)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini authored May 4, 2023
1 parent 58ade36 commit 835933b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions __tests__/plots/static/alphabet-interval-sort-x-domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { G2Spec } from '../../../src';

export function alphabetIntervalSortXDomain(): G2Spec {
return {
type: 'interval',
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
encode: {
x: 'letter',
y: 'frequency',
},
transform: [{ type: 'sortX', by: 'y', reverse: true }],
scale: { x: { domain: ['A', 'B', 'C'] } },
axis: { y: { labelFormatter: '.0%' } },
};
}
1 change: 1 addition & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,4 @@ export { scoreByItemAreaRadarSize } from './score-by-item-area-radar-size';
export { mockPointLogTicks } from './mock-point-log-ticks';
export { alphabetIntervalLabelRotate } from './alphabet-interval-label-rotate';
export { aaplLineBasicTranspose } from './aapl-line-basic-transpose';
export { alphabetIntervalSortXDomain } from './alphabet-interval-sort-x-domain';
15 changes: 12 additions & 3 deletions src/transform/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type SortOptions = {
};

function sortQuantitative(I, mark, options): [number[], G2Mark] {
const { reverse, slice, channel } = options;
const { reverse, channel } = options;
const { encode } = mark;
const [V] = columnOf(encode, channel);
const sortedI = sort(I, (i: number) => V[i]);
Expand All @@ -53,12 +53,21 @@ function sortQuantitative(I, mark, options): [number[], G2Mark] {
return [sortedI, mark];
}

// If domain is specified, only sort data in the domain.
function filterIndex(I, values, specifiedDomain): number[] {
if (!Array.isArray(specifiedDomain)) return I;
const domain = new Set(specifiedDomain);
return I.filter((i) => domain.has(values[i]));
}

function sortOrdinal(I, mark, options): [number[], G2Mark] {
const { reverse, slice, channel, ...rest } = options;
const { encode } = mark;
const { encode, scale = {} } = mark;
const domain = scale[channel]?.domain;
const [T] = columnOf(encode, channel);
const normalizeReducer = createReducer(channel, rest, encode);
const sortedDomain = groupSort(I, normalizeReducer, (i: number) => T[i]);
const SI = filterIndex(I, T, domain);
const sortedDomain = groupSort(SI, normalizeReducer, (i: number) => T[i]);
if (reverse) sortedDomain.reverse();
const s = typeof slice === 'number' ? [0, slice] : slice;
const slicedDomain = slice ? sortedDomain.slice(...s) : sortedDomain;
Expand Down

0 comments on commit 835933b

Please sign in to comment.