UNPKG

2.76 kBMarkdownView Raw
1# Transform
2
3## Install
4
5### npm
6
7```shell
8npm install @antv/dw-transform
9```
10
11### yarn
12
13```shell
14yarn add @antv/dw-analyzer
15```
16
17## Schema
18
19```js
20{
21 "groupBy": [...],
22 "actions": [
23 {
24 "type": ...,
25 "field": ...,
26 "as": ...,
27 "options": {
28 ...,
29 }
30 }
31 ]
32}
33```
34
35## Usage
36
37### autoTransform
38
39The `autoTransform` function generates transform schemas to aggregate data fields in a default way.
40
41```ts
42const data = [
43 { gender: 'Male', height: 180 },
44 { gender: 'Female', height: 165 },
45 { gender: 'Male', height: 170 },
46];
47
48const { result, schemas } = autoTransform(data);
49
50console.log(schemas);
51// [
52// {
53// groupBy: ['gender'],
54// actions: [
55// {
56// type: 'sum',
57// field: 'height',
58// as: 'SUM(height)',
59// },
60// ],
61// },
62// ]
63
64console.log(result);
65// [
66// { gender: 'Male', 'SUM(height)': 350 },
67// { gender: 'Female', 'SUM(height)': 165 },
68// ]
69```
70
71#### renameOption
72
73The `renameOption` parameter of the `autoTransform` function defines how the created aggregation field would be named.
74
75Say, field title is `field1` and aggregation type is `sum`:
76
77| renameOption | as |
78| :--------------------------------: | :------------------: |
79| `'brackets'` (default) <br> `true` | `'SUM(field1)'` |
80| `'underline'` | `'SUM_field1'` |
81| `'origin'` <br> `false` | `'field1'` |
82| *function* `f(a,b)` | `f('field1', 'sum')` |
83
84```ts
85const data = [
86 { gender: 'Male', height: 180 },
87 { gender: 'Female', height: 165 },
88 { gender: 'Male', height: 170 },
89];
90
91const { result, schemas } = autoTransform(data, false);
92
93console.log(schemas);
94// [
95// {
96// groupBy: ['gender'],
97// actions: [
98// {
99// type: 'sum',
100// field: 'height',
101// as: 'height', // as origin
102// },
103// ],
104// },
105// ]
106
107console.log(result);
108// [
109// { gender: 'Male', 'height': 350 }, // as origin
110// { gender: 'Female', 'height': 165 }, // as origin
111// ]
112```
113
114### parse
115
116If you already have a dataset and a transform schema for it, you can use the function `parse` to get the result.
117
118```ts
119const data = [
120 { id: '1', height: 10.5, weight: 60 },
121 { id: '2', height: null, weight: 40 },
122 { id: '3', height: 9.5, weight: null },
123 { id: '', height: 9.5, weight: 80 },
124 { id: '5', height: 9.5 },
125];
126
127const schema = {
128 actions: [
129 {
130 type: 'removeNull',
131 field: 'id',
132 as: null,
133 },
134 ],
135}
136
137const result = parse(data, schema);
138// [
139// { id: '1', height: 10.5, weight: 60 },
140// { id: '2', height: null, weight: 40 },
141// { id: '3', height: 9.5, weight: null },
142// { id: '5', height: 9.5 },
143// ]
144```