Skip to content

Commit

Permalink
fix: make sure timeCat scale to sort by default. Closed #151.
Browse files Browse the repository at this point in the history
  • Loading branch information
simaQ committed Jun 11, 2018
1 parent b121180 commit a9e7fee
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/chart/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ class Chart extends Base {
return fields;
}

_createScale(field, data, sortable) {
_createScale(field, data) {
const scaleController = this.get('scaleController');
return scaleController.createScale(field, data, sortable);
return scaleController.createScale(field, data);
}

_adjustScale() {
Expand Down Expand Up @@ -618,10 +618,9 @@ class Chart extends Base {
/**
* 创建度量
* @param {String} field 度量对应的名称
* @param {Boolean} sortable 是否需要排序
* @return {Scale} 度量
*/
createScale(field, sortable) {
createScale(field) {
let data = this.get('data');
const filteredData = this.get('filteredData');
// 过滤导致数据为空时,需要使用全局数据
Expand All @@ -635,7 +634,7 @@ class Chart extends Base {

const scales = this.get('scales');
if (!scales[field]) {
scales[field] = this._createScale(field, data, sortable);
scales[field] = this._createScale(field, data);
}
return scales[field];
}
Expand Down
3 changes: 1 addition & 2 deletions src/chart/controller/scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ScaleController {
return cfg;
}

createScale(field, data, sortable) {
createScale(field, data) {
const self = this;
const def = self._getDef(field);
let scale;
Expand Down Expand Up @@ -112,7 +112,6 @@ class ScaleController {
if (def) {
Util.mix(cfg, def);
}
cfg.sortable = sortable;
scale = new Scale[SCALE_TYPES_MAP[type]](cfg);
}
return scale;
Expand Down
3 changes: 1 addition & 2 deletions src/geom/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,10 @@ class Geom extends Base {
}

_createScale(field) {
const sortable = this.get('sortable');
const scales = this.get('scales');
let scale = scales[field];
if (!scale) {
scale = this.get('chart').createScale(field, sortable);
scale = this.get('chart').createScale(field);
scales[field] = scale;
}
return scale;
Expand Down
2 changes: 1 addition & 1 deletion src/scale/time-cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TimeCategory extends Category {
Util.each(values, function(v, i) {
values[i] = self._toTimeStamp(v);
});
if (this.sortable) {
if (this.sortable) { // 允许排序
values.sort(function(v1, v2) {
return v1 - v2;
});
Expand Down
62 changes: 62 additions & 0 deletions test/bug/issue-151-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const expect = require('chai').expect;
const F2 = require('../../src/core');
require('../../src/geom/interval');
require('../../src/geom/adjust/dodge');
require('../../src/scale/time-cat');

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

describe('issue 145', function() {
it('Make sure TimeCat scale to sort by default.', function() {
const data = [
{ type: '欣天科技', date: '2011-12-01', value: 37.37428940721245 },
{ type: '欣天科技', date: '2012-12-01', value: 35.534330280123484 },
{ type: '欣天科技', date: '2013-12-01', value: 35.483750315503 },
{ type: '欣天科技', date: '2014-12-01', value: 36.93420323516352 },
{ type: '欣天科技', date: '2015-12-01', value: 28.440425157430234 },
{ type: '欣天科技', date: '2016-12-01', value: 26.539661716086698 },
{ type: '保利地产', date: '2007-12-01', value: 25.308530029569354 },
{ type: '保利地产', date: '2008-12-01', value: 24.721858861887316 },
{ type: '保利地产', date: '2009-12-01', value: 22.71019080164943 },
{ type: '保利地产', date: '2010-12-01', value: 20.684340451124587 },
{ type: '保利地产', date: '2011-12-01', value: 21.183867630801377 },
{ type: '保利地产', date: '2012-12-01', value: 19.437525921551583 },
{ type: '保利地产', date: '2013-12-01', value: 17.33174541312307 },
{ type: '保利地产', date: '2014-12-01', value: 17.404313051756663 },
{ type: '保利地产', date: '2015-12-01', value: 18.410947230529278 },
{ type: '保利地产', date: '2016-12-01', value: 14.94209942310791 }
];
const chart = new F2.Chart({
id: 'issue-151',
pixelRatio: window.devicePixelRatio
});
chart.source(data);
chart.scale('date', { type: 'timeCat', tickCount: 3 });
chart.scale('value', { tickCount: 5 });
chart
.interval()
.position('date*value')
.color('type')
.adjust({
type: 'dodge',
marginRatio: 0.05
});
chart.render();

const xScale = chart.getXScale();
const ticks = xScale.getTicks();
const tickLabels = [];
ticks.map(obj => {
tickLabels.push(obj.text);
return obj;
});
expect(tickLabels).to.eql([ '2007-12-01', '2012-12-01', '2016-12-01' ]);
});
});

0 comments on commit a9e7fee

Please sign in to comment.