-->

Conditional coloring in dc charts

2019-09-15 19:00发布

问题:

I am using dc.js a to create a pie chart to show gain and loss, How can I put some condition based on which the color of the pie chart changes.

I have tried using

     .colors(['#fdd752', '#AEC785', '#a48ad4', '#3acdc7'])
    // (optional) define color domain to match your data domain if you want to bind data or color
    .colorDomain([-1750, 1644])
    // (optional) define color value accessor
    .colorAccessor(function (d, i) {console.log(d);  return d.value; })

Any ideas?

回答1:

Just like X-axis scales, the color domain can be ordinal, which means each value maps to a different color out of a palette, or quantitative, for continuous colors.

By default the pie chart (and most of the others) uses an ordinal scale, but if you mean to color by a continuous value, a linear scale might be best.

E.g.

chart.linearColors(['red', 'green'])
   .colorDomain([-1750, 1644])
   .colorAccessor(function (d, i) {console.log(d);  return d.value; })


标签: d3.js dc.js