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

后端 未结 3 803
粉色の甜心
粉色の甜心 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:04

    Here's another way, maybe in some instances that might help:

    All I do is split the data by using a filter:

    var lineGraph1 = svgContainer.append("path")
            .attr("d", lineFunction(lineData.filter(function(d) {
                return d.x <= 100;
            })))
            .attr("stroke", "blue")
            .attr("stroke-width", 2)
            .attr("fill", "none");
    var lineGraph2 = svgContainer.append("path")
            .attr("d", lineFunction(lineData.filter(function(d) {
                return d.x >= 100;
            })))
            .attr("stroke", "red")
            .attr("stroke-width", 2)
            .attr("fill", "none");
    

提交回复
热议问题