Skip to content

Commit

Permalink
Removed duplicate code between composite chart and coordinate grid mi…
Browse files Browse the repository at this point in the history
…xin related to brushing. Line chart brushing now works with resizing (http://localhost:8888/web/resizing/resizing-right-axis.html)
  • Loading branch information
kum-deepak committed Apr 12, 2018
1 parent d5551d2 commit c001c0e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 40 deletions.
2 changes: 2 additions & 0 deletions spec/composite-chart-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ describe('dc.compositeChart', function () {
dateIdSumGroup, dateIdNegativeSumGroup, dateGroup;

beforeEach(function () {
dc.constants.EVENT_DELAY = 0; // so that dc.events.trigger executes immediately

data = crossfilter(loadDateFixture());
dateDimension = data.dimension(function (d) { return d3.utcDay(d.dd); });
dateValueSumGroup = dateDimension.group().reduceSum(function (d) { return d.value; });
Expand Down
2 changes: 1 addition & 1 deletion spec/coordinate-grid-chart-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ describe('dc.coordinateGridChart', function () {
});

it('should update its range chart\'s filter', function () {
expect(chart.rangeChart().filter()).toEqual(chart.filter());
expect(dc.utils.arraysEqual(chart.rangeChart().filter(), chart.filter())).toEqual(true);
});

it('should trigger redraw on its range chart', function () {
Expand Down
32 changes: 5 additions & 27 deletions src/composite-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dc.compositeChart = function (parent, chartGroup) {
child.svg(_chart.svg());
child.xUnits(_chart.xUnits());
child.transitionDuration(_chart.transitionDuration(), _chart.transitionDelay());
// TODO: Determine if we can switch off brush for children and only keep it on for parent
child.brushOn(_chart.brushOn());
child.renderTitle(_chart.renderTitle());
child.elasticX(_chart.elasticX());
Expand All @@ -68,34 +69,12 @@ dc.compositeChart = function (parent, chartGroup) {
return g;
});

_chart._brushing = function () {
// Avoids infinite recursion (mutual recursion between range and focus operations)
// Source Event will be null when brush.move is called programmatically (see below as well).
if (!d3.event.sourceEvent) { return; }

// Ignore event if recursive event - i.e. not directly generated by user action (like mouse/touch etc.)
// In this case we are more worried about this handler causing brush move programmatically which will
// cause this handler to be invoked again with a new d3.event (and current event set as sourceEvent)
// This check avoids recursive calls
if (d3.event.sourceEvent.type && ['start', 'brush', 'end'].indexOf(d3.event.sourceEvent.type) !== -1) {
return;
}

var selection = d3.event.selection;
if (selection) {
selection = selection.map(_chart.x().invert);
}
selection = _chart.extendBrush(selection);

_chart.redrawBrush(selection, false);

var brushIsEmpty = _chart.brushIsEmpty(selection);

_chart.replaceFilter(brushIsEmpty ? null : selection);

_chart.applyBrushSelection = function (rangedFilter) {
_chart.replaceFilter(rangedFilter);
for (var i = 0; i < _children.length; ++i) {
_children[i].replaceFilter(brushIsEmpty ? null : selection);
_children[i].replaceFilter(rangedFilter);
}
_chart.redrawGroup();
};

_chart._prepareYAxis = function () {
Expand Down Expand Up @@ -287,7 +266,6 @@ dc.compositeChart = function (parent, chartGroup) {
_chart.fadeDeselectedArea = function (selection) {
for (var i = 0; i < _children.length; ++i) {
var child = _children[i];
child.brush(_chart.brush());
child.fadeDeselectedArea(selection);
}
};
Expand Down
22 changes: 10 additions & 12 deletions src/coordinate-grid-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1065,19 +1065,17 @@ dc.coordinateGridMixin = function (_chart) {

_chart.redrawBrush(selection, false);

if (_chart.brushIsEmpty(selection)) {
dc.events.trigger(function () {
_chart.filter(null);
_chart.redrawGroup();
}, dc.constants.EVENT_DELAY);
} else {
var rangedFilter = dc.filters.RangedFilter(selection[0], selection[1]);
var rangedFilter = _chart.brushIsEmpty(selection) ? null : dc.filters.RangedFilter(selection[0], selection[1]);

dc.events.trigger(function () {
_chart.replaceFilter(rangedFilter);
_chart.redrawGroup();
}, dc.constants.EVENT_DELAY);
}
dc.events.trigger(function () {
_chart.applyBrushSelection(rangedFilter);
}, dc.constants.EVENT_DELAY);
};

// This can be overridden in a derived chart. For example Composite chart overrides it
_chart.applyBrushSelection = function (rangedFilter) {
_chart.replaceFilter(rangedFilter);
_chart.redrawGroup();
};

_chart.setBrushExtents = function (doTransition) {
Expand Down

0 comments on commit c001c0e

Please sign in to comment.