After selecting a number, the task is to generate circles using d3.js. Each circle will be assigned a color from an array:
var color =["red", "blue", "yellow", "orange",.....]
● For instance, if the user picks 593, the first 500 circles should be red (color[0]
), the next 90 blue (color[1]
), and the last 3 yellow (color[2]
) because
593 = 500 + 90 + 3 = 5 * 10^2 + 9 * 10^1 + 3 * 10^0
or with
var number = 593
var number_as_array = number.toString().split('');
Hence,
593 = 5 * number_as_array[0] * 10^(number_as_array.length - 1) + 9 * number_as_array[1] * 10^(number_as_array.length - 2) + 3 * number_as_array[2] * 10^(number_as_array.length - 3)
● If the chosen number is 4168, then 4000 circles will be red, the following 100 blue, another 60 yellow, and the remaining 8 orange.
To set the color for each circle, an array of JS objects is created using a for loop:
var data=[]
for (index = 0; index < number; index++){
circle= {};
circle.cx = circle_x;
circle.cy = circle_y;
circle.color = color[????]
data.push(circle);
The challenge lies in how to assign colors to circle.color
based on the mentioned criteria?