Created
February 23, 2016 23:07
-
-
Save kirjavascript/1f55c18a8fcc58aee5a2 to your computer and use it in GitHub Desktop.
MFP
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> | |
<html> | |
<meta charset="utf-8"> | |
<head><title>calories</title></head> | |
<body> | |
</body> | |
<style> | |
.bar { | |
fill: steelblue; | |
} | |
.bar:hover { | |
fill: brown; | |
} | |
.axis { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.x.axis path { | |
display: none; | |
} | |
</style> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 100, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 600 - margin.top - margin.bottom; | |
var x = d3.scale.ordinal() | |
.rangeRoundBands([0, width], .1); | |
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 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("mfp.csv", calories); | |
function calories(e, data) { | |
x.domain(data.map(d => d.Date)); | |
y.domain([0, d3.max(data, d => d.Calories|0)+500]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis) | |
.selectAll('text') | |
.style("text-anchor", "end") | |
.attr("dx", "-1em") | |
.attr("dy", "-0.5em") | |
.attr("transform", "rotate(-90)") | |
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("Calories") | |
svg.selectAll(".bar") | |
.data(data) | |
.enter().append("rect") | |
.attr("class", "bar") | |
.attr("x", d => x(d.Date)) | |
.attr("width", x.rangeBand()) | |
.attr("y", d => y(d.Calories)) | |
.attr("height", d => height - y(d.Calories)) | |
} | |
</script> | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment