d3 simple line graph, showing 'step-after' interpolation
This example shows a problem with the 'step-after' interpolation. It does not draw the 'last' step -- i.e., the step for the last data point. starting from bostock's example. then using different interpolation. and without an url for data.
by Mark Friesen
CSS
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
JavaScript
var myData = "date close\n\
27-Apr-12 603.00\n\
28-Apr-12 583.98\n\
29-Apr-12 582.13\n\
";
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 500 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
// try this to see interpolation issues.
// Note that the data is "backwards"-going in time.
// Note that with step-after, the last data point (i.e. earliest) shows no "step". Why?
line.interpolate('step-after');
// what if we use the defined method of line?
//line.defined(function (d) { return d.close; });
//line.interpolate('step-before');
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 + ")");
/*
d3.tsv("http://some.url/data.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
*/
var dataArray = d3.tsv.parse(myData);
console.log("dataArray=", dataArray);
dataArray.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
// add a null entry after last date
var DAY_IN_MILLIS = 24*60*60*1000;
dataArray.push(
{date: new Date(dataArray[dataArray.length-1].date.getTime() + DAY_IN_MILLIS),
close:NaN});
console.log("after massage, dataArray=", dataArray);
var data = dataArray;
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.close;...