This program is made in order to test the fill function and the gradient utility
Last active
April 10, 2017 15:09
-
-
Save silgon/aa3bc2282edbbb0614023c82987f17e3 to your computer and use it in GitHub Desktop.
Randomly Generated Gradient Fill Graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<svg width="960" height="500"></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() | |
.rangeRound([0, width]); | |
var y = d3.scaleLinear() | |
.rangeRound([height, 0]); | |
n_years_=10; | |
today = new Date(); | |
begin = today.getFullYear(); | |
end = begin+ n_years_; | |
begin = new Date(begin.toString()); | |
end = new Date(end.toString()); | |
array_dates = d3.timeYears(begin,end); | |
data = [] | |
for(i=0; i<array_dates.length;i++){ | |
data.push({"date":array_dates[i], | |
"open":Math.random()+1, | |
"close":Math.random()+2, | |
}) | |
} | |
x.domain(d3.extent(data, function(d) { return d.date; })); | |
y.domain([0, d3.max(data, function(d) { return d.close; })]); | |
var area = d3.area() | |
.x(function(d) { return x(d.date); }) | |
.y1(function(d) { return y(d.close); }) | |
.y0(function(d) { return y(d.open); }); | |
svg.append("linearGradient") | |
.attr("id", "temperature-gradient") | |
.attr("gradientUnits", "userSpaceOnUse") | |
.attr("x1", 0).attr("y1", y(1)) | |
.attr("x2", 0).attr("y2", y(3)) | |
.selectAll("stop") | |
.data([ | |
{offset: "0%", color: d3.rgb("red").darker(2.5)}, | |
{offset: "60%", color: "red"}, | |
{offset: "100%", color: "red"} | |
]) | |
.enter().append("stop") | |
.attr("offset", function(d) { return d.offset; }) | |
.attr("stop-color", function(d) { return d.color; }); | |
g.append("path") | |
.datum(data) | |
.attr("class","area") | |
.style("fill", "url(#temperature-gradient)") | |
.attr("d", area); | |
g.append("g") | |
.attr("transform", "translate(0," + height + ")") | |
.call(d3.axisBottom(x)); | |
g.append("g") | |
.call(d3.axisLeft(y)) | |
.append("text") | |
.attr("fill", "#000") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", "0.71em") | |
.attr("text-anchor", "end") | |
.text("Price ($)"); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment