@@ -168,6 +168,8 @@ class Geometry<
168
168
this . _createAttrs ( ) ;
169
169
if ( ! this . dataRecords ) {
170
170
this . _processData ( ) ;
171
+ } else {
172
+ this . _readjustData ( this . dataRecords ) ;
171
173
}
172
174
}
173
175
@@ -214,6 +216,46 @@ class Geometry<
214
216
} ;
215
217
}
216
218
219
+ _createAdjust ( ) {
220
+ const { attrs, props } = this ;
221
+ const { adjust } = props ;
222
+
223
+ if ( ! adjust ) {
224
+ return null ;
225
+ }
226
+ const adjustCfg : AdjustProps =
227
+ typeof adjust === 'string'
228
+ ? {
229
+ type : adjust ,
230
+ }
231
+ : adjust ;
232
+ const adjustType = upperFirst ( adjustCfg . type ) ;
233
+ const AdjustConstructor = AdjustMap [ adjustType ] ;
234
+ if ( ! AdjustConstructor ) {
235
+ throw new Error ( 'not support such adjust : ' + adjust ) ;
236
+ }
237
+
238
+ if ( adjustType === 'Dodge' ) {
239
+ // @ts -ignore
240
+ adjustCfg . adjustNames = [ 'x' ] ;
241
+ }
242
+
243
+ const { x, y } = attrs ;
244
+ // @ts -ignore
245
+ adjustCfg . xField = x . field ;
246
+ // @ts -ignore
247
+ adjustCfg . yField = y . field ;
248
+
249
+ const adjustInstance = new AdjustConstructor ( adjustCfg ) ;
250
+
251
+ this . adjust = {
252
+ type : adjustCfg . type ,
253
+ adjust : adjustInstance ,
254
+ } ;
255
+
256
+ return this . adjust ;
257
+ }
258
+
217
259
_adjustScales ( ) {
218
260
const { attrs, props, startOnZero : defaultStartOnZero } = this ;
219
261
const { chart, startOnZero = defaultStartOnZero , coord, adjust } = props ;
@@ -287,56 +329,46 @@ class Geometry<
287
329
const scale = scales [ i ] ;
288
330
if ( scale . isCategory ) {
289
331
const field = scale . field ;
290
- obj [ field ] = scale . translate ( obj . origin [ field ] ) ;
332
+ const value = scale . translate ( obj . origin [ field ] ) ;
333
+ obj [ field ] = isNaN ( value ) ? 0 : value ;
291
334
}
292
335
}
293
336
}
294
337
}
295
338
296
339
_adjustData ( records ) {
297
- const { attrs, props } = this ;
298
- const { adjust } = props ;
299
-
340
+ const { adjust } = this ;
300
341
// groupedArray 是二维数组
301
342
const groupedArray = records . map ( ( record ) => record . children ) ;
302
343
303
344
if ( ! adjust ) {
304
345
return groupedArray ;
305
346
}
306
- const adjustCfg : AdjustProps =
307
- typeof adjust === 'string'
308
- ? {
309
- type : adjust ,
310
- }
311
- : adjust ;
312
- const adjustType = upperFirst ( adjustCfg . type ) ;
313
- const AdjustConstructor = AdjustMap [ adjustType ] ;
314
- if ( ! AdjustConstructor ) {
315
- throw new Error ( 'not support such adjust : ' + adjust ) ;
347
+
348
+ const { attrs } = this ;
349
+ const scales = [ attrs . x . scale , attrs . y . scale ] ;
350
+
351
+ for ( let i = 0 , len = groupedArray . length ; i < len ; i ++ ) {
352
+ const records = groupedArray [ i ] ;
353
+ for ( let j = 0 , len = records . length ; j < len ; j ++ ) {
354
+ const record = records [ j ] ;
355
+ const count = scales . length ;
356
+ for ( let i = 0 ; i < count ; i ++ ) {
357
+ const scale = scales [ i ] ;
358
+ const field = scale . field ;
359
+ record [ field ] = record . origin [ field ] ;
360
+ }
361
+ }
316
362
}
317
363
318
- if ( adjustType === 'Dodge ' ) {
364
+ if ( adjust . type === 'dodge ' ) {
319
365
for ( let i = 0 , len = groupedArray . length ; i < len ; i ++ ) {
320
366
// 如果是dodge, 需要处理数字再处理
321
367
this . _numberic ( groupedArray [ i ] ) ;
322
368
}
323
- // @ts -ignore
324
- adjustCfg . adjustNames = [ 'x' ] ;
325
369
}
326
370
327
- const { x, y } = attrs ;
328
- // @ts -ignore
329
- adjustCfg . xField = x . field ;
330
- // @ts -ignore
331
- adjustCfg . yField = y . field ;
332
-
333
- const adjustInstance = new AdjustConstructor ( adjustCfg ) ;
334
- const adjustData = adjustInstance . process ( groupedArray ) ;
335
-
336
- this . adjust = {
337
- type : adjustCfg . type ,
338
- adjust : adjustInstance ,
339
- } ;
371
+ const adjustData = adjust . adjust . process ( groupedArray ) ;
340
372
341
373
// process 返回的是新数组,所以要修改 records
342
374
records . forEach ( ( record , index : number ) => {
@@ -376,6 +408,8 @@ class Geometry<
376
408
const data = this . _saveOrigin ( originData ) ;
377
409
// 根据分类度量进行数据分组
378
410
const records = this . _groupData ( data ) ;
411
+
412
+ this . _createAdjust ( ) ;
379
413
// 根据adjust分组
380
414
const dataArray = this . _adjustData ( records ) ;
381
415
@@ -392,6 +426,15 @@ class Geometry<
392
426
this . dataRecords = records ;
393
427
}
394
428
429
+ _readjustData ( records ) {
430
+ const { adjust } = this ;
431
+ if ( ! adjust ) return ;
432
+ // 根据adjust分组
433
+ const dataArray = this . _adjustData ( records ) ;
434
+
435
+ this . dataArray = dataArray ;
436
+ }
437
+
395
438
_sortData ( records ) {
396
439
const xScale = this . getXScale ( ) ;
397
440
const { field, type } = xScale ;
@@ -466,7 +509,7 @@ class Geometry<
466
509
* 如果是Category/Identity 则第一个元素走 mapping
467
510
*/
468
511
_mapping ( records ) {
469
- const { attrs, props, attrController, adjust } = this ;
512
+ const { attrs, props, attrController } = this ;
470
513
const { coord } = props ;
471
514
472
515
const { linearAttrs, nonlinearAttrs } = attrController . getAttrsByLinear ( ) ;
@@ -501,14 +544,7 @@ class Geometry<
501
544
const attrName = linearAttrs [ k ] ;
502
545
const attr = attrs [ attrName ] ;
503
546
504
- // TODO: 这块逻辑只是临时方案,需要整体考虑
505
- let value = child [ attr . field ] ;
506
- // 如果 scale变化,每组偏移需要重新计算
507
- if ( adjust ?. type === 'dodge' && attr . field === adjust . adjust . xField ) {
508
- value =
509
- attr . scale . translate ( child . origin [ attr . field ] ) -
510
- ( Math . round ( child [ attr . field ] ) - child [ attr . field ] ) ;
511
- }
547
+ const value = child [ attr . field ] ;
512
548
513
549
// 分类属性的线性映射
514
550
if ( attrController . isGroupAttr ( attrName ) ) {
0 commit comments