1

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

5
  • you need to provide more code. from what you're showing, you've assigned one color to rand. where are you iterating over your array? Commented Oct 27, 2015 at 20:23
  • 2
    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}; });. You may not be creating rand for each item! Commented Oct 27, 2015 at 20:23
  • 2
    You're missing a } on the final line: }); Commented Oct 27, 2015 at 20:23
  • @RayonDabre type that as answer. it solved Commented Oct 27, 2015 at 20:24
  • from what i see, your rand variable is outside of the scope und is beeing initialized only once. You could calculate a set of random colors in advance or just put the randomizer inside the data.data.map function like 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 }; }); Commented Oct 27, 2015 at 21:00

1 Answer 1

2

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};
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.