Assuming you bought AAPL stock at its closing price on January 2, 2008, how would your investment fare over the next four and a half years? (Quite well, for the most part…)
Rather than simply plot the absolute price on a linear scale, which would exaggerate gains and understate losses, instead compute the relative price, i.e., the current price divided by the purchase price. Then, use a log scale to accurately show proportional gains and losses: a +100% gain (2×) is as good as a -50% loss is bad (0.5×). As long as the current value never reaches absolute zero, meaning as long as the stock is not completely worthless, the scale domain never crosses zero.
To show percentage change (+20%, -20%), simply subtract 1 from the relative price:
var formatPercent = d3.format("+.0%");
// Formats a relative price (e.g., 2) as percentage change (e.g., +100%).
function formatChange(x) {
return formatPercent(x - 1);
}
Although the log scale ticks are adequate in relative price space, a second linear scale can also be used to generate uniformly-spaced ticks.
A nice related technique is to add color to show positive or negative change.
xxxxxxxxxx
<meta charset="utf-8">
<style>
.axis {
font: 10px sans-serif;
}
.axis-title {
text-anchor: end;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis--x path {
display: none;
}
.axis--y .tick:not(.tick--one) line {
stroke-opacity: .15;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
stroke-linejoin: round;
stroke-linecap: round;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 30, right: 30, bottom: 40, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var formatPercent = d3.format("+.0%"),
formatChange = function(x) { return formatPercent(x - 1); },
parseDate = d3.time.format("%d-%b-%y").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.log()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickSize(-width, 0)
.tickFormat(formatChange);
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.ratio); });
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 + ")");
var gX = svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")");
var gY = svg.append("g")
.attr("class", "axis axis--y");
gY.append("text")
.attr("class", "axis-title")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.text("Change in Price");
d3.tsv("data.tsv", function(error, data) {
if (error) throw error;
// Compute price relative to base value (hypothetical purchase price).
var baseValue = +data[0].close;
data.forEach(function(d) {
d.date = parseDate(d.date);
d.ratio = d.close / baseValue;
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.ratio; }));
// Use a second linear scale for ticks.
yAxis.tickValues(d3.scale.linear()
.domain(y.domain())
.ticks(20));
gX.call(xAxis);
gY.call(yAxis)
.selectAll(".tick")
.classed("tick--one", function(d) { return Math.abs(d - 1) < 1e-6; });
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
</script>
https://d3js.org/d3.v3.min.js