D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
romainbossut
Full window
Github gist
Area between two curves gradient
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 10px sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis path { display: none; } .area{ fill: url(#spread-gradient); } .line { fill: none; stroke: #C0C0C0; stroke-width: 1px; } </style> <body> <div id="legend1"></div> <script src="//d3js.org/d3.v3.min.js"></script> <script> var margin = {top: 20, right: 20, bottom: 30, left: 50}, width = 960 - 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 y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.area() .interpolate("basis") .x(function(d) { return x(d.date); }) .y(function(d) { return y(d["VALUE2"]); }); var lineVALUE1 = d3.svg.area() .interpolate("basis") .x(function(d) { return x(d.date); }) .y(function(d) { return y(d["VALUE1"]); }); var area = d3.svg.area() .interpolate("basis") .x(function(d) { return x(d.date); }) .y1(function(d) { return y(d["VALUE2"]); }); 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.csv("data.csv", function(error, data) { if (error) throw error; var offsets =[]; var quantize = d3.scale.quantize() .domain(d3.extent(data, function(d) { return (d["VALUE1"]-d["VALUE2"]); })) .range(["#d73027","#f46d43","#fdae61","#fee090","#abd9e9", "#74add1","#4575b4", "#313695" ]); var quantized = d3.scale.linear() .domain(d3.extent(data, function(d) { return parseDate(d.date); })) .range([0,100]); data.forEach(function(d) { d.date = parseDate(d.date); d["VALUE1"]= +d["VALUE1"]; d["VALUE2"] = +d["VALUE2"]; offsets.push({offset: Math.round(quantized(d.date),2)+"%", color: quantize(+d["VALUE1"]-d["VALUE2"])}) }); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([ d3.min(data, function(d) { return Math.min(d["VALUE1"], d["VALUE2"])-1; }), d3.max(data, function(d) { return Math.max(d["VALUE1"], d["VALUE2"])+1; }) ]); svg.datum(data); svg.append("path") .attr("class", "area") .attr("clip-path", "url(#clip-above)") .attr("d", area.y0(function(d) { return y(d["VALUE1"]); })); svg.append("path") .attr("class", "line") .attr("d", line); svg.append("path") .attr("class", "line") .attr("d", lineVALUE1); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Spread"); x = d3.time.scale() .range([0, width]); y = d3.scale.linear() .range([height, 0]); svg.append("linearGradient") .attr("id", "spread-gradient") .attr("gradientUnits", "userSpaceOnUse") .attr("x1", x(0)).attr("y1", y(0)) .attr("x2", x(1)).attr("y2", y(0)) .selectAll("stop") .data(offsets) .enter().append("stop") .attr("offset", function(d) { return d.offset; }) .attr("stop-color", function(d) { return d.color; }); }); </script>
https://d3js.org/d3.v3.min.js