d3.js轴标签-颜色不变

d3.js axis labels - color not changing

本文关键字:颜色 标签 js d3      更新时间:2023-09-26

我使用以下文本创建轴,但是标签的颜色没有正确更改;文本的颜色保持为黑色。有人知道为什么吗?

  // create Axis
  svg.selectAll(".axis")
      .data(d3.range(angle.domain()[1]))
    .enter().append("g")
      .attr("class", "axis")
      .attr("transform", function(d) { return "rotate(" + angle(d) * 180 / Math.PI + ")"; })
    .call(d3.svg.axis()
      .scale(radius.copy().range([-5, -outerRadius]))
      .ticks(5)
      .orient("left"))
    .append("text")
      .attr("y", 
        function (d) {
          if (window.innerWidth < 455){
            return -(window.innerHeight * .33);
          }
          else{
            return -(window.innerHeight * .33);
          }
        })
      .attr("dy", ".71em")
      .attr("text-anchor", "middle")
      .text(function(d, i) { return capitalMeta[i]; })
      .attr("style","font-size:12px;")
      .style("color","#DE3378");   <--------------- adding color attribute here...

编辑-尝试用以下代码给不同的轴标签涂上不同的颜色,但这不能正常工作。。。

  .style(function() {
      for (var i = 0; i < unhealthyArray.length; i++) {
              if ($.inArray(unhealthyArray[i], capitalMeta) != -1)
                    return "fill","red";
              else
                    return "fill","black";
      }
  });

您需要使用stroke属性(用于轮廓)或fill属性(用于填充颜色)。参见本教程。