gsiradze
gsiradze

Reputation: 4733

random background color for each array element

I'm generating random color using that:

var rand = 'rgb(' + (Math.floor((256-199)*Math.random()) + 200) + ',' + (Math.floor((256-199)*Math.random()) + 200) + ',' + (Math.floor((256-199)*Math.random()) + 200) + ')';

and I'm mapping data:

var mappedData = data.data.map(function(item){    

    return { value: item[1], name: item[0], color: rand };
);

after which it's passed to chart.

But it generates whole chart with one color. How can I do that this change display for each chart element?

I've tried to add var bgColor = rand; inside of mappedData and then assign it to color: bgColor but it gave me same result

Upvotes: 1

Views: 612

Answers (1)

Rayon
Rayon

Reputation: 36609

It seems that you are not creating rand for every item in map

Try this:

var mappedData = data.data.map(function(item) {
    var rand = 'rgb(' + (Math.floor((256 - 199) * Math.random()) + 200) + ',' + (Math.floor((256 - 199) * Math.random()) + 200) + ',' + (Math.floor((256 - 199) * Math.random()) + 200) + ')';
    return {value: item[1], name: item[0], color: rand};
});

Upvotes: 2

Related Questions