Area generator with y0 and y1 accessors.
From D3 in Depth book by Peter Cook.
license: gpl-3.0 | |
height: 210 | |
border: no |
Area generator with y0 and y1 accessors.
From D3 in Depth book by Peter Cook.
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<head> | |
<title>Area generator with y0 and y1 accessors</title> | |
</head> | |
<style> | |
path { | |
fill: #ddd; | |
} | |
</style> | |
<body> | |
<svg width="700" height="200"> | |
<g></g> | |
</svg> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script> | |
<script> | |
var yScale = d3.scaleLinear().domain([0, 100]).range([200, 0]); | |
var points = [ | |
{x: 0, low: 30, high: 80}, | |
{x: 100, low: 80, high: 100}, | |
{x: 200, low: 20, high: 30}, | |
{x: 300, low: 20, high: 50}, | |
{x: 400, low: 10, high: 40}, | |
{x: 500, low: 50, high: 80} | |
]; | |
var areaGenerator = d3.area() | |
.x(function(d) { | |
return d.x; | |
}) | |
.y0(function(d) { | |
return yScale(d.low); | |
}) | |
.y1(function(d) { | |
return yScale(d.high); | |
}); | |
var area = areaGenerator(points); | |
// Create a path element and set its d attribute | |
d3.select('g') | |
.append('path') | |
.attr('d', area); | |
</script> | |
</body> | |
</html> |