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%
Good - as crisp as a cucumber !
Use vector-effect="non-scaling-stroke" so the stroke width doesn't scale. That way it won't collapse when you zoom out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With