Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include 0 as max in elastic axis if all values are negative #1156

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions spec/bar-chart-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,11 @@ describe('dc.barChart', function () {
});
});

describe('with negative data', function () {
describe('with mixed data', function () {
beforeEach(function () {
var negativeGroup = dimension.group().reduceSum(function (d) { return d.nvalue; });
var mixedGroup = dimension.group().reduceSum(function (d) { return d.nvalue; });

chart.group(negativeGroup).stack(negativeGroup).stack(negativeGroup);
chart.group(mixedGroup).stack(mixedGroup).stack(mixedGroup);
chart.x(d3.time.scale.utc().domain([makeDate(2012, 4, 20), makeDate(2012, 7, 15)]));

chart.margins({top: 30, right: 50, bottom: 30, left: 30})
Expand Down Expand Up @@ -552,13 +552,38 @@ describe('dc.barChart', function () {
});

it('should generate y axis domain dynamically', function () {
var nthText = function (n) { return d3.select(chart.selectAll('g.y text')[0][n]); };
var nthText = function (n) { return d3.select(chart.selectAll('g.axis.y .tick text')[0][n]); };

expect(nthText(0).text()).toBe('-20');
expect(nthText(1).text()).toBe('0');
expect(nthText(2).text()).toBe('20');
});
});

describe('with negative data', function () {
beforeEach(function () {
var negativeGroup = dimension.group().reduceSum(function (d) { return -Math.abs(d.nvalue); });

chart.group(negativeGroup).stack(negativeGroup).stack(negativeGroup);
chart.x(d3.time.scale.utc().domain([makeDate(2012, 4, 20), makeDate(2012, 7, 15)]));

chart.margins({top: 30, right: 50, bottom: 30, left: 30})
.elasticY(true)
.xUnits(d3.time.days.utc)
.yAxis().ticks(3);

chart.render();
});

it('should generate y axis domain dynamically', function () {
var nthText = function (n) { return d3.select(chart.selectAll('g.axis.y .tick text')[0][n]); };

expect(nthText(0).text()).toBe('-30');
expect(nthText(1).text()).toBe('-20');
expect(nthText(2).text()).toBe('-10');
expect(nthText(3).text()).toBe('0');
});
});
});

it('should not be focused by default', function () {
Expand Down
33 changes: 29 additions & 4 deletions spec/line-chart-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,11 @@ describe('dc.lineChart', function () {
});
});

describe('with negative data', function () {
describe('with mixed data', function () {
beforeEach(function () {
var negativeGroup = dimension.group().reduceSum(function (d) { return d.nvalue; });
var mixedGroup = dimension.group().reduceSum(function (d) { return d.nvalue; });

chart.group(negativeGroup).stack(negativeGroup).stack(negativeGroup);
chart.group(mixedGroup).stack(mixedGroup).stack(mixedGroup);
chart.x(d3.time.scale.utc().domain([makeDate(2012, 4, 20), makeDate(2012, 7, 15)]));

chart.margins({top: 30, right: 50, bottom: 30, left: 30})
Expand Down Expand Up @@ -531,7 +531,7 @@ describe('dc.lineChart', function () {
});

it('should generate y axis domain dynamically', function () {
var nthText = function (n) { return d3.select(chart.selectAll('g.y text')[0][n]); };
var nthText = function (n) { return d3.select(chart.selectAll('g.axis.y .tick text')[0][n]); };

expect(nthText(0).text()).toBe('-20');
expect(nthText(1).text()).toBe('0');
Expand All @@ -548,6 +548,31 @@ describe('dc.lineChart', function () {
it('should generate labels with positions corresponding to their data', lineLabelPositions);
});
});

describe('with negative data', function () {
beforeEach(function () {
var negativeGroup = dimension.group().reduceSum(function (d) { return -Math.abs(d.nvalue); });

chart.group(negativeGroup).stack(negativeGroup).stack(negativeGroup);
chart.x(d3.time.scale.utc().domain([makeDate(2012, 4, 20), makeDate(2012, 7, 15)]));

chart.margins({top: 30, right: 50, bottom: 30, left: 30})
.elasticY(true)
.xUnits(d3.time.days.utc)
.yAxis().ticks(3);

chart.render();
});

it('should generate y axis domain dynamically', function () {
var nthText = function (n) { return d3.select(chart.selectAll('g.axis.y .tick text')[0][n]); };

expect(nthText(0).text()).toBe('-30');
expect(nthText(1).text()).toBe('-20');
expect(nthText(2).text()).toBe('-10');
expect(nthText(3).text()).toBe('0');
});
});
});

describe('legend hovering', function () {
Expand Down
27 changes: 23 additions & 4 deletions spec/row-chart-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('dc.rowChart', function () {
});
});

function itShouldBehaveLikeARowChartWithGroup (groupHolder, N) {
function itShouldBehaveLikeARowChartWithGroup (groupHolder, N, xAxisTicks) {
describe('for ' + groupHolder.groupType + ' data', function () {
beforeEach(function () {
chart.group(groupHolder.group);
Expand Down Expand Up @@ -352,11 +352,30 @@ describe('dc.rowChart', function () {
});
});
});

if (xAxisTicks) {
describe('with elasticX', function () {
beforeEach(function () {
chart.elasticX(true)
.xAxis().ticks(3);

chart.render();
});

it('should generate x axis domain dynamically', function () {
var nthText = function (n) { return d3.select(chart.selectAll('g.axis .tick text')[0][n]); };

for (let i = 0; i < xAxisTicks.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crazy... all your code works well, but for some reason the let here caused Jasmine to skip the row chart tests! Completely silent failure!

dc.js is still ES5 and will be for a while yet. @sebgrohn, did you have to do anything special to get the tests to run?

Of course, I noticed 58 commits later that my test count was way down. Easy enough to fix, but whoah that's a little scary if one ES6 keyword causes a file to get dropped entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's interesting (and scary)! Sloppy to let a let (sic.) slip in there; I am truly ES6 damaged.

I don't remember if I did something out of the ordinary, and I have the code and dev env set up on another computer I can't access right now... :o

expect(nthText(i).text()).toBe(xAxisTicks[i]);
}
});
});
}
});
}

itShouldBehaveLikeARowChartWithGroup(positiveGroupHolder, 5);
itShouldBehaveLikeARowChartWithGroup(negativeGroupHolder, 5);
itShouldBehaveLikeARowChartWithGroup(mixedGroupHolder, 5);
itShouldBehaveLikeARowChartWithGroup(positiveGroupHolder, 5, ['0', '5', '10']);
itShouldBehaveLikeARowChartWithGroup(negativeGroupHolder, 5, ['-10', '-5', '0']);
itShouldBehaveLikeARowChartWithGroup(mixedGroupHolder, 5, ['-5', '0', '5']);
itShouldBehaveLikeARowChartWithGroup(largerGroupHolder, 7);
});
3 changes: 3 additions & 0 deletions src/row-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ dc.rowChart = function (parent, chartGroup) {
if (extent[0] > 0) {
extent[0] = 0;
}
if (extent[1] < 0) {
extent[1] = 0;
}
_x = d3.scale.linear().domain(extent)
.range([0, _chart.effectiveWidth()]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/stack-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ dc.stackMixin = function (_chart) {

_chart.yAxisMax = function () {
var max = d3.max(flattenStack(), function (p) {
return p.y + p.y0;
return (p.y + p.y0 > p.y0) ? (p.y + p.y0) : p.y0;
});

return dc.utils.add(max, _chart.yAxisPadding());
Expand Down