每个阵列元素的随机背景颜色



我使用生成随机颜色

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

我正在绘制数据:

var mappedData = data.data.map(function(item){    
    return { value: item[1], name: item[0], color: rand };
);

之后它被传递到图表。

但是它会生成一种颜色的整个图表。如何才能为每个图表元素显示此更改?

我试图在mappedData中添加var bgColor = rand;,然后将其分配给color: bgColor,但它给了我相同的结果

似乎您没有为map 中的每个项目创建rand

试试这个:

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};
});

最新更新