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 Abhishek Hingu
February 17, 2019
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-Mar-13 583.98\n\
29-Oct-14 582.13\n\
29-Jul-15 300.13\n\
29-Feb-16 400.13\n\
";
var quarter = function (date, i) {
if (i >= 0) {
var date2 = new Date();
date2.setMonth(date.getMonth() - 1);
q = Math.ceil((date2.getMonth()) / 3);
return "Q" + q;
}
};
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 x2 = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.ticks(d3.time.months, 3)
.tickSize(5, 0)
.tickFormat(quarter)
.orient("bottom");
var xAxis2 = d3.svg.axis()
.scale(x)
.ticks(d3.time.year,1)
.tickFormat(d3.time.format("%Y"))
.orient("top");
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('spline');
// 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;
});
*/
...