D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
sarubenfeld
Full window
Github gist
d3 linear gradient area chart: stock market close simulation (update to d3 tips and tricks)
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font-family: Futura; font-size: 10px; } div { display: inline-block; } h1 { font-family: futura; font-size: 10px; } .axis { stroke-dasharray: 2, 2; stroke-linecap: "round"; shape-rendering: crispEdges; } .area { fill: url(#area-gradient); stroke-width: 0px; } .axis line { stroke: #ccc; stroke-dasharray: 1px 1px; } text { font-family: Futura; } </style> <body> <script src="https://d3js.org/d3.v4.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 parseTime = d3.timeParse("%d-%b-%y"); var x = d3.scaleTime().range([0, width]); var y = d3.scaleLinear().range([height, 0]); var area = d3.area() .x(function(d) { return x(d.date); }) .y0(height) .y1(function(d) { return y(d.close); }); 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; data.forEach(function(d) { d.date = parseTime(d.date); d.close = +d.close; }); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([0, d3.max(data, function(d) { return d.close; })]); var colours = ["#00ffb3","#00fff3","#00cbff","#008cff","#004cff","#000cff"]; svg.append("linearGradient") .attr("id", "area-gradient") .attr("gradientUnits", "userSpaceOnUse") .attr("spreadMethod", "pad") .style("mix-blend-mode","color-dodge") .attr("x1", 0).attr("y1", y(0)) .attr("x2", 0).attr("y2", y(1000)) .selectAll("stop") .data(colours) .enter().append("stop") .attr("offset", function(d,i) { return i/(colours.length-1); }) .attr("stop-color", function(d) { return d; }); svg.append("path") .data([data]) .attr("class", "area") .attr("d", area); svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); svg.append("g") .call(d3.axisLeft(y)); }); </script> </body>
https://d3js.org/d3.v4.min.js