Skip to content

Commit 5776594

Browse files
xAxisPaddingUnit should be the d3 interval not the name of it
fixes #1326 fixes #1320 fixes #1420 ref #892
1 parent bff2ec3 commit 5776594

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

spec/bubble-chart-spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ describe('dc.bubbleChart', function () {
471471

472472
describe('with 10 day padding', function () {
473473
beforeEach(function () {
474-
chart.xAxisPadding(10)
474+
chart.xAxisPaddingUnit(d3.utcDay)
475+
.xAxisPadding(10)
475476
.render();
476477
});
477478
it('should stretch the domain appropriately', function () {
@@ -481,7 +482,7 @@ describe('dc.bubbleChart', function () {
481482

482483
describe('with 2 month padding', function () {
483484
beforeEach(function () {
484-
chart.xAxisPaddingUnit('month')
485+
chart.xAxisPaddingUnit(d3.utcMonth)
485486
.xAxisPadding(2)
486487
.render();
487488
});

src/coordinate-grid-mixin.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dc.coordinateGridMixin = function (_chart) {
3333
var _xAxis = d3.axisBottom();
3434
var _xUnits = dc.units.integers;
3535
var _xAxisPadding = 0;
36-
var _xAxisPaddingUnit = 'day';
36+
var _xAxisPaddingUnit = d3.timeDay;
3737
var _xElasticity = false;
3838
var _xAxisLabel;
3939
var _xAxisLabelPadding = 0;
@@ -378,13 +378,15 @@ dc.coordinateGridMixin = function (_chart) {
378378
* use when applying xAxis padding if elasticX is turned on and if x-axis uses a time dimension;
379379
* otherwise it is ignored.
380380
*
381-
* Padding unit is a string that will be used when the padding is calculated. Available parameters are
382-
* the available d3 time intervals; see
383-
* {@link https://github.com/d3/d3-time/blob/master/README.md#intervals d3.timeInterval}.
381+
* The padding unit should be a
382+
* [d3 time interval](https://github.com/d3/d3-time/blob/master/README.md#_interval).
383+
* For backward compatibility with dc.js 2.0, it can also be the name of a d3 time interval
384+
* ('day', 'hour', etc). Available arguments are the
385+
* [d3 time intervals](https://github.com/d3/d3-time/blob/master/README.md#intervals d3.timeInterval).
384386
* @method xAxisPaddingUnit
385387
* @memberof dc.coordinateGridMixin
386388
* @instance
387-
* @param {String} [unit='days']
389+
* @param {String} [unit=d3.timeDay]
388390
* @returns {String|dc.coordinateGridMixin}
389391
*/
390392
_chart.xAxisPaddingUnit = function (unit) {

src/utils.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ dc.utils.toTimeFunc = function (t) {
127127

128128
/**
129129
* Arbitrary add one value to another.
130-
* @method add
131-
* @memberof dc.utils
132130
*
133131
* If the value l is of type Date, adds r units to it. t becomes the unit.
134132
* For example dc.utils.add(dt, 3, 'week') will add 3 (r = 3) weeks (t= 'week') to dt.
@@ -138,10 +136,14 @@ dc.utils.toTimeFunc = function (t) {
138136
* dc.utils.add(30, 10) will give 40 and dc.utils.add(30, '10') will give 33.
139137
*
140138
* They also generate strange results if l is a string.
139+
* @method add
140+
* @memberof dc.utils
141141
* @param {Date|Number} l the value to modify
142142
* @param {String|Number} r the amount by which to modify the value
143-
* @param {String} [t] if `l` is a `Date`, then possible values are
144-
* 'millis', 'second', 'minute', 'hour', 'day', 'week', 'month', and 'year'
143+
* @param {Function|String} [t=d3.timeDay] if `l` is a `Date`, then this should be a
144+
* [d3 time interval](https://github.com/d3/d3-time/blob/master/README.md#_interval).
145+
* For backward compatibility with dc.js 2.0, it can also be the name of an interval, i.e.
146+
* 'millis', 'second', 'minute', 'hour', 'day', 'week', 'month', or 'year'
145147
* @returns {Date|Number}
146148
*/
147149
dc.utils.add = function (l, r, t) {
@@ -156,8 +158,11 @@ dc.utils.add = function (l, r, t) {
156158
if (t === 'millis') {
157159
return new Date(l.getTime() + r);
158160
}
159-
t = t || 'day';
160-
return d3[dc.utils.toTimeFunc(t)].offset(l, r);
161+
t = t || d3.timeDay;
162+
if (typeof t !== 'function') {
163+
t = d3[dc.utils.toTimeFunc(t)];
164+
}
165+
return t.offset(l, r);
161166
} else if (typeof r === 'string') {
162167
var percentage = (+r / 100);
163168
return l > 0 ? l * (1 + percentage) : l * (1 - percentage);
@@ -168,8 +173,7 @@ dc.utils.add = function (l, r, t) {
168173

169174
/**
170175
* Arbitrary subtract one value from another.
171-
* @method subtract
172-
* @memberof dc.utils
176+
*
173177
* If the value l is of type Date, subtracts r units from it. t becomes the unit.
174178
* For example dc.utils.subtract(dt, 3, 'week') will subtract 3 (r = 3) weeks (t= 'week') from dt.
175179
*
@@ -178,10 +182,14 @@ dc.utils.add = function (l, r, t) {
178182
* dc.utils.subtract(30, 10) will give 20 and dc.utils.subtract(30, '10') will give 27.
179183
*
180184
* They also generate strange results if l is a string.
185+
* @method subtract
186+
* @memberof dc.utils
181187
* @param {Date|Number} l the value to modify
182188
* @param {String|Number} r the amount by which to modify the value
183-
* @param {String} [t] if `l` is a `Date`, then possible values are
184-
* 'millis', 'second', 'minute', 'hour', 'day', 'week', 'month', and 'year'
189+
* @param {Function|String} [t=d3.timeDay] if `l` is a `Date`, then this should be a
190+
* [d3 time interval](https://github.com/d3/d3-time/blob/master/README.md#_interval).
191+
* For backward compatibility with dc.js 2.0, it can also be the name of an interval, i.e.
192+
* 'millis', 'second', 'minute', 'hour', 'day', 'week', 'month', or 'year'
185193
* @returns {Date|Number}
186194
*/
187195
dc.utils.subtract = function (l, r, t) {
@@ -196,8 +204,11 @@ dc.utils.subtract = function (l, r, t) {
196204
if (t === 'millis') {
197205
return new Date(l.getTime() - r);
198206
}
199-
t = t || 'day';
200-
return d3[dc.utils.toTimeFunc(t)].offset(l, -r);
207+
t = t || d3.timeDay;
208+
if (typeof t !== 'function') {
209+
t = d3[dc.utils.toTimeFunc(t)];
210+
}
211+
return t.offset(l, -r);
201212
} else if (typeof r === 'string') {
202213
var percentage = (+r / 100);
203214
return l < 0 ? l * (1 + percentage) : l * (1 - percentage);

0 commit comments

Comments
 (0)