I'm creating a D3 area chart, and the area is showing above the line rather than below.
Area chart with area appearing above line
Here's a fiddle: https://jsfiddle.net/8xsrmgzw/
//Read the data
d3.csv("https://raw.githubusercontent.com/kalidogg/UCB/main/DebtPenny6.csv",
// When reading the csv, I must format variables:
function(d){
return { date : d3.timeParse("%Y-%m-%d")(d.date), value : d.value }
},
// Now I can use this dataset:
function(data) {
// Add X axis --> it is a date format
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.date; }))
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add Y axis
var y = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return +d.value; })])
.range([ height, 0 ]);
svg.append("g")
.call(d3.axisLeft(y));
// Add the area
svg.append("path")
.datum(data)
.attr("fill", "#cce5df")
.attr("stroke", "#69b3a2")
.attr("stroke-width", 1.5)
.attr("d", d3.area()
.x(function(d) { return x(d.date) })
.y0(y(0))
.y1(function(d) { return y(d.value) })
)
})
</script>
Thanks.
There are a couple issues. First, you need to sort your data by the date before drawing the area. Second, your dataset contains null values. In the example below, I have filtered those out, but you could consider doing something like these examples to indicate where data is missing.
Also, setting a stroke color on the area will create an outline of the area, including on the sides and bottom. If you just want a line outlining the top of the area, then you can add a separate path and use
area.lineY1()
to get a line generator for the top line of the area.