d3 simple line graph, showing 'step-after' interpolation
Forked from https://jsfiddle.net/hrabinowitz/abJLf/22/ 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 Ram Tobolski
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;
}
svg {
border: 1px solid orange;
}
}
JavaScript
var myData = "date close\n\
24-Apr-12 560.28\n\
25-Apr-12 610.00\n\
26-Apr-12 607.70\n\
27-Apr-12 603.00\n\
30-Apr-12 583.98\n\
1-May-12 582.13\n\
";
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 480 - margin.left - margin.right,
height = 250 - 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');
//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 parsedData = d3.tsv.parse(myData);
console.log("parsedData=", parsedData);
parsedData.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
console.log("after massage, parsedData=", parsedData);
var data = parsedData;
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.close; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform",...