Skip to content

Commit

Permalink
feat: add chart.scale()
Browse files Browse the repository at this point in the history
  • Loading branch information
simaQ committed Feb 1, 2018
1 parent 3a44b3d commit dee7019
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
3 changes: 2 additions & 1 deletion demos/area-00.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
pixelRatio: window.devicePixelRatio
});

chart.source(data, {
chart.source(data);
chart.scale({
time: {
range: [ 0, 1 ]
},
Expand Down
17 changes: 14 additions & 3 deletions src/chart/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class Chart extends Base {
const colDefs = this.get('colDefs');
if (colDefs) {
const scaleController = this.get('scaleController');
scaleController.defs = colDefs;
Util.mix(scaleController.defs, colDefs);
}
}

Expand All @@ -374,7 +374,6 @@ class Chart extends Base {
frontPlot: self.get('frontPlot'),
backPlot: self.get('backPlot')
}));
self.initColDefs();
Chart.plugins.notify(self, 'init'); // TODO: beforeInit afterInit
}

Expand All @@ -394,8 +393,20 @@ class Chart extends Base {
source(data, colDefs) {
this.set('data', data);
if (colDefs) {
this.set('colDefs', colDefs);
this.scale(colDefs);
}
return this;
}

scale(field, cfg) {
const colDefs = this.get('colDefs') || {};
if (Util.isObject(field)) {
Util.mix(colDefs, field);
} else {
colDefs[field] = cfg;
}

this.set('colDefs', colDefs);
this.initColDefs();
return this;
}
Expand Down
32 changes: 28 additions & 4 deletions test/unit/chart/chart-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Chart = require('../../../src/chart/chart');
const { Guide } = require('../../../src/plugin/index');
require('../../../src/geom/shape/index');
require('../../../src/geom/adjust/index');
require('../../../src/component/guide');

Chart.plugins.register(Guide);

Expand Down Expand Up @@ -127,17 +128,15 @@ describe('chart test', () => {
a: 1,
b: 3,
c: '2'
},
{
}, {
a: 2,
b: 1,
c: '1'
}, {
a: 2,
b: 4,
c: '2'
},
{
}, {
a: 3,
b: 5,
c: '1'
Expand All @@ -153,6 +152,8 @@ describe('chart test', () => {
width: 500,
height: 500
});

expect(chart.get('canvas')).to.not.be.empty;
});


Expand All @@ -167,6 +168,29 @@ describe('chart test', () => {
expect(chart.get('colDefs').a.min).equal(0);
});

it('scale', function() {
chart.scale('a', {
max: 10
});
chart.scale({
b: {
nice: false
}
});
expect(chart.get('colDefs').a).eql({ max: 10 });
expect(chart.get('colDefs').b).eql({ nice: false });
expect(chart.get('scaleController').defs).eql({ a: { max: 10 }, b: { nice: false } });

chart.scale({
a: {
min: 0,
max: 4
}
});
expect(chart.get('colDefs').a).eql({ min: 0, max: 4 });
expect(chart.get('scaleController').defs).eql({ a: { min: 0, max: 4 }, b: { nice: false } });
});

it('guide', function() {
expect(chart.guide().text).not.equal(undefined);
chart.guide().text({
Expand Down

0 comments on commit dee7019

Please sign in to comment.