-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdodgeX.ts
66 lines (59 loc) · 1.91 KB
/
dodgeX.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { deepMix } from '@antv/util';
import { TransformComponent as TC } from '../runtime';
import { DodgeXTransform } from '../spec';
import { column, columnOf, maybeColumnOf } from './utils/helper';
import {
createGroups,
normalizeComparator,
applyOrder,
domainOf,
} from './utils/order';
export type DodgeXOptions = Omit<DodgeXTransform, 'type'>;
/**
* The dodge group marks into series by color or series channel,
* and then produce new series channel for each series by specified order,
* say to form horizontal "columns" by specified channels.
*/
export const DodgeX: TC<DodgeXOptions> = (options = {}) => {
const { groupBy = 'x', reverse = false, orderBy, padding, ...rest } = options;
return (I, mark) => {
const { data, encode, scale } = mark;
const { series: scaleSeries } = scale;
const [Y] = columnOf(encode, 'y');
const [S] = maybeColumnOf(encode, 'series', 'color');
const domainSeries = domainOf(S, scaleSeries);
const newMark = deepMix({}, mark, {
scale: {
series: {
domain: domainSeries,
paddingInner: padding,
},
},
});
// Create groups and apply specified order for each group.
const groups = createGroups(groupBy, I, mark);
const createComparator = normalizeComparator(orderBy);
if (!createComparator) {
return [I, deepMix(newMark, { encode: { series: column(S) } })];
}
// Sort and Update series for each mark related to series domain.
const comparator = createComparator(data, Y, S);
if (comparator) applyOrder(groups, comparator);
const newS = new Array(I.length);
for (const G of groups) {
if (reverse) G.reverse();
for (let i = 0; i < G.length; i++) {
newS[G[i]] = domainSeries[i];
}
}
return [
I,
deepMix(newMark, {
encode: {
series: column(orderBy ? newS : S),
},
}),
];
};
};
DodgeX.props = {};