diff --git a/demos/area-00.html b/demos/area-00.html index 139efe819..a95612ad8 100644 --- a/demos/area-00.html +++ b/demos/area-00.html @@ -29,7 +29,8 @@ pixelRatio: window.devicePixelRatio }); - chart.source(data, { + chart.source(data); + chart.scale({ time: { range: [ 0, 1 ] }, diff --git a/src/chart/chart.js b/src/chart/chart.js index 7010fe956..60931fa7f 100644 --- a/src/chart/chart.js +++ b/src/chart/chart.js @@ -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); } } @@ -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 } @@ -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; } diff --git a/test/unit/chart/chart-spec.js b/test/unit/chart/chart-spec.js index af529d847..737936146 100644 --- a/test/unit/chart/chart-spec.js +++ b/test/unit/chart/chart-spec.js @@ -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); @@ -127,8 +128,7 @@ describe('chart test', () => { a: 1, b: 3, c: '2' - }, - { + }, { a: 2, b: 1, c: '1' @@ -136,8 +136,7 @@ describe('chart test', () => { a: 2, b: 4, c: '2' - }, - { + }, { a: 3, b: 5, c: '1' @@ -153,6 +152,8 @@ describe('chart test', () => { width: 500, height: 500 }); + + expect(chart.get('canvas')).to.not.be.empty; }); @@ -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({