Skip to content

Commit 2dd561f

Browse files
cleanup of #985
add tests force resize when changing alignYAxes rename calculateYAxisRanges add author
1 parent 606b218 commit 2dd561f

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ Chris Alvino <alvino.chris@gmail.com>
6666
Emiliano Guevara <emiliano@opoint.com>
6767
Wang Xuan <wangxuan927@gmail.com>
6868
Ethan Jewett <esjewett@gmail.com>
69+
Mohamed Gazal <mohamed.gazal@gmail.com>

spec/composite-chart-spec.js

+61-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* global appendChartID, loadDateFixture, makeDate */
22
describe('dc.compositeChart', function () {
3-
var id, chart, data, dateDimension, dateValueSumGroup, dateIdSumGroup, dateGroup;
3+
var id, chart, data, dateDimension, dateValueSumGroup, dateValueNegativeSumGroup,
4+
dateIdSumGroup, dateGroup;
45

56
beforeEach(function () {
67
data = crossfilter(loadDateFixture());
78
dateDimension = data.dimension(function (d) { return d3.time.day.utc(d.dd); });
89
dateValueSumGroup = dateDimension.group().reduceSum(function (d) { return d.value; });
10+
dateValueNegativeSumGroup = dateDimension.group().reduceSum(function (d) { return -d.value; });
911
dateIdSumGroup = dateDimension.group().reduceSum(function (d) { return d.id; });
1012
dateGroup = dateDimension.group();
1113

@@ -547,6 +549,64 @@ describe('dc.compositeChart', function () {
547549
expect(chart.selectAll('.grid-line.horizontal line').size()).toBe(7);
548550
});
549551
});
552+
553+
describe('when composing a left axis chart with negative values', function () {
554+
var leftChart, rightChart;
555+
beforeEach(function () {
556+
chart
557+
.compose([
558+
leftChart = dc.barChart(chart)
559+
.group(dateValueNegativeSumGroup, 'Date Value Group'),
560+
rightChart = dc.lineChart(chart)
561+
.group(dateIdSumGroup, 'Date ID Group')
562+
.useRightYAxis(true)
563+
])
564+
.render();
565+
});
566+
567+
it('the axis baselines shouldn\'t match', function () {
568+
expect(leftChart.y()(0)).not.toEqual(rightChart.y()(0));
569+
});
570+
571+
describe('with alignYAxes', function () {
572+
beforeEach(function () {
573+
chart.alignYAxes(true)
574+
.render();
575+
});
576+
it('the axis baselines should match', function () {
577+
expect(leftChart.y()(0)).toEqual(rightChart.y()(0));
578+
});
579+
});
580+
});
581+
582+
describe('when composing a right axis chart with negative values', function () {
583+
var leftChart, rightChart;
584+
beforeEach(function () {
585+
chart
586+
.compose([
587+
leftChart = dc.barChart(chart)
588+
.group(dateIdSumGroup, 'Date Value Group'),
589+
rightChart = dc.lineChart(chart)
590+
.group(dateValueNegativeSumGroup, 'Date ID Group')
591+
.useRightYAxis(true)
592+
])
593+
.render();
594+
});
595+
596+
it('the axis baselines shouldn\'t match', function () {
597+
expect(leftChart.y()(0)).not.toEqual(rightChart.y()(0));
598+
});
599+
600+
describe('with alignYAxes', function () {
601+
beforeEach(function () {
602+
chart.alignYAxes(true)
603+
.render();
604+
});
605+
it('the axis baselines should match', function () {
606+
expect(leftChart.y()(0)).toEqual(rightChart.y()(0));
607+
});
608+
});
609+
});
550610
});
551611

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

src/composite-chart.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ dc.compositeChart = function (parent, chartGroup) {
8383
_chart._prepareYAxis = function () {
8484
var left = (leftYAxisChildren().length !== 0);
8585
var right = (rightYAxisChildren().length !== 0);
86-
var ranges = calculateYAxesRanges(left, right);
86+
var ranges = calculateYAxisRanges(left, right);
8787

8888
if (left) { prepareLeftYAxis(ranges); }
8989
if (right) { prepareRightYAxis(ranges); }
@@ -107,7 +107,7 @@ dc.compositeChart = function (parent, chartGroup) {
107107
}
108108
};
109109

110-
function calculateYAxesRanges (left, right) {
110+
function calculateYAxisRanges (left, right) {
111111
var lyAxisMin, lyAxisMax, ryAxisMin, ryAxisMax;
112112

113113
if (left) {
@@ -153,7 +153,7 @@ dc.compositeChart = function (parent, chartGroup) {
153153
}
154154

155155
function prepareRightYAxis (ranges) {
156-
if (_chart.rightY() === undefined || _chart.elasticY()) {
156+
if (_chart.rightY() === undefined || _chart.elasticY() || _chart.resizing()) {
157157
if (_chart.rightY() === undefined) {
158158
_chart.rightY(d3.scale.linear());
159159
}
@@ -167,7 +167,7 @@ dc.compositeChart = function (parent, chartGroup) {
167167
}
168168

169169
function prepareLeftYAxis (ranges) {
170-
if (_chart.y() === undefined || _chart.elasticY()) {
170+
if (_chart.y() === undefined || _chart.elasticY() || _chart.resizing()) {
171171
if (_chart.y() === undefined) {
172172
_chart.y(d3.scale.linear());
173173
}
@@ -403,6 +403,7 @@ dc.compositeChart = function (parent, chartGroup) {
403403
return _alignYAxes;
404404
}
405405
_alignYAxes = alignYAxes;
406+
_chart.rescale();
406407
return _chart;
407408
};
408409

src/coordinate-grid-mixin.js

+4
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ dc.coordinateGridMixin = function (_chart) {
119119
return _chart;
120120
};
121121

122+
_chart.resizing = function () {
123+
return _resizing;
124+
};
125+
122126
/**
123127
* Get or set the range selection chart associated with this instance. Setting the range selection
124128
* chart using this function will automatically update its selection brush when the current chart

0 commit comments

Comments
 (0)