Browse Source

Fix legend state when reverted

pull/1014/merge
Masayuki Tanaka 10 years ago
parent
commit
6cf97b7294
  1. 18
      c3.js
  2. 4
      c3.min.js
  3. 14
      spec/api.focus-spec.js
  4. 6
      src/api.focus.js
  5. 12
      src/legend.js

18
c3.js

@ -3873,7 +3873,6 @@
var paddingTop = 4, paddingRight = 10, maxWidth = 0, maxHeight = 0, posMin = 10, tileWidth = 15;
var l, totalLength = 0, offsets = {}, widths = {}, heights = {}, margins = [0], steps = {}, step = 0;
var withTransition, withTransitionForTransform;
var hasFocused = $$.legend.selectAll('.' + CLASS.legendItemFocused).size();
var texts, rects, tiles, background;
options = options || {};
@ -4063,16 +4062,7 @@
// toggle legend state
$$.legend.selectAll('.' + CLASS.legendItem)
.classed(CLASS.legendItemHidden, function (id) { return !$$.isTargetToShow(id); })
.transition()
.style('opacity', function (id) {
var This = $$.d3.select(this);
if ($$.isTargetToShow(id)) {
return !hasFocused || This.classed(CLASS.legendItemFocused) ? $$.opacityForLegend(This) : $$.opacityForUnfocusedLegend(This);
} else {
return null; // c3-legend-item-hidden will be applied
}
});
.classed(CLASS.legendItemHidden, function (id) { return !$$.isTargetToShow(id); });
// Update all to reflect change of legend
$$.updateLegendItemWidth(maxWidth);
@ -5809,7 +5799,6 @@
targetIds = $$.mapToTargetIds(targetIds);
candidates = $$.svg.selectAll($$.selectorTargets(targetIds.filter($$.isTargetToShow, $$))),
this.revert();
candidates.classed(CLASS.focused, false).classed(CLASS.defocused, true);
if ($$.hasArcType()) {
$$.unexpandArc(targetIds);
@ -5834,6 +5823,11 @@
}
if ($$.config.legend_show) {
$$.showLegend(targetIds.filter($$.isLegendToShow.bind($$)));
$$.legend.selectAll($$.selectorLegends(targetIds))
.filter(function () {
return $$.d3.select(this).classed(CLASS.legendItemFocused);
})
.classed(CLASS.legendItemFocused, false);
}
$$.focusedTargetIds = [];

4
c3.min.js vendored

File diff suppressed because one or more lines are too long

14
spec/api.focus-spec.js

@ -219,6 +219,7 @@ describe('c3 api focus', function () {
});
legendItems.each(function () {
var item = d3.select(this);
expect(item.classed('c3-legend-item-focused')).toBeFalsy();
expect(+item.style('opacity')).toBeCloseTo(1);
});
done();
@ -241,6 +242,7 @@ describe('c3 api focus', function () {
});
legendItems.each(function () {
var item = d3.select(this);
expect(item.classed('c3-legend-item-focused')).toBeFalsy();
expect(+item.style('opacity')).toBeCloseTo(1);
});
done();
@ -271,6 +273,9 @@ describe('c3 api focus', function () {
expect(+legendItems.data1.style('opacity')).toBeCloseTo(1);
expect(+legendItems.data2.style('opacity')).toBeCloseTo(1);
expect(+legendItems.data3.style('opacity')).toBeCloseTo(1);
expect(legendItems.data1.classed('c3-legend-item-focused')).toBeTruthy();
expect(legendItems.data2.classed('c3-legend-item-focused')).toBeFalsy();
expect(legendItems.data3.classed('c3-legend-item-focused')).toBeTruthy();
done();
}, 1000);
}, 1000);
@ -299,6 +304,9 @@ describe('c3 api focus', function () {
expect(+legendItems.data1.style('opacity')).toBeCloseTo(0.3);
expect(+legendItems.data2.style('opacity')).toBeCloseTo(1);
expect(+legendItems.data3.style('opacity')).toBeCloseTo(0.3);
expect(legendItems.data1.classed('c3-legend-item-focused')).toBeFalsy();
expect(legendItems.data2.classed('c3-legend-item-focused')).toBeFalsy();
expect(legendItems.data3.classed('c3-legend-item-focused')).toBeFalsy();
done();
}, 1000);
}, 1000);
@ -327,6 +335,9 @@ describe('c3 api focus', function () {
expect(+legendItems.data1.style('opacity')).toBeCloseTo(1);
expect(+legendItems.data2.style('opacity')).toBeCloseTo(1);
expect(+legendItems.data3.style('opacity')).toBeCloseTo(1);
expect(legendItems.data1.classed('c3-legend-item-focused')).toBeFalsy();
expect(legendItems.data2.classed('c3-legend-item-focused')).toBeFalsy();
expect(legendItems.data3.classed('c3-legend-item-focused')).toBeTruthy();
done();
}, 1000);
}, 1000);
@ -355,6 +366,9 @@ describe('c3 api focus', function () {
expect(+legendItems.data1.style('opacity')).toBeCloseTo(1);
expect(+legendItems.data2.style('opacity')).toBeCloseTo(1);
expect(+legendItems.data3.style('opacity')).toBeCloseTo(0.3);
expect(legendItems.data1.classed('c3-legend-item-focused')).toBeFalsy();
expect(legendItems.data2.classed('c3-legend-item-focused')).toBeFalsy();
expect(legendItems.data3.classed('c3-legend-item-focused')).toBeFalsy();
done();
}, 1000);
}, 1000);

6
src/api.focus.js

@ -24,7 +24,6 @@ c3_chart_fn.defocus = function (targetIds) {
targetIds = $$.mapToTargetIds(targetIds);
candidates = $$.svg.selectAll($$.selectorTargets(targetIds.filter($$.isTargetToShow, $$))),
this.revert();
candidates.classed(CLASS.focused, false).classed(CLASS.defocused, true);
if ($$.hasArcType()) {
$$.unexpandArc(targetIds);
@ -49,6 +48,11 @@ c3_chart_fn.revert = function (targetIds) {
}
if ($$.config.legend_show) {
$$.showLegend(targetIds.filter($$.isLegendToShow.bind($$)));
$$.legend.selectAll($$.selectorLegends(targetIds))
.filter(function () {
return $$.d3.select(this).classed(CLASS.legendItemFocused);
})
.classed(CLASS.legendItemFocused, false);
}
$$.focusedTargetIds = [];

12
src/legend.js

@ -117,7 +117,6 @@ c3_chart_internal_fn.updateLegend = function (targetIds, options, transitions) {
var paddingTop = 4, paddingRight = 10, maxWidth = 0, maxHeight = 0, posMin = 10, tileWidth = 15;
var l, totalLength = 0, offsets = {}, widths = {}, heights = {}, margins = [0], steps = {}, step = 0;
var withTransition, withTransitionForTransform;
var hasFocused = $$.legend.selectAll('.' + CLASS.legendItemFocused).size();
var texts, rects, tiles, background;
options = options || {};
@ -307,16 +306,7 @@ c3_chart_internal_fn.updateLegend = function (targetIds, options, transitions) {
// toggle legend state
$$.legend.selectAll('.' + CLASS.legendItem)
.classed(CLASS.legendItemHidden, function (id) { return !$$.isTargetToShow(id); })
.transition()
.style('opacity', function (id) {
var This = $$.d3.select(this);
if ($$.isTargetToShow(id)) {
return !hasFocused || This.classed(CLASS.legendItemFocused) ? $$.opacityForLegend(This) : $$.opacityForUnfocusedLegend(This);
} else {
return null; // c3-legend-item-hidden will be applied
}
});
.classed(CLASS.legendItemHidden, function (id) { return !$$.isTargetToShow(id); });
// Update all to reflect change of legend
$$.updateLegendItemWidth(maxWidth);

Loading…
Cancel
Save