Skip to content

Commit 68c63b1

Browse files
committed
fix: 修复 dodae 平移显示问题
1 parent 43f3e5a commit 68c63b1

File tree

6 files changed

+76
-40
lines changed

6 files changed

+76
-40
lines changed

packages/f2/src/components/geometry/index.tsx

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class Geometry<
168168
this._createAttrs();
169169
if (!this.dataRecords) {
170170
this._processData();
171+
} else {
172+
this._readjustData(this.dataRecords);
171173
}
172174
}
173175

@@ -214,6 +216,46 @@ class Geometry<
214216
};
215217
}
216218

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+
217259
_adjustScales() {
218260
const { attrs, props, startOnZero: defaultStartOnZero } = this;
219261
const { chart, startOnZero = defaultStartOnZero, coord, adjust } = props;
@@ -287,56 +329,46 @@ class Geometry<
287329
const scale = scales[i];
288330
if (scale.isCategory) {
289331
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;
291334
}
292335
}
293336
}
294337
}
295338

296339
_adjustData(records) {
297-
const { attrs, props } = this;
298-
const { adjust } = props;
299-
340+
const { adjust } = this;
300341
// groupedArray 是二维数组
301342
const groupedArray = records.map((record) => record.children);
302343

303344
if (!adjust) {
304345
return groupedArray;
305346
}
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+
}
316362
}
317363

318-
if (adjustType === 'Dodge') {
364+
if (adjust.type === 'dodge') {
319365
for (let i = 0, len = groupedArray.length; i < len; i++) {
320366
// 如果是dodge, 需要处理数字再处理
321367
this._numberic(groupedArray[i]);
322368
}
323-
// @ts-ignore
324-
adjustCfg.adjustNames = ['x'];
325369
}
326370

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);
340372

341373
// process 返回的是新数组,所以要修改 records
342374
records.forEach((record, index: number) => {
@@ -376,6 +408,8 @@ class Geometry<
376408
const data = this._saveOrigin(originData);
377409
// 根据分类度量进行数据分组
378410
const records = this._groupData(data);
411+
412+
this._createAdjust();
379413
// 根据adjust分组
380414
const dataArray = this._adjustData(records);
381415

@@ -392,6 +426,15 @@ class Geometry<
392426
this.dataRecords = records;
393427
}
394428

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+
395438
_sortData(records) {
396439
const xScale = this.getXScale();
397440
const { field, type } = xScale;
@@ -466,7 +509,7 @@ class Geometry<
466509
* 如果是Category/Identity 则第一个元素走 mapping
467510
*/
468511
_mapping(records) {
469-
const { attrs, props, attrController, adjust } = this;
512+
const { attrs, props, attrController } = this;
470513
const { coord } = props;
471514

472515
const { linearAttrs, nonlinearAttrs } = attrController.getAttrsByLinear();
@@ -501,14 +544,7 @@ class Geometry<
501544
const attrName = linearAttrs[k];
502545
const attr = attrs[attrName];
503546

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];
512548

513549
// 分类属性的线性映射
514550
if (attrController.isGroupAttr(attrName)) {

packages/f2/src/components/zoom/zoomUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function updateCategoryRange(scale: Scale, originScale: Scale, range: ZoomRange)
2121
const valueEnd = end * len;
2222

2323
// 保持滑动时个数的稳定
24-
const count = Math.round(valueEnd - valueStart);
24+
const count = Math.ceil(valueEnd - valueStart);
2525
const sliceSatrt = Math.round(valueStart);
2626

2727
// 从原始数据里截取需要显示的数据

0 commit comments

Comments
 (0)