diff --git a/.gitignore b/.gitignore index 7863212d3..235d38361 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,4 @@ spec/index-browserify.html # font noise from some doc tool web/docs/public - +web/docs diff --git a/dc.css b/dc.css index eb907a4fa..1614c4048 100644 --- a/dc.css +++ b/dc.css @@ -1,5 +1,15 @@ div.dc-chart { - float: left; + float: left; /*Keep this for backward compatibity, but it should actually be removed*/ +} +div.dc-responsive { + float: none; /*Keep this for backward compatibity, but it should actually be removed*/ + height: 100%; +} + + +div.dc-responsive svg { + min-width: 100%; + min-height: 100%; } .dc-chart rect.bar { diff --git a/src/base-mixin.js b/src/base-mixin.js index 5a2345eb2..ac5f583c4 100644 --- a/src/base-mixin.js +++ b/src/base-mixin.js @@ -32,7 +32,7 @@ dc.baseMixin = function (_chart) { return (height && height > _minHeight) ? height : _minHeight; }; var _heightCalc = _defaultHeightCalc; - var _width, _height; + var _width, _height, _responsive = false; var _keyAccessor = dc.pluck('key'); var _valueAccessor = dc.pluck('value'); @@ -210,6 +210,23 @@ dc.baseMixin = function (_chart) { return _chart; }; + /** + * Set or get the responsive behavior of the chart. + * + * When the chart is responsive, it will automatically fit its container's dimension. For backward compatibilty, chart are not responsive by default. + * + * @param {Boolean} [responsive] + * @return {Boolean} + * @return {dc.baseMixin} + */ + _chart.responsive = function (responsive) { + if (!arguments.length) { + return _responsive; + } + _responsive = responsive; + return _chart; + }; + /** * **mandatory** * @@ -466,6 +483,8 @@ dc.baseMixin = function (_chart) { return _chart; }; + + /** * Returns the top SVGElement for this specific chart. You can also pass in a new SVGElement, * however this is usually handled by dc internally. Resetting the SVGElement on a chart outside @@ -499,11 +518,28 @@ dc.baseMixin = function (_chart) { return generateSvg(); }; + + function sizeSvg () { if (_svg) { - _svg - .attr('width', _chart.width()) - .attr('height', _chart.height()); + if(_responsive) { + _svg + .attr({ + width : '100%', + height : '100%', + viewBox : '0 0 ' + _chart.width() + ' ' + _chart.height() + }); + _root.classed(dc.constants.RESPONSIVE_CLASS, true); + } + else { + _svg + .attr({ + width : _chart.width(), + height : _chart.height(), + viewBox : null + }); + _root.classed(dc.constants.RESPONSIVE_CLASS, false); + } } } diff --git a/src/core.js b/src/core.js index 1e6ddc599..f90d33ba1 100644 --- a/src/core.js +++ b/src/core.js @@ -20,6 +20,7 @@ var dc = { version: '<%= conf.pkg.version %>', constants: { CHART_CLASS: 'dc-chart', + RESPONSIVE_CLASS: 'dc-responsive', DEBUG_GROUP_CLASS: 'debug', STACK_CLASS: 'stack', DESELECTED_CLASS: 'deselected', diff --git a/web/css/dc.css b/web/css/dc.css index eb907a4fa..1614c4048 100644 --- a/web/css/dc.css +++ b/web/css/dc.css @@ -1,5 +1,15 @@ div.dc-chart { - float: left; + float: left; /*Keep this for backward compatibity, but it should actually be removed*/ +} +div.dc-responsive { + float: none; /*Keep this for backward compatibity, but it should actually be removed*/ + height: 100%; +} + + +div.dc-responsive svg { + min-width: 100%; + min-height: 100%; } .dc-chart rect.bar { diff --git a/web/examples/bar-responsive.html b/web/examples/bar-responsive.html new file mode 100644 index 000000000..13f42fa36 --- /dev/null +++ b/web/examples/bar-responsive.html @@ -0,0 +1,85 @@ + + + + + dc.js - Bar Chart Example + + + + + + + switch width + switch height +
+
+
+ + + + + + + diff --git a/web/examples/index.html b/web/examples/index.html index f0208250b..2fbdc61de 100644 --- a/web/examples/index.html +++ b/web/examples/index.html @@ -10,43 +10,46 @@

Examples of using dc.js

area bar extra line + bar responsive bar single select bar - box plot time + box plot time box plot complex reduce composite filtering removing - filtering + filtering heat heatmap filtering line multi focus - multi scatter + multi scatter number ord pie external labels pie - replacing data + replacing data right axis row scatter brushing scatter series - scatter + scatter select series stacked bar table on aggregated data + + table pagination diff --git a/web/js/d3.js b/web/js/d3.js index 267cc2a7e..a7a616cea 100644 --- a/web/js/d3.js +++ b/web/js/d3.js @@ -1,6 +1,6 @@ !function() { var d3 = { - version: "3.5.12" + version: "3.5.13" }; var d3_arraySlice = [].slice, d3_array = function(list) { return d3_arraySlice.call(list);