How to change line color in d3js according to axis value?

后端 未结 3 795
粉色の甜心
粉色の甜心 2021-02-06 06:09

I want to change color for this line chart if X > 100 I want it to turn \"red\"

Is there a way I can use condition within stroke color style based on value of X ?

3条回答
  •  独厮守ぢ
    2021-02-06 07:11

    I think you can achieve this by defining gradient for the line instead of styling it. Check out this solution here. change color of line graph based on data (red for above threshold of say 0 , and blue for below 0)

    I asked a very similar question yesterday and was able to get it working by reading D3 documentation and looking at some samples like this one https://bl.ocks.org/mbostock/3970883

    svg.append("linearGradient")
                   .attr("id", "line-gradient")
                   .attr("gradientUnits", "userSpaceOnUse")
                   .attr("x1", 0).attr("y1", y(0))
                   .attr("x2", 0).attr("y2", y(2))
                   .selectAll("stop")
                   .data(
                          [
                           {offset: "100%", color: "blue"},
                           {offset: "100%", color: "red"},
                          ]
                        )
                    .enter().append("stop")
                            .attr("offset", function(d) { return d.offset; })
                            .attr("stop-color", function(d) { return d.color; });
    

提交回复
热议问题