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 avg min max\n\
01-Jul-17 344251.9118 10000.0000 1847283.0000\n\
01-Oct-17 244474.6831 10000.0000 1443736.0000\n\
01-Jan-18 334264.7699 33052.0000 1443736.0000\n\
01-Apr-18 348314.0586 15380.0000 2410046.0000\n\
01-Jul-18 202586.6371 14005.0000 2322936.0000\n\
01-Oct-18 140596.8278 14005.0000 2322936.0000\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.avg); });
// 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(" +...