dc-js / dc.js

Multi-Dimensional charting built to work natively with crossfilter rendered with d3.js
Apache License 2.0
7.42k stars 1.81k forks source link

dc sunburst chart -- color of legend and the chart does NOT match #1864

Open lipingyang-geoai opened 2 years ago

lipingyang-geoai commented 2 years ago

In the dc sunburst chart (see this example https://dc-js.github.io/dc.js/examples/sunburst.html), the color of the legend seems does not match the color of the sunburst chart. Any hints and suggestions to make the color scheme match? otherwise it is misleading and confusing. thanks.

gordonwoodhull commented 2 years ago

Thanks for filing this, @lipingyang-geoai!

It looks like it may be confused between alphabetical and numeric order... notice that the colors are contiguous e.g. from 1,10 to 1,19 on the chart (slice 2-11 clockwise from the top), but have gaps where alphabetic and numeric order disagree.

EDIT: numeric order would make the colors contiguous, and make more sense for this example, but it is the multikeys that cause the legend to differ from the chart.

image
lipingyang-geoai commented 2 years ago

Thanks, Gordon @gordonwoodhull, still not able to figure this out. I checked all three sunburst examples on the dc.js examples page, seem all sunburst chart has the same issue, the color scheme of the legend does not match that of the chart. Is that the way it should be?? Thanks.

gordonwoodhull commented 2 years ago

Whoops, meant to look at the last week. Will try to look tomorrow!

Should not be complicated, probably a scale configured wrong or something.

lipingyang-geoai commented 2 years ago

sounds great. look forward to your suggestions!

gordonwoodhull commented 2 years ago

I haven't dug into the exact cause here, but it looks like you can get consistent colors by making sure the colorAccessor for the sunburst chart uses the last part of the key:

        .colorAccessor(d => {
            var key = d.key;
            if(Array.isArray(key))
                key = key[key.length-1];
            return key;
        })

I am not sure why it is sometimes getting called with a scalar number and sometimes with the multikey array. This probably has something to do with the problem!

I quickly tried pasting the above code into the other three sunburst examples, and it seems to fix them. Presumably it could be changed in the sunburst constructor, which currently uses the key accessor for the color accessor:

https://github.com/dc-js/dc.js/blob/04120002511aae005567c9f441087ecebef9ee9e/src/charts/sunburst-chart.js#L66

However, there may be a cleaner fix, so let's leave this open in case someone wants to investigate why this is happening.

lipingyang-geoai commented 2 years ago

@gordonwoodhull Thank you so much, Gordon, You ROCK! it works! Ok, I will leave this issue open. But Thank you so much again! Very best!