@@ -3,42 +3,223 @@ title: bin
3
3
order : 2
4
4
---
5
5
6
- 对连续的 x 和 连续的 y 通道进行分箱,并且对通道根据指定的 reducer 进行聚合。
6
+ ## 概述
7
7
8
- ## 开始使用
8
+ ` bin ` 是一个用于数据处理的重要函数,它的主要作用是将连续的数值数据划分为离散的区间(即分箱),从而将数据进行分组。这种操作通常用于数据分析和可视化,以方便统计或展示数据分布。
9
9
10
- 在对应的 mark 中有 transform 方法可以使用 bin 变换 。
10
+ ` bin ` 的核心目的是将原始数据按照指定的规则进行分箱操作,将连续型数据转换为多个离散区间的类别数据。这在数据处理和构建直方图等视图时尤为重要。例如,当需要根据数据的数值范围生成多个区间并统计其频率时,就可以使用 ` bin ` 。
11
11
12
- ``` ts
13
- chart
14
- .point ()
15
- .encode (' x' , ' x' )
16
- // ...
17
- .transform ({
18
- type: ' bin' ,
19
- /* options */
12
+ ## 使用场景
13
+
14
+ - 数据分箱,用于按区间统计数据频率。
15
+ - 构建直方图视图。
16
+ - 将连续型数据转化为离散型数据以便于分析。
17
+
18
+ 下面这个例子展示了如果创建一个分箱图,展示了两个评分系统评分在不同分数区间中的分布情况,可以直观地观察哪个区间的评分较多,哪个区间评分较少。
19
+
20
+ ``` js | ob
21
+ (() => {
22
+ const chart = new G2.Chart ();
23
+
24
+ chart .options ({
25
+ type: ' rect' , // 图表类型为矩形图(直方图)
26
+ data: {
27
+ type: ' fetch' ,
28
+ value: ' https://assets.antv.antgroup.com/g2/movies.json' ,
29
+ },
30
+ encode: {
31
+ x: ' IMDB Rating' , // X 轴编码为 IMDB 评分
32
+ y: ' Rotten Tomatoes Rating' , // Y 轴编码为 Rotten Tomatoes 评分
33
+ },
34
+ transform: [{
35
+ type: ' bin' , // 数据转换类型为分箱
36
+ color: ' count' , // 通过颜色编码表示每个分箱内的数据点数量
37
+ }],
20
38
});
39
+
40
+ chart .render ();
41
+
42
+ return chart .getContainer ();
43
+ })();
21
44
```
22
45
23
- ## 选项
46
+ ## 配置项
47
+
48
+ | 属性 | 描述 | 类型 | 默认值 |
49
+ | ----------- | ----------------------------------------- | ------------------- | ------------------- |
50
+ | thresholdsX | 对 x 分箱的数量 | number | ` d3.thresholdScott ` |
51
+ | thresholdsY | 对 y 分箱的数量 | number | ` d3.thresholdScott ` |
52
+ | [ channel] | 输出到具体 mark 的 channel 数据的聚合方式 | [ channel] ( #channel ) | |
24
53
25
- | 属性 | 描述 | 类型 | 默认值 |
26
- | -------------------| ------------------------------------------------| ---------------------| -----------------------|
27
- | thresholdsX | 对 x 分箱的数量 | ` number ` | ` d3.thresholdScott ` |
28
- | thresholdsY | 对 y 分箱的数量 | ` number ` | ` d3.thresholdScott ` |
29
- | [ channel] | 输出到具体 mark 的 channel 数据的聚合方式 | ` Reducer ` | |
54
+ ### thresholdsX 和 thresholdsY
55
+
56
+ ` thresholdsX ` 和 ` thresholdsY ` 是用于定义数据分箱的两个非常重要的配置项,主要在二维数据分箱(如网格图或热力图)中使用。它们分别控制在 X 和 Y 方向上的分箱(区间划分)规则或数量,用于将二维连续数据划分为离散的网格。
57
+
58
+ ``` js | ob {pin: false}
59
+ (() => {
60
+ const chart = new G2.Chart ();
61
+ let thresholdsX;
62
+ let thresholdsY;
63
+ chart .options ({
64
+ type: ' rect' , // 图表类型为矩形图(直方图)
65
+ data: {
66
+ type: ' fetch' ,
67
+ value: ' https://assets.antv.antgroup.com/g2/movies.json' ,
68
+ },
69
+ encode: {
70
+ x: ' IMDB Rating' , // X 轴编码为 IMDB 评分
71
+ y: ' Rotten Tomatoes Rating' , // Y 轴编码为 Rotten Tomatoes 评分
72
+ },
73
+ transform: [{
74
+ type: ' bin' , // 数据转换类型为分箱
75
+ color: ' count' , // 通过颜色编码表示每个分箱内的数据点数量
76
+ }],
77
+ });
78
+
79
+ // 插入 thresholdsX,thresholdsY 的输入框
80
+ const container = document .createElement (" div" );
81
+ const thresholdsX_Text = document .createElement (" span" );
82
+ thresholdsX_Text .textContent = " thresholdsX: " ;
83
+ const thresholdsX_Input = document .createElement (" input" );
84
+ thresholdsX_Input .setAttribute (" type" , " number" );
85
+ thresholdsX_Input .addEventListener (" input" , (e ) => {
86
+ thresholdsX = e .target .value ;
87
+ chart .options ({
88
+ transform: [{
89
+ type: ' bin' ,
90
+ color: ' count' ,
91
+ thresholdsX,
92
+ thresholdsY,
93
+ }]
94
+ });
95
+ chart .render ();
96
+ });
97
+
98
+ const thresholdsY_Text = document .createElement (" span" );
99
+ thresholdsY_Text .textContent = " thresholdsY: " ;
100
+ const thresholdsY_Input = document .createElement (" input" );
101
+ thresholdsY_Input .setAttribute (" type" , " number" );
102
+ thresholdsY_Input .addEventListener (" input" , (e ) => {
103
+ thresholdsY = e .target .value ;
104
+ chart .options ({
105
+ transform: [{
106
+ type: ' bin' ,
107
+ color: ' count' ,
108
+ thresholdsX,
109
+ thresholdsY,
110
+ }]
111
+ });
112
+ chart .render ();
113
+ });
114
+
115
+ container .appendChild (thresholdsX_Text);
116
+ container .appendChild (thresholdsX_Input);
117
+ container .appendChild (thresholdsY_Text);
118
+ container .appendChild (thresholdsY_Input);
119
+
120
+ const node = chart .getContainer ();
121
+ node .insertBefore (container, node .childNodes [0 ]);
122
+
123
+ chart .render ();
124
+
125
+ return chart .getContainer ();
126
+ })();
127
+ ```
128
+
129
+ ### channel
130
+
131
+ 理论上,` channel ` 可以设置为所有的通道值,具体可以参考 [ encode] ( /manual/core/encode ) 文档。所有的枚举值如下:
30
132
31
133
``` ts
32
- type Primitive = number | string | boolean | Date ;
33
-
34
- type Reducer =
35
- | ' mean'
36
- | ' max'
37
- | ' count'
38
- | ' min'
39
- | ' median'
40
- | ' sum'
41
- | ' first'
42
- | ' last'
43
- | ((I : number [], V : Primitive []) => Primitive );
134
+ type Channel =
135
+ | ' x'
136
+ | ' y'
137
+ | ' z'
138
+ | ' x1'
139
+ | ' y1'
140
+ | ' series'
141
+ | ' color'
142
+ | ' opacity'
143
+ | ' shape'
144
+ | ' size'
145
+ | ' key'
146
+ | ' groupKey'
147
+ | ' position'
148
+ | ' series'
149
+ | ' enterType'
150
+ | ' enterEasing'
151
+ | ' enterDuration'
152
+ | ' enterDelay'
153
+ | ' updateType'
154
+ | ' updateEasing'
155
+ | ' updateDuration'
156
+ | ' updateDelay'
157
+ | ' exitType'
158
+ | ' exitEasing'
159
+ | ' exitDuration'
160
+ | ' exitDelay'
161
+ | ` position${number } ` ;
162
+ ```
163
+
164
+
165
+ ## 示例
166
+
167
+ ### 使用 ` bin ` + ` opacity ` 渲染出透明度分箱
168
+
169
+ ``` js | ob {pin: false}
170
+ (() => {
171
+ const chart = new G2.Chart ();
172
+
173
+ chart .options ({
174
+ type: ' rect' ,
175
+ data: {
176
+ type: ' fetch' ,
177
+ value: ' https://assets.antv.antgroup.com/g2/movies.json' ,
178
+ },
179
+ encode: {
180
+ x: ' IMDB Rating' ,
181
+ y: ' Rotten Tomatoes Rating' ,
182
+ },
183
+ transform: [{
184
+ type: ' bin' , // 数据转换类型为分箱
185
+ opacity: ' count' , // 通过透明度编码表示每个分箱内的数据点数量
186
+ thresholdsX: 10 ,
187
+ thresholdsY: 10
188
+ }],
189
+ });
190
+
191
+ chart .render ();
192
+
193
+ return chart .getContainer ();
194
+ })();
44
195
```
196
+
197
+ ### 使用 ` bin ` + ` size ` 渲染出大小分箱
198
+
199
+ ``` js | ob {pin: false}
200
+ (() => {
201
+ const chart = new G2.Chart ();
202
+
203
+ chart .options ({
204
+ type: ' point' , // 图表类型为矩形图(直方图)
205
+ data: {
206
+ type: ' fetch' ,
207
+ value: ' https://assets.antv.antgroup.com/g2/movies.json' ,
208
+ },
209
+ encode: {
210
+ x: ' IMDB Rating' ,
211
+ y: ' Rotten Tomatoes Rating' ,
212
+ },
213
+ transform: [{
214
+ type: ' bin' , // 数据转换类型为分箱
215
+ size: ' count' , // 通过大小编码表示每个分箱内的数据点数量
216
+ thresholdsX: 10 ,
217
+ thresholdsY: 10
218
+ }],
219
+ });
220
+
221
+ chart .render ();
222
+
223
+ return chart .getContainer ();
224
+ })();
225
+ ```
0 commit comments