Skip to content

Commit

Permalink
fix: fix the error when values are all null in linear scale.
Browse files Browse the repository at this point in the history
  • Loading branch information
simaQ committed Jun 1, 2018
1 parent f9384fd commit 54787f3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/chart/controller/scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const SCALE_TYPES_MAP = {
};

function getRange(values) {
if (!values.length) { // 空数组需要手动设置 min:0 max:0,否则 linear scale 会报错
return {
min: 0,
max: 0
};
}
const max = Math.max.apply(null, values);
const min = Math.min.apply(null, values);
return {
Expand Down
44 changes: 44 additions & 0 deletions test/bug/issue-140-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const expect = require('chai').expect;
const F2 = require('../../src/core');
require('../../src/geom/line');

const canvas = document.createElement('canvas');
canvas.width = 500;
canvas.height = 500;
canvas.id = 'issue-140';
canvas.style.position = 'fixed';
canvas.style.top = 0;
canvas.style.left = 0;
document.body.appendChild(canvas);

describe('issue 140', () => {
it('Linear scale values are all null.', function() {
const data = [
{ type: '湿度/%', value: null, date: '2018-06-01 11:09:00' },
{ type: '温度/℃', value: null, date: '2018-06-01 11:09:00' },
{ type: '湿度/%', value: null, date: '2018-06-01 11:10:00' },
{ type: '温度/℃', value: null, date: '2018-06-01 11:10:00' }
];
const chart = new F2.Chart({
id: 'issue-140',
pixelRatio: window.devicePixelRatio
});
chart.source(data, {
value: {
nice: false
}
});
const line = chart.line().position('date*value').color('type');
chart.render();

const valueScale = line.getYScale();
expect(valueScale.min).to.equal(0);
expect(valueScale.max).to.equal(0);

chart.scale('value', null);
chart.changeData(data);
const newValueScale = line.getYScale();
expect(newValueScale.type).eql('identity');
});
});

0 comments on commit 54787f3

Please sign in to comment.