Skip to content

Commit

Permalink
fix: 数据对象中存在 x,y 等关键字段 (#1688)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue authored Dec 22, 2022
1 parent 52769c9 commit b6414ad
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
36 changes: 24 additions & 12 deletions packages/f2/src/components/geometry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Geometry<

// 预处理后的数据
dataArray: any;
// data 预处理后,records mapping 前的数据
dataRecords: any[];
records: any[];
mappedArray: any;
// x 轴居中
Expand Down Expand Up @@ -120,17 +122,17 @@ class Geometry<

if (!isEqual(nextAttrOptions, lastAttrOptions)) {
attrController.update(nextAttrOptions);
this.records = null;
this.dataRecords = null;
}

// 重新处理数据
if (nextData !== lastData) {
this.records = null;
this.dataRecords = null;
}

// 重新处理数据
if (nextAdjust !== lastAdjust) {
this.records = null;
this.dataRecords = null;
}

// selection 发生变化
Expand All @@ -141,13 +143,13 @@ class Geometry<

willMount() {
this._createAttrs();
if (!this.records) {
if (!this.dataRecords) {
this._processData();
}
}
willUpdate() {
this._createAttrs();
if (!this.records) {
if (!this.dataRecords) {
this._processData();
}
}
Expand Down Expand Up @@ -355,7 +357,7 @@ class Geometry<
this._sortData(records);
}

this.records = records;
this.dataRecords = records;
}

_sortData(records) {
Expand Down Expand Up @@ -443,6 +445,7 @@ class Geometry<
const { linearAttrs, nonlinearAttrs } = attrController.getAttrsByLinear();
const defaultAttrValues = attrController.getDefaultAttrValues();

const mappedRecords = [];
for (let i = 0, len = records.length; i < len; i++) {
const record = records[i];
const { children } = record;
Expand All @@ -451,6 +454,7 @@ class Geometry<
};
const firstChild = children[0];
if (children.length === 0) {
mappedRecords.push({ ...record });
continue;
}
// 非线性映射
Expand All @@ -462,6 +466,7 @@ class Geometry<
}

// 线性属性映射
const mappedChildren = [];
for (let j = 0, childrenLen = children.length; j < childrenLen; j++) {
const child = children[j];
const normalized: any = {};
Expand All @@ -482,11 +487,14 @@ class Geometry<
});

// 获取 shape 的 style
const { origin } = child;
const shapeName = attrValues.shape;
const shape = this._getShapeStyle(shapeName, child.origin);
const shape = this._getShapeStyle(shapeName, origin);
const selected = this.isSelected(child);

mix(child, attrValues, {
mappedChildren.push({
...child,
...attrValues,
normalized,
x,
y,
Expand All @@ -495,17 +503,21 @@ class Geometry<
selected,
});
}
mappedRecords.push({
...record,
children: mappedChildren,
});
}
return records;
return mappedRecords;
}

// 数据映射
mapping() {
const { records } = this;
const { dataRecords } = this;
// 数据映射
this._mapping(records);
this.records = this._mapping(dataRecords);

return records;
return this.records;
}

getClip() {
Expand Down
3 changes: 2 additions & 1 deletion packages/f2/test/canvas/lifecycle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class TestContainer extends Component {
}

describe('Canvas', () => {
it.skip('生命周期', async () => {
it('生命周期', async () => {
const ref = { current: null };
const { props } = (
<Canvas context={context} pixelRatio={1}>
Expand Down Expand Up @@ -292,6 +292,7 @@ describe('Canvas', () => {
['componentRender'],
['componentDidUpdate'],
['containerDidUpdate'],
['componentShouldUpdate'],
['componentWillUpdate'],
['componentRender'],
['componentDidUpdate'],
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 41 additions & 1 deletion packages/f2/test/components/geometry/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jsx } from '../../../src';
import { jsx, Line } from '../../../src';
import Geometry from '../../../src/components/geometry';
import { createContext, delay } from '../../util';
import { Canvas, Chart, Interval, Axis } from '../../../src';
Expand Down Expand Up @@ -87,4 +87,44 @@ describe('geometry', () => {
await delay(50);
expect(context).toMatchImageSnapshot();
});

it('原始数据中有绘图属性', async () => {
const data = [
{ type: 'FULFILLMENT', y: 171, x: '守约记录' },
{ type: 'BEHAVIOR', y: 180, x: '行为积累' },
{ type: 'CHARACTERISTICS', y: 179, x: '身份证明' },
{ type: 'CAPITAL', y: 160, x: '资产证明' },
{ type: 'RELATIONSHIP', y: 146, x: '人脉关系' },
];
const { props } = (
<Canvas context={context} pixelRatio={1} animate={false}>
<Chart data={data}>
<Line x="x" y="y" />
</Chart>
</Canvas>
);
const canvas = new Canvas(props);
await canvas.render();

const newChart = (
<Chart data={data}>
<Line x="x" y="y" />
<circle
style={{
cx: 100,
cy: 100,
r: 20,
fill: 'red',
}}
/>
</Chart>
);

await canvas.update({
children: newChart,
});

await delay(50);
expect(context).toMatchImageSnapshot();
});
});

0 comments on commit b6414ad

Please sign in to comment.