Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active February 9, 2016 02:00
Show Gist options
  • Save mbostock/6738109 to your computer and use it in GitHub Desktop.
Save mbostock/6738109 to your computer and use it in GitHub Desktop.
Superscript Format
license: gpl-3.0

Perhaps not the best approach due to the inconsistent vertical positioning of ¹²³ vs. the other numbers… Alternatively, you could use axis post-selection to achieve the same effect.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
font: 13px "helvetica neue";
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 250, right: 20, bottom: 250, left: 20},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var superscript = "⁰¹²³⁴⁵⁶⁷⁸⁹",
formatPower = function(d) { return (d + "").split("").map(function(c) { return superscript[c]; }).join(""); };
var x = d3.scale.log()
.domain([1e0, 1e9])
.range([0, width]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(10, function(d) { return 10 + formatPower(Math.round(Math.log(d) / Math.LN10)); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment