This program is made in order to test the fill function
Last active
April 10, 2017 13:10
-
-
Save silgon/025a08d4eb4f3c4ed9cb1ce77aebc515 to your computer and use it in GitHub Desktop.
Randomly Generated 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> | |
<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); }); | |
g.append("path") | |
.datum(data) | |
.attr("fill", "steelblue") | |
.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