I would like to be able to reproduce this legend with the d3 library, currently I have my own algorithm that does this operation for me, but I don’t know how it would be with d3. For example for this data array [
24.17,
24.58,
24.82,
24.91,
26.25,
28.04,
29.67,
32.06,
32.08,
32.4,
32.53,
32.58,
32.94,
33.89,
34.15,
34.84,
35.39,
37.64,
40.75,
42.31,
46.89
]
The output of 3 serious quantiles
The output of 4 serious quantiles
The output of 5 serious quantiles
What I really need is a function that by passing the data array and the number of quantiles (3,4 or 5) the output is that of the image, taking into account that for example 3 is a minimum, maximum and a value between minimum and maximum, in reality they are not 3 quantiles, they would be 2, this procedure would be the same for 4 or 5, and these would be the colors that would be in the range colorbase: rgb(231, 25, 32)colorLimit: rgb(245, 163, 166)
2
Answers
I would rather to create a linear scale of colors like this:
Basically you need to make the number of colours you require (according to the number of quantiles), and then feed them as input along with your data into a d3 quantileScale
From there you can get the cutoff values and start making your legend. Later, use the quantile scale to generate the colours for each individual datum as necessary
PS I should also point out there is a d3 legend library that will do this stuff with less work on your part, but hasn’t been maintained to the latest d3 versions (https://github.com/susielu/d3-legend). If you’re on d3 v5 or less it’ll be worth a look.