diff --git a/spec/api.chart-spec.js b/spec/api.chart-spec.js new file mode 100644 index 0000000..dd46239 --- /dev/null +++ b/spec/api.chart-spec.js @@ -0,0 +1,84 @@ +describe('c3 api chart', function () { + 'use strict'; + + var chart, args; + + beforeEach(function (done) { + chart = window.initChart(chart, args, done); + }); + + describe('width', function(){ + it('should update args', function () { + args = { + data: { + columns: [ + ['data1', 30, 200, 100], + ['data2', 50, 20, 10] + ], + }, + size: { + width: 1600, + height: 900 + } + }; + expect(true).toBeTruthy(); + }); + + it('should get width', function(){ + expect(chart.width()).toBe(1600); + + chart.resize({ + width: 400, + height: 300 + }); + + expect(chart.width()).toBe(400); + }); + + it('should set width', function(){ + chart.width(500); + + expect(chart.width()).toBe(500); + expect(chart.internal.config.size_width).toBe(500); + }); + }); + + describe('height', function(){ + it('should update args', function () { + args = { + data: { + columns: [ + ['data1', 30, 200, 100], + ['data2', 50, 20, 10] + ], + }, + size: { + width: 1600, + height: 900 + } + }; + expect(true).toBeTruthy(); + }); + + it('should get height', function(){ + expect(chart.height()).toBe(900); + + chart.resize({ + width: 400, + height: 300 + }); + + expect(chart.height()).toBe(300); + }); + + it('should set height', function(){ + chart.height(500); + + expect(chart.height()).toBe(500); + expect(chart.internal.config.size_height).toBe(500); + }); + }); + + + +}); diff --git a/src/api.chart.js b/src/api.chart.js index 0e4eacb..8ca1d96 100644 --- a/src/api.chart.js +++ b/src/api.chart.js @@ -5,6 +5,29 @@ c3_chart_fn.resize = function (size) { this.flush(); }; +c3_chart_fn.width = function(width){ + var $$ = this.internal, config = $$.config; + + if(width === undefined){ + return config.size_width; + } + + config.size_width = width; + this.flush(); +} + +c3_chart_fn.height = function(height){ + var $$ = this.internal, config = $$.config; + + if(height === undefined){ + return config.size_height; + } + + config.size_height = height; + this.flush(); +} + + c3_chart_fn.flush = function () { var $$ = this.internal; $$.updateAndRedraw({withLegend: true, withTransition: false, withTransitionForTransform: false});