Proper way to draw gridlines

前端 未结 6 1453
闹比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:39

    Assuming that you have Mike Bostock's standard margins defined and you have defined a linear scale for the y-axis the following code will create horizontal gridlines without using tickSize().

    svg.selectAll("line.horizontalGrid").data(yScale.ticks(4)).enter()
        .append("line")
            .attr(
            {
                "class":"horizontalGrid",
                "x1" : margin.right,
                "x2" : width,
                "y1" : function(d){ return yScale(d);},
                "y2" : function(d){ return yScale(d);},
                "fill" : "none",
                "shape-rendering" : "crispEdges",
                "stroke" : "black",
                "stroke-width" : "1px"
            });
    

提交回复
热议问题