Skip to content

Commit

Permalink
fix: 双y轴, getPosition取不到point. Fixed #1004
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Sep 4, 2020
1 parent 58f2481 commit 147a1c2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/chart/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,21 @@ class Chart extends Base {
const self = this;
const coord = self.get('coord');
const xScale = self.getXScale();
const yScale = self.getYScales()[0];
const xField = xScale.field;
const yScales = self.getYScales();
// default first
let yScale = yScales[0];
let yField = yScale.field;
for (let i = 0, len = yScales.length; i < len; i++) {
const scale = yScales[i];
const field = scale.field;
if (record[field]) {
yScale = scale;
yField = field;
break;
}
}
const x = xScale.scale(record[xField]);
const yField = yScale.field;
const y = yScale.scale(record[yField]);
return coord.convertPoint({
x,
Expand Down
3 changes: 3 additions & 0 deletions src/coord/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Base {

convertPoint(point) {
const { x, y } = this._convertPoint(point);
if (!MatrixUtil.isChanged(this.matrix)) {
return { x, y };
}
const vector = [ x, y ];
Vector2.transformMat2d(vector, vector, this.matrix);

Expand Down
6 changes: 1 addition & 5 deletions src/graphic/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import MatrixUtil from './util/matrix';
import Vector2 from './util/vector2';
import { parseStyle } from './util/style-parse';

function isUnchanged(m) {
return m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1 && m[4] === 0 && m[5] === 0;
}

const ALIAS_ATTRS_MAP = {
stroke: 'strokeStyle',
fill: 'fillStyle',
Expand Down Expand Up @@ -315,7 +311,7 @@ class Element {

resetTransform(context) {
const mo = this._attrs.attrs.matrix;
if (!isUnchanged(mo)) {
if (MatrixUtil.isChanged(mo)) {
context.transform(mo[0], mo[1], mo[2], mo[3], mo[4], mo[5]);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/graphic/util/matrix.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const Matrix = {
generateDefault() {
return [ 1, 0, 0, 1, 0, 0 ];
},
isChanged(m) {
return m[0] !== 1 || m[1] !== 0 || m[2] !== 0 || m[3] !== 1 || m[4] !== 0 || m[5] !== 0;
},
multiply(m1, m2) {
const m11 = m1[0] * m2[0] + m1[2] * m2[1];
const m12 = m1[1] * m2[0] + m1[3] * m2[1];
Expand Down
69 changes: 69 additions & 0 deletions test/bug/issue-1004-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import F2 from '../../src/index';

const canvas = document.createElement('canvas');
canvas.style.width = '350px';
canvas.style.height = '300px';
document.body.appendChild(canvas);

describe('issue 1004', () => {
it('双y轴,getPosition', () => {
const series = [
{ type: 'www', date: '201805', AA: 70294 },
{ type: 'www', date: '201806', AA: 72487 },
{ type: 'www', date: '201807', AA: 73983 },
{ type: 'www', date: '201808', AA: 74169 },
{ type: 'www', date: '201809', AA: 74799 },
{ type: 'www', date: '201810', AA: 72403 },
{ type: 'eee', date: '201805', BB: 47273 },
{ type: 'eee', date: '201806', BB: 48219 },
{ type: 'eee', date: '201807', BB: 48757 },
{ type: 'eee', date: '201808', BB: 49131 },
{ type: 'eee', date: '201809', BB: 48868 },
{ type: 'eee', date: '201810', BB: 47638 }
];
const chart = new F2.Chart({
el: canvas,
syncY: true,
pixelRatio: window.devicePixelRatio
});
chart.source(series, {
AA: {
type: 'linear',
ticks: [ 40000, 60000, 80000 ]
},
BB: {
type: 'linear',
ticks: [ 40000, 60000, 80000 ]
}
});
chart.line().position('date*AA').color('type');
chart.line().position('date*BB').color('type');

chart.render();

// 第一个y轴
const position = chart.getPosition({ date: '201810', AA: 72403 });
expect(position.x).toBeCloseTo(278.9166768391927, 1);
expect(position.y).toBeCloseTo(96.97921249999999, 1);

// 第二个y轴
const position1 = chart.getPosition({ date: '201810', BB: 47638 });
expect(position1.x).toBeCloseTo(278.9166768391927, 1);
expect(position1.y).toBeCloseTo(227.305025, 1);

// 包含2部分数据
const position2 = chart.getPosition({ date: '201810', AA: 72403, BB: 47638 });
expect(position2.x).toBeCloseTo(278.9166768391927, 1);
expect(position2.y).toBeCloseTo(96.97921249999999, 1);

// 没有y轴数据
const position3 = chart.getPosition({ date: '201810' });
expect(position3.x).toBeCloseTo(278.9166768391927, 1);
expect(position3.y).toBeNaN();

// 空对象
const position4 = chart.getPosition({ });
expect(position4.x).toBeNaN();
expect(position4.y).toBeNaN();
});
});

0 comments on commit 147a1c2

Please sign in to comment.