Permalink
Browse files

fix: Fix sorting problem for categorical data. Closed #257.

  • Loading branch information...
simaQ committed Aug 17, 2018
1 parent 73653f8 commit 3a129289515fff4a04e84823a517e38b2f103356
Showing with 72 additions and 2 deletions.
  1. +3 −2 src/geom/base.js
  2. +69 −0 test/bug/issue-257-spec.js
@@ -287,8 +287,8 @@ class Geom extends Base {
_sort(mappedArray) {
const self = this;
const xScale = self.getXScale();
const { field, type, isLinear } = xScale;
if ((isLinear || type === 'timeCat') && xScale.values.length > 1) { // sort only for linear and timeCat type.
const { field, type } = xScale;
if (type !== 'identity' && xScale.values.length > 1) {
Util.each(mappedArray, itemArr => {
itemArr.sort((obj1, obj2) => {
if (type === 'timeCat') {
@@ -298,6 +298,7 @@ class Geom extends Base {
});
});
}
self.set('hasSorted', true);
self.set('dataArray', mappedArray);
}
@@ -0,0 +1,69 @@
const expect = require('chai').expect;
const F2 = require('../../src/core');
require('../../src/geom/line');
const canvas = document.createElement('canvas');
canvas.width = 375;
canvas.height = 260;
canvas.id = 'issue257';
document.body.appendChild(canvas);
describe('issue 257', () => {
it('Issue257', () => {
const data = [
{ year: '2013', manage: 100, netAmount: 200, name: '经营活动产生现金流' },
{ year: '2014', manage: 110, netAmount: 150, name: '经营活动产生现金流' },
{ year: '2015', manage: 115, netAmount: 123, name: '经营活动产生现金流' },
{ year: '2016', manage: 230, netAmount: 250, name: '经营活动产生现金流' },
{ year: '2017', manage: 180, netAmount: 280, name: '经营活动产生现金流' },
{ year: '2018', manage: 160, netAmount: 230, name: '经营活动产生现金流' },
{ year: '2013', manage: 200, netAmount: 200, name: '投资活动产生现金流' },
{ year: '2014', manage: 140, netAmount: 150, name: '投资活动产生现金流' },
{ year: '2015', manage: 150, netAmount: 123, name: '投资活动产生现金流' },
{ year: '2016', manage: 230, netAmount: 250, name: '投资活动产生现金流' },
{ year: '2017', manage: 196, netAmount: 280, name: '投资活动产生现金流' },
{ year: '2018', manage: 150, netAmount: 230, name: '投资活动产生现金流' },
{ year: '2013', manage: 200, netAmount: 200, name: '筹资活动产生现金流' },
{ year: '2014', manage: 370, netAmount: 150, name: '筹资活动产生现金流' },
{ year: '2015', manage: 400, netAmount: 123, name: '筹资活动产生现金流' },
{ year: '2016', manage: -450, netAmount: 250, name: '筹资活动产生现金流' },
{ year: '2017', manage: 260, netAmount: 280, name: '筹资活动产生现金流' },
{ year: '2018', manage: 140, netAmount: 230, name: '筹资活动产生现金流' }]
;
const chart = new F2.Chart({
id: 'issue257',
pixelRatio: window.devicePixelRatio
});
chart.source(data);
const geom = chart.line().position('year*netAmount');
chart.render();
const geomData = geom.get('dataArray')[0];
expect(geomData.length).to.equal(18);
console.log(geomData);
geomData.map((obj, index) => {
if (index < 3) {
expect(obj._originY).to.equal(200);
} else if (index < 6) {
expect(obj._originY).to.equal(150);
} else if (index < 9) {
expect(obj._originY).to.equal(123);
} else if (index < 12) {
expect(obj._originY).to.equal(250);
} else if (index < 15) {
expect(obj._originY).to.equal(280);
} else {
expect(obj._originY).to.equal(230);
}
return obj;
});
chart.destroy();
document.body.removeChild(canvas);
});
});

0 comments on commit 3a12928

Please sign in to comment.