Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop paths from blurring/disappearing in D3 when using a viewbox?

I am using the D3 library to create some graphs. They are placed within a viewbox (see snippet below under "output").

The viewbox is to allow for responsive scaling.

However at certain resolutions, the line either disappears, or blurs/thickens. I think this is linked to it not being pixel perfect at those resolutions, but I may be wrong.

No additional styling has been added except

shape-rendering: crispEdges;

The JS

this.y = d3.scale.linear()
    .rangeRound([this.height, 0]);

this.yAxis = d3.svg.axis()
    .scale(this.y)
    .tickSize(this.width)
    .tickValues([0, 25, 50, 75, 100])
    .tickFormat(function(d) {
        return parseInt(d, 10) + "%";
    })
    .orient("right");

    this.y.domain([0, 100]);


// Y Axis elements
var gy = this.svg.append("g").attr("class", "y axis").call(this.yAxis);

// Add minor class to all the things
gy.selectAll("g").classed("minor", true);

// Move the text over to the left
gy.selectAll("text").attr("x", 0).attr("dy", -4);

The Output

<svg class="bargraph" viewBox="0 0 250 250" preserveAspectRatio="xMidYMid" width="100%" height="100%">
    <g class="x axis">...</g>
    <g class="y axis">
        <g class="tick minor" transform="translate(0,150)">
        ..repeat..
    </g>
    ...content...
</svg>

The Painted Output

Bad - note lines 75% and 50%

Bad

Good - as crisp as a cucumber !

enter image description here

like image 680
Chris Avatar asked Nov 27 '15 06:11

Chris


1 Answers

Use vector-effect="non-scaling-stroke" so the stroke width doesn't scale. That way it won't collapse when you zoom out.

like image 106
Robert Longson Avatar answered Nov 14 '22 17:11

Robert Longson