dc.js icon indicating copy to clipboard operation
dc.js copied to clipboard

Line chart legend: legendHighlight matches by color

Open emiguevara opened this issue 12 years ago • 3 comments

Which means that if you have a chart where the same color is used by multiple elements, clicking on a legend item will highlight all of them at once (wrong selection).

I discovered this while working with a composite lineChart, but I suppose that it applies also to other charts with legends. In order to avoid this behaviour, legends and charts should be linked by unique id attributes, not by color, stroke, or other style attributes.

The culprit function in lineChart():

function colorFilter(color, dashstyle, inv) {
    return function () {
        var item = d3.select(this);
        var match = (item.attr('stroke') === color &&
            item.attr('stroke-dasharray') === ((dashstyle instanceof Array) ?
                dashstyle.join(',') : null)) || item.attr('fill') === color;
        return inv ? !match : match;
    };
}

emiguevara avatar Oct 15 '14 13:10 emiguevara

Yeah that certainly is not a robust way to select items! Thanks for reporting this.

gordonwoodhull avatar Nov 29 '14 18:11 gordonwoodhull

Another fun fact on this method: Internet Explorer may return an equivalent but different value that the one assigned.

If your original color was "rgba( 1, 1, 1, .2 )", IE will happily return "rgba( 1, 1, 1, 0.2 )" from the item.attr("xxx") methods.

In answer to a question above, the same sketchy code is found in the barChart.

eric-poitras avatar Jan 08 '15 13:01 eric-poitras

One of the happy side effects of this "undocumented feature" is that I cna have a composite chart with two sub charts that represent one legend item. By making both charts the same color and setting one of the sub charts to not be in the legend ( see #871 ), I'm able to get the correct things to highlight when selecting the item in the legend.

this of course reminds me of: http://xkcd.com/1172/

What ever method is used in the future to get a "robost way to select items", should include the ability to highlight multiple sub charts.

LorenzGardner avatar Mar 04 '15 19:03 LorenzGardner