Skip to content

Commit

Permalink
fix(#4141): avoid cost O(n^2) to getComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky committed Sep 16, 2022
1 parent db5f035 commit 237c69b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/chart/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1978,9 +1978,10 @@ export class View extends Base {
* @param isUpdate
*/
private renderComponents(isUpdate: boolean) {
const components = this.getComponents();
// 先全部清空,然后 render
for (let i = 0; i < this.getComponents().length; i++) {
const co = this.getComponents()[i];
for (let i = 0; i < components.length; i++) {
const co = components[i];
(co.component as GroupComponent).render();
}
}
Expand Down
71 changes: 71 additions & 0 deletions tests/bugs/4141-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Chart } from '../../src';
import { createDiv } from '../util/dom';

describe('#4141', () => {
it('render large-amount-of-annotations, should cost less than 5s', () => {
const data: any[] = [];
for (let i = 0; i < 1000; i++) {
data.push(
...[
{ type: '17 岁以下', value: 654, percent: 0.02 },
{ type: '18-24 岁', value: 4400, percent: 0.2 },
{ type: '25-29 岁', value: 5300, percent: 0.24 },
{ type: '30-39 岁', value: 6200, percent: 0.28 },
{ type: '40-49 岁', value: 3300, percent: 0.14 },
{ type: '50 岁以上', value: 1500, percent: 0.06 },
{ type: '未知', value: 654, percent: 0.02 },
]
);
}
const startTime = performance.now();
const chart = new Chart({
container: createDiv(),
autoFit: true,
height: 500,
padding: [50, 20, 50, 20],
});
chart.data(data);
chart.scale('value', {
alias: '销售额(万)',
});

chart.axis('type', {
tickLine: {
alignTick: false,
},
});
chart.axis('value', false);

chart.tooltip({
showMarkers: false,
});
chart.interval({ sortable: false, zIndexReversed: false, sortZIndex: false }).position('type*value');
chart.interaction('element-active');

// 添加文本标注
data.forEach((item) => {
chart
.annotation()
.text({
position: [item.type, item.value],
content: item.value,
style: {
textAlign: 'center',
},
offsetY: -30,
})
.text({
position: [item.type, item.value],
content: (item.percent * 100).toFixed(0) + '%',
style: {
textAlign: 'center',
},
offsetY: -12,
});
});

chart.render();
// Actual cost less than 1500ms.
expect(performance.now() - startTime).toBeLessThan(5000);
});
});

0 comments on commit 237c69b

Please sign in to comment.