最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change the color of a d3.js axis line - Stack Overflow

programmeradmin1浏览0评论

Here is my code. I want to change the color of the actual axis line from black to grey.

var xAxisCall = d3.axisBottom(x);
  g.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0, " + height + ")")
    .call(xAxisCall)
    .selectAll("text")
        .attr("y", "10")
        .attr("x", "-5")
        .attr("text-anchor", "end")
        .attr("transform", "rotate(-40)")
        .style("fill", "#999999");

Here is my code. I want to change the color of the actual axis line from black to grey.

var xAxisCall = d3.axisBottom(x);
  g.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0, " + height + ")")
    .call(xAxisCall)
    .selectAll("text")
        .attr("y", "10")
        .attr("x", "-5")
        .attr("text-anchor", "end")
        .attr("transform", "rotate(-40)")
        .style("fill", "#999999");
Share Improve this question asked Jan 18, 2019 at 19:43 Art3misArt3mis 1091 silver badge8 bronze badges 1
  • 1 add a CSS style that address the path in the g: .x.axis path { stroke:red;} – rioV8 Commented Jan 18, 2019 at 20:35
Add a ment  | 

1 Answer 1

Reset to default 7

If you're using D3 v5 (and possibly v4) the default styling is now embedded at the element level, which saves you having to specify it yourself in CSS. One way to get over this is to override it with CSS's !important flag.

.x.axis line {
    stroke: gray !important;
}

Alternatively re-apply it to the elements after calling the axis using d3:

d3.selectAll(".x.axis line")
    .style("stroke","gray");

A little finessing may be required, but the principle should work.

发布评论

评论列表(0)

  1. 暂无评论