Permalink
Browse files

fix: when Text shape's text attribute is updated, the textArr attribu…

…te should be reset. Closed #302.
  • Loading branch information...
simaQ committed Sep 3, 2018
1 parent b916e27 commit 1625a22e52e529d3c5bf2c45b620c95d139fd160
Showing with 82 additions and 21 deletions.
  1. +17 −17 src/component/list.js
  2. +5 −4 src/graphic/shape/text.js
  3. +60 −0 test/bug/issue-302-spec.js
@@ -80,20 +80,25 @@ class List {

_renderTitle(title) {
title = title || this.title;

let titleShape = this.titleShape;
let titleHeight = 0;

if (this.showTitle && title) {
const { wrapper, titleStyle } = this;
const titleShape = wrapper.addShape('text', {
className: 'title',
attrs: Util.mix({
x: 0,
y: 0,
text: title
}, titleStyle)
});
if (titleShape && !titleShape.get('destroyed')) {
titleShape.attr('text', title);
} else {
const { wrapper, titleStyle } = this;
titleShape = wrapper.addShape('text', {
className: 'title',
attrs: Util.mix({
x: 0,
y: 0,
text: title
}, titleStyle)
});
this.titleShape = titleShape;
}
titleHeight = titleShape.getBBox().height + this.titleGap;
this.titleShape = titleShape;
}
this._titleHeight = titleHeight;
}
@@ -375,12 +380,7 @@ class List {
}

setTitle(title) {
const titleShape = this.titleShape;
if (titleShape) {
titleShape.attr('text', title);
} else {
this._renderTitle(title);
}
this._renderTitle(title);
}

clearItems() {
@@ -41,13 +41,14 @@ class Text extends Shape {

if (attrs.text) {
const text = attrs.text;
let textArr;
let textArr = null;
let lineCount = 1;
if (Util.isString(text) && (text.indexOf('\n') !== -1)) {
textArr = text.split('\n');
const lineCount = textArr.length;
attrs.lineCount = lineCount;
attrs.textArr = textArr;
lineCount = textArr.length;
}
attrs.lineCount = lineCount;
attrs.textArr = textArr;
}
this.set('attrs', attrs);
}
@@ -0,0 +1,60 @@
const expect = require('chai').expect;
const F2 = require('../../src/core');
require('../../src/geom/interval');
const Tooltip = require('../../src/plugin/tooltip');

const canvas = document.createElement('canvas');
canvas.width = 375;
canvas.height = 260;
canvas.id = 'issue302';
document.body.appendChild(canvas);

// function snapEqual(a, b) {
// return Math.abs(a - b) < 0.01;
// }

describe('issue 302', () => {
it('Issue 302', () => {
const data = [
{ country: '巴西', population: 100 },
{ country: '印尼印尼\n印尼印尼', population: 234 },
{ country: '美国', population: 290 },
{ country: '印度', population: 104 },
{ country: '中国', population: 131 }
];
const chart = new F2.Chart({
id: 'issue302',
pixelRatio: window.devicePixelRatio,
plugins: Tooltip
});

chart.source(data);
chart.tooltip({
showTitle: true,
offsetY: 60
});
chart.interval().position('country*population');
chart.render();

let point = chart.getPosition({
country: '印尼印尼\n印尼印尼', population: 234
});
chart.showTooltip(point);

let titleShape = chart.get('tooltipController').tooltip.container.titleShape;
expect(titleShape.getBBox().height).to.equal(25.68);
expect(titleShape.attr('textArr')).to.eql([ '印尼印尼', '印尼印尼' ]);

point = chart.getPosition({
country: '美国', population: 290
});
chart.showTooltip(point);

titleShape = chart.get('tooltipController').tooltip.container.titleShape;
expect(titleShape.getBBox().height).to.equal(12);
expect(titleShape.attr('textArr')).to.be.null;

chart.destroy();
document.body.removeChild(canvas);
});
});

0 comments on commit 1625a22

Please sign in to comment.