Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: geom repaint #915

Merged
merged 2 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/chart/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ class Chart extends Base {
}

_initGeom(geom) {
if (geom.get('isInit')) {
return;
}
const coord = this.get('coord');
const data = this.get('filteredData');
const colDefs = this.get('colDefs');
Expand Down Expand Up @@ -577,10 +580,14 @@ class Chart extends Base {
const canvas = this.get('canvas');
const geoms = this.get('geoms');

if (!rendered) {
// 已经渲染过
if (rendered) {
this._initGeoms();
} else {
this.init();
this.set('rendered', true);
}

this.emit(EVENT_BEFORE_RENDER);

Chart.plugins.notify(this, 'beforeGeomDraw');
Expand Down Expand Up @@ -756,13 +763,8 @@ class Chart extends Base {
* @param {Geom} geom geometry instance
*/
addGeom(geom) {
const rendered = this.get('rendered');
const geoms = this.get('geoms');
geoms.push(geom);
// 如果图表已经渲染过了,则直接初始化geom
if (rendered) {
this._initGeom(geom);
}
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/geom/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,21 @@ class Geom extends Base {
visible: true,
connectNulls: false,
// 是否丢弃没有值的分组。
ignoreEmptyGroup: false
ignoreEmptyGroup: false,
// 是否已经初始化
isInit: false
};
}

init() {
const self = this;
const isInit = self.get('isInit');
if (isInit) {
return;
}
self._initAttrs();
self._processData();
self.set('isInit', true);
}

_getGroupScales() {
Expand Down Expand Up @@ -794,6 +801,7 @@ class Geom extends Base {
this.set('data', data);
// 改变数据后,情况度量,因为需要重新实例化
this.set('scales', {});
this.set('isInit', false);
this.init();
}

Expand All @@ -806,6 +814,7 @@ class Geom extends Base {
}

reset() {
this.set('isInit', false);
this.set('attrs', {});
this.set('attrOptions', {});
this.set('adjust', null);
Expand All @@ -817,6 +826,7 @@ class Geom extends Base {
}

destroy() {
this.set('isInit', false);
this.clear();
super.destroy();
}
Expand Down
61 changes: 61 additions & 0 deletions test/unit/geom/geom-init-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const expect = require('chai').expect;
const Chart = require('../../../src/chart/chart');

require('../../../src/geom/index');

describe('Geom init', function() {

const canvas = document.createElement('canvas');
canvas.width = 500;
canvas.height = 500;
document.body.appendChild(canvas);
let chart;

it('repetition Geom', function() {

chart = new Chart({
el: canvas
});

const data = [
{
date: '2020-05-10',
dailyProfit: '0.00',
profit: -15.69
},
{
date: '2020-05-20',
dailyProfit: '0.00',
profit: 15.69
},
{
date: '2020-06-10',
dailyProfit: '0.00',
profit: 115.69
},
{
date: '2020-07-10',
dailyProfit: '0.00',
profit: 215.69
}
];


chart.source(data);
chart.axis('date', {
label: false
});

chart.line().position('date*profit');
expect(chart.get('geoms')[0].get('isInit')).to.be.false;
chart.render();
expect(chart.get('geoms').length).equal(1);
expect(chart.get('geoms')[0].get('isInit')).to.be.true;
chart.line().position('date*profit');
chart.render();

expect(chart.get('geoms').length).equal(2);

});

});
2 changes: 2 additions & 0 deletions test/unit/geom/geom-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,13 @@ describe('test geoms', function() {
];
geom.set('data', data);
geom.position('a*b').color('c');
geom.set('isInit', false);
geom.init();
const dataArray = geom.get('dataArray');
expect(dataArray.length).equal(4);

geom.set('ignoreEmptyGroup', true);
geom.set('isInit', false);
geom.init();
const dataArray1 = geom.get('dataArray');
expect(dataArray1.length).equal(2);
Expand Down