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

Fixes bubble chart filtering issues #1025

Merged
merged 2 commits into from
Nov 11, 2015
Merged
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
8 changes: 4 additions & 4 deletions spec/bubble-chart-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,21 @@ describe('dc.bubbleChart', function () {
it('creates correct label for each bubble', function () {
chart.selectAll('g.node title').each(function (d, i) {
if (i === 0) {
expect(d3.select(this).text()).toBe('F: {count:0,value:0}');
expect(d3.select(this).text()).toBe('T: {count:2,value:77}');
}
if (i === 1) {
expect(d3.select(this).text()).toBe('T: {count:2,value:77}');
expect(d3.select(this).text()).toBe('F: {count:0,value:0}');
}
});
});

it('fills bubbles with correct colors', function () {
chart.selectAll('circle.bubble').each(function (d, i) {
if (i === 0) {
expect(d3.select(this).attr('fill')).toBe('#a60000');
expect(d3.select(this).attr('fill')).toBe('#ff4040');
}
if (i === 1) {
expect(d3.select(this).attr('fill')).toBe('#ff4040');
expect(d3.select(this).attr('fill')).toBe('#a60000');
}
});
});
Expand Down
8 changes: 7 additions & 1 deletion src/bubble-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ dc.bubbleChart = function (parent, chartGroup) {

_chart.r().range([_chart.MIN_RADIUS, _chart.xAxisLength() * _chart.maxBubbleRelativeSize()]);

// sort descending so smaller bubbles are on top
var data = _chart.data(),
radiusAccessor = _chart.radiusValueAccessor();
data.sort(function (a, b) { return d3.descending(radiusAccessor(a), radiusAccessor(b)); });
var bubbleG = _chart.chartBodyG().selectAll('g.' + _chart.BUBBLE_NODE_CLASS)
.data(_chart.data(), function (d) { return d.key; });
.data(data, function (d) { return d.key; });
// Call order here to update dom order based on sort
bubbleG.order();

renderNodes(bubbleG);

Expand Down
12 changes: 11 additions & 1 deletion src/bubble-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,16 @@ dc.bubbleMixin = function (_chart) {
return _chart.label()(d);
};

var shouldLabel = function (d) {
return (_chart.bubbleR(d) > _minRadiusWithLabel);
};

var labelOpacity = function (d) {
return (_chart.bubbleR(d) > _minRadiusWithLabel) ? 1 : 0;
return shouldLabel(d) ? 1 : 0;
};

var labelPointerEvent = function (d) {
return shouldLabel(d) ? 'all' : 'none';
};

_chart._doRenderLabel = function (bubbleGEnter) {
Expand All @@ -112,6 +120,7 @@ dc.bubbleMixin = function (_chart) {

label
.attr('opacity', 0)
.attr('pointer-events', labelPointerEvent)
.text(labelFunction);
dc.transition(label, _chart.transitionDuration())
.attr('opacity', labelOpacity);
Expand All @@ -121,6 +130,7 @@ dc.bubbleMixin = function (_chart) {
_chart.doUpdateLabels = function (bubbleGEnter) {
if (_chart.renderLabel()) {
var labels = bubbleGEnter.selectAll('text')
.attr('pointer-events', labelPointerEvent)
.text(labelFunction);
dc.transition(labels, _chart.transitionDuration())
.attr('opacity', labelOpacity);
Expand Down
16 changes: 16 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@ dc.optionalTransition = function (enable, duration, callback, name) {
}
};

// See http://stackoverflow.com/a/20773846
dc.afterTransition = function (transition, callback) {
if (transition.empty() || !transition.duration) {
callback.call(transition);
} else {
var n = 0;
transition
.each(function () { ++n; })
.each('end', function () {
if (!--n) {
callback.call(transition);
}
});
}
};

/**
* @name units
* @memberof dc
Expand Down