I would like get the area in the line graph to fill correctly. (Look at the picture to see problem)
Notice the area filled and the text under the fill:
Here is the code:
<!DOCTYPE html>
<style type="text/css">
.area {
fill: lightsteelblue;
stroke-width: 0;
}
</style>
<svg width="860" height="400"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 50},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var parseTime = d3.timeParse("%d-%b-%y");
var x = d3.scaleTime()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var line = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
d3.tsv("data.tsv", function(d) {
d.date = parseTime(d.date);
d.close = +d.close;
return d;
}, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]);
var xAxis = d3.axisBottom(x)
.ticks(d3.timeYear);
var yAxis = d3.axisRight(y)
.tickSize(width);
var area = d3.area()
.x0(function () {
return 0,width;
})
.x1(function (d) {
return x(d.date);
}).y0(function () {
return height,height;
})
.y1(function (d) {
return y(d.close);
});
var valueline = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(customXAxis);
g.append("g")
.call(customYAxis);
function customXAxis(g) {
g.call(xAxis);
g.select(".domain").remove();
g.selectAll(".tick line")
.attr("stroke", "#e9e9e9")
.attr("height","510px")
.attr("transform", "translate(0," + -height + ")");
}
function customYAxis(g) {
g.call(yAxis);
g.select(".domain").remove();
g.selectAll(".tick:not(:first-of-type) line").attr("stroke", "#e9e9e9");
g.selectAll(".tick text").attr("x", width - 18).attr("dy", -4);
}
g.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
g.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
});
</script>
Also if possible, if anyone knows, how can you get the area fill to not overlap the y-axis text and all the axis-lines, so the text and the axis-lines gets on top of the area fill.
And also one more thing, I've been trying for hours. How to get x-axis lines to create a grid together with the grey y-axis lines?
I would be grateful for any help. Thanks.