How can I generate as many colors as I want using d3?

前端 未结 8 1234
有刺的猬
有刺的猬 2020-12-05 01:24

I\'m building a pie chart using d3.js, and visualizing a big data set. There are more than 137 items to visualize on the chart. I have just 10 color

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 01:39

    A simple solution (with d3@5, not sure about earlier versions) that I'm surprised hasn't been mentioned yet is to use the interpolation functions combined with a function to derive a [0,1] ratio of your category respective to the categories set.

         const color = d3.interpolateSpectral;
    
         //...
    
         .style("color", (d:any, i:number) => {
           const t:number = i / data.columns.length;
           return color(t) as string;
        });
    

    https://github.com/d3/d3-scale-chromatic

提交回复
热议问题