You might reserve a special place to render zeroes when using a log axis, since normally these will be displayed at infinity. However, since this represents a discontinuity in the encoding, it’s important to show the discontinuity visually.
Last active
February 9, 2016 02:05
Log Axis with Zero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.axis text { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var margin = {top: 210, right: 20, bottom: 220, left: 60}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scale.log() | |
.domain([1e-1, 1e4]) | |
.range([0, width]); | |
var x0 = d3.scale.ordinal() | |
.domain([0]) | |
.range([-30]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom") | |
.ticks(20, d3.format(",.1s")); | |
var x0Axis = d3.svg.axis() | |
.scale(x0) | |
.orient("bottom"); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.right + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
svg.append("g") | |
.attr("class", "x axis") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "x-zero axis") | |
.call(x0Axis); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment