Skip to content

Commit

Permalink
add nicing
Browse files Browse the repository at this point in the history
  • Loading branch information
fin committed Aug 25, 2015
1 parent 8aca755 commit eed88c7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,51 @@
resetFunct();
return dimension;
};

dimension.$nice = -1;
dimension.nice = function(tick_count) {
if (!arguments.length) {
return dimension.$nice;
}
dimension.$nice = tick_count;

var nice_level = parseInt(tick_count);

if (isNaN(nice_level) && nice_level) {
scale.nice();
} else {
if (!isNaN(nice_level) && nice_level >= 0) {
scale.nice(nice_level);
}
else {
dimension.domain(dimension.$domain); /* reset domain */
}
}
return dimension;
};

dimension.$domain = [];
var orig_domain = dimension.domain;
dimension.domain = function(val) {
console.log(val);
if (!arguments.length) {
return dimension.$domain;
}
dimension.$domain = val;
orig_domain.apply(scale, arguments);

var nice_level = parseInt(dimension.$nice);

if (isNaN(nice_level) && nice_level) {
scale.nice();
console.log('nicing w/o args');
}
if (!isNaN(nice_level) && nice_level >= 0) {
scale.nice(nice_level);
console.log('nicing w/ args');
}
return dimension;
}
};

var createAxisScale = function(dimension, opts, axis) {
Expand Down
3 changes: 3 additions & 0 deletions test/tests/base-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ describe('d4.base', function() {
// min is a function of d4
y.min(1);

// nice is a d4-enhanced wrapper for the d3.scale function
y.nice(false);

}).to.not.throw(Error);
});
});
Expand Down
22 changes: 21 additions & 1 deletion test/tests/smoke-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ describe('smoke tests', function() {
expect(d3.select('.series0')[0][0]).to.not.be.an('null');
});

it('should render a basic row chart, while nicing values', function() {
var data = [
{ x: '2010', y:-10 },
{ x: '2011', y:20 },
{ x: '2012', y:30 },
{ x: '2013', y:40 },
{ x: '2014', y:51 },
];
var chart = d4.charts.row();
chart.y(function(y) {
y.scale('linear');
y.nice(true);
});
d3.select('#chart')
.datum(data)
.call(chart);
console.log(chart);
expect(d3.select('.series0')[0][0]).to.not.be.an('null');
});

it('should render a basic stacked column chart', function() {
var data = [
{ year: '2010', unitsSold: 200, salesman : 'Bob' },
Expand Down Expand Up @@ -309,4 +329,4 @@ describe('smoke tests', function() {

expect(d3.select('.series0')[0][0]).to.not.be.an('null');
});
});
});

0 comments on commit eed88c7

Please sign in to comment.