Proper way to draw gridlines

前端 未结 6 1432
闹比i
闹比i 2020-12-08 04:11

Okay, I\'m starting to get a little more familiar with D3 but am still a little hazy on some things. I\'m now trying to draw grid lines but am realizing that I may be hackin

6条回答
  •  时光说笑
    2020-12-08 04:34

    Following @arete's idea, you can use the following to avoid re-drawing unnecessarily the gridline:

    function createsGrid(data) {
           var grid = gridLine.selectAll("line.horizontalGrid").data(scaleY.ticks());
    
           grid.enter()
           .append("line")
           .attr("class","horizontalGrid");
    
           grid.exit().remove();
    
           grid.attr({
                   "x1":0,
                   "x2": width,
                   "y1": function (d) { return scaleY(d); },
                   "y2": function (d) { return scaleY(d); }
                    });
    }
    

    and define the following in your CSS file

    line.horizonalGrid{
      fill : none;
     shape-rendering : crispEdges;
     stroke : black;
     stroke-width : 1.5px;
    } 
    

提交回复
热议问题