Just started with D3.js, I'm trying to color a chart with many categorical levels. The following code is aimed to extract the element of colorExtra (20 elements of color codes) by dataset(d) with index i to be stored in variable fill. However, the following code did not turn out to store the color code of colorExtra in fill.
var colorExtra = d3.schemeCategory20; //add
console.log(colorExtra);
var fill = function (d, i) { return colorExtra[i]}; //add
console.log(fill);
Could someone help me to code for the variable fill accurately?
To be clear, there are 20 color code elements in colorExtra:
["#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#2ca02c", "#98df8a", "#d62728",
"#ff9896", "#9467bd", "#c5b0d5", "#8c564b", "#c49c94", "#e377c2", "#f7b6d2",
"#7f7f7f", "#c7c7c7", "#bcbd22", "#dbdb8d", "#17becf", "#9edae5"]
I have a variable, race, with the following levels of race_code, and I'm trying to make a pie graph with all available levels in a different color. In the code, d stands for race_code, and i stands for the index of that race_code, and I want to extract the color element from colorExtra by i. For example, race_code "A" will have fill value = "#1f77b4", "B" -> fill = #aec7e8", "C"-> fill = "#ff7f0e" and so on..
The objective here is not helping me to make the graph, but more on extracting the color code by i. So that each race_code gets its own color.
Here is my complete code: https://bl.ocks.org/lydiawawa/38243015ab2ac96b6086d3bae56572b9/40326b1fb9b0b2db89d3d7601c824d18f97a5e95
fillvariable, just use an ordinal scale to set thefillattribute (or style) of each SVG element: bl.ocks.org/pstuffa/3393ff2711a53975040077b7453781a9