Skip to content

Commit c374ef2

Browse files
gazal-kgordonwoodhull
authored andcommitted
hadn't considered case when y axis range is fully negative
separated the code for aligning axes into function alignYAxisRanges to reduce cyclomatic complexity
1 parent cc4c15e commit c374ef2

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

spec/composite-chart-spec.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* global appendChartID, loadDateFixture, makeDate */
22
describe('dc.compositeChart', function () {
33
var id, chart, data, dateDimension, dateValueSumGroup, dateValueNegativeSumGroup,
4-
dateIdSumGroup, dateGroup;
4+
dateIdSumGroup, dateIdNegativeSumGroup, dateGroup;
55

66
beforeEach(function () {
77
data = crossfilter(loadDateFixture());
88
dateDimension = data.dimension(function (d) { return d3.time.day.utc(d.dd); });
99
dateValueSumGroup = dateDimension.group().reduceSum(function (d) { return d.value; });
1010
dateValueNegativeSumGroup = dateDimension.group().reduceSum(function (d) { return -d.value; });
1111
dateIdSumGroup = dateDimension.group().reduceSum(function (d) { return d.id; });
12+
dateIdNegativeSumGroup = dateDimension.group().reduceSum(function (d) { return -d.id; });
1213
dateGroup = dateDimension.group();
1314

1415
id = 'composite-chart';
@@ -598,9 +599,9 @@ describe('dc.compositeChart', function () {
598599
chart
599600
.compose([
600601
leftChart = dc.barChart(chart)
601-
.group(dateIdSumGroup, 'Date Value Group'),
602+
.group(dateIdSumGroup, 'Date ID Group'),
602603
rightChart = dc.lineChart(chart)
603-
.group(dateValueNegativeSumGroup, 'Date ID Group')
604+
.group(dateValueNegativeSumGroup, 'Date Value Group')
604605
.useRightYAxis(true)
605606
])
606607
.render();
@@ -621,6 +622,35 @@ describe('dc.compositeChart', function () {
621622
});
622623
});
623624
});
625+
626+
describe('when composing left and right axes charts with negative values', function () {
627+
var leftChart, rightChart;
628+
beforeEach(function () {
629+
chart
630+
.compose([
631+
leftChart = dc.barChart(chart)
632+
.group(dateIdNegativeSumGroup, 'Date ID Group'),
633+
rightChart = dc.lineChart(chart)
634+
.group(dateValueNegativeSumGroup, 'Date Value Group')
635+
.useRightYAxis(true)
636+
])
637+
.render();
638+
});
639+
640+
it('the axis baselines shouldn\'t match', function () {
641+
expect(leftChart.y()(0)).not.toEqual(rightChart.y()(0));
642+
});
643+
644+
describe('with alignYAxes', function () {
645+
beforeEach(function () {
646+
chart.alignYAxes(true)
647+
.render();
648+
});
649+
it('the axis baselines should match', function () {
650+
expect(leftChart.y()(0)).toEqual(rightChart.y()(0));
651+
});
652+
});
653+
});
624654
});
625655

626656
describe('sub-charts with different filter types', function () {

src/composite-chart.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,45 @@ dc.compositeChart = function (parent, chartGroup) {
117117
}
118118

119119
if (_chart.alignYAxes() && left && right && (lyAxisMin < 0 || ryAxisMin < 0)) {
120-
// both y axis are linear and at least one doesn't start at zero
121-
var leftYRatio, rightYRatio;
120+
return alignYAxisRanges(lyAxisMin, lyAxisMax, ryAxisMin, ryAxisMax);
121+
}
122+
return {
123+
lyAxisMin: lyAxisMin,
124+
lyAxisMax: lyAxisMax,
125+
ryAxisMin: ryAxisMin,
126+
ryAxisMax: ryAxisMax
127+
};
128+
}
122129

123-
if (lyAxisMin < 0) {
124-
leftYRatio = lyAxisMax / lyAxisMin;
125-
}
130+
function alignYAxisRanges (lyAxisMin, lyAxisMax, ryAxisMin, ryAxisMax) {
131+
// both y axis are linear and at least one doesn't start at zero
132+
var leftYRatio, rightYRatio;
126133

127-
if (ryAxisMin < 0) {
128-
rightYRatio = ryAxisMax / ryAxisMin;
129-
}
134+
if (lyAxisMin < 0) {
135+
leftYRatio = lyAxisMax / lyAxisMin;
136+
}
130137

131-
if (lyAxisMin < 0 && ryAxisMin < 0) {
132-
if (leftYRatio < rightYRatio) {
133-
ryAxisMax = ryAxisMin * leftYRatio;
134-
} else {
135-
lyAxisMax = lyAxisMin * rightYRatio;
136-
}
137-
} else if (lyAxisMin < 0) {
138-
ryAxisMin = ryAxisMax / leftYRatio;
138+
if (ryAxisMin < 0) {
139+
rightYRatio = ryAxisMax / ryAxisMin;
140+
}
141+
142+
if (lyAxisMin < 0 && ryAxisMin < 0) {
143+
if (leftYRatio < rightYRatio) {
144+
ryAxisMax = ryAxisMin * leftYRatio;
139145
} else {
140-
lyAxisMin = lyAxisMax / (ryAxisMax / ryAxisMin);
146+
lyAxisMax = lyAxisMin * rightYRatio;
147+
}
148+
} else if (lyAxisMin < 0) {
149+
ryAxisMin = ryAxisMax / leftYRatio;
150+
if (lyAxisMax < 0) {
151+
lyAxisMax = -lyAxisMax;
152+
ryAxisMin = -ryAxisMin;
153+
}
154+
} else {
155+
lyAxisMin = lyAxisMax / rightYRatio;
156+
if (ryAxisMax < 0) {
157+
ryAxisMax = -ryAxisMax;
158+
lyAxisMin = -lyAxisMin;
141159
}
142160
}
143161
return {

0 commit comments

Comments
 (0)