我可以't用d3改变多线图中的线上的颜色

i can't change the colors on the lines in a multi line graph with d3

本文关键字:颜色 改变 d3 我可以      更新时间:2024-03-02

看起来我做的一切都很好,除了我不能改变颜色来区分行之外,这个代码应该可以工作:

colors = ["blue","red","yellow","green","black","blue","gray"];
linesGroup = svg.append("g").attr("class", "lines");
var linedata;
for (var i in chart_data) {
    linedata = chart_data[i];
        console.log(linedata);
    linesGroup.append("path")
    .attr("d", line(linedata.points))
    .attr("class", "line")
    .attr("fill", "none")
   .attr("stroke", function(d, i) { 
       console.log(colors[Math.floor((Math.random()*6)+1)]);
       return colors[colors[Math.floor((Math.random()*6)+1)]];
    });;
};

我还在使用jsfiddle作为完整的示例http://jsfiddle.net/yr2Nw/

将笔划设置为内联样式,并正确访问颜色数组:

   .style("stroke", function(d, i) { 
       return colors[Math.floor((Math.random()*6)+1)];
    });

使用for in循环并不是在d3中完成任务的一种非常习惯的方式(如果尝试使用i,您会遇到问题)。