xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dormitory Energy Usage</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: 'Pt Sans', Arial, sans-serif;
}
#container {
width: 1000px;
margin-left: auto;
margin-right: auto;
margin-top: 50px;
padding: 50px;
background-color: white;
box-shadow: 3px 3px 5px 6px #ccc;
}
h1 {
font-size: 24px;
margin: 0;
font-family: "Merriweather";
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
#captionbox {
font-size: 1.5em;
font-weight: bold;
}
svg {
background-color: #fff;
}
path {
opacity: .6;
}
path:hover {
opacity: .99;
-webkit-transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-ms-transition: opacity .25s ease-in-out;
-o-transition: opacity .25s ease-in-out;
transition: opacity .25s ease-in-out;
cursor: pointer;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: 'PT Sans';
font-size: 13px;
}
</style>
<link href='https://fonts.googleapis.com/css?family=Merriweather|PT+Sans' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="container">
<h1>Yale's Total Energy Usage in Undergrad Dorms: <span id="captionbox"></span> </h1>
<div id="mainGraph">
</div>
<p>Units: MMBTUs per square foot in each building. Includes electricity, gas, steam, and chilled water. </p>
<p><b>Source</b>: <a href="https://java.facilities.yale.edu/energy/">Yale Facilities</a>, 2011-2015</p>
</div>
<script type="text/javascript">
//Set up stack method
var stack = d3.layout.stack()
.values(function(d) {
return d.energy;
})
.order("ascend");
//Width, height, padding
var w = 950;
var h = 600;
var padding = [ 60, 10, 50, 100 ]; //Top, right, bottom, left
//Set up date format function (years)
var dateFormat = d3.time.format("%B-%y");
var shortDate = d3.time.format("%b-%Y");
//Set up scales
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(5)
.tickFormat(function(d) {
return shortDate(d);
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(6);
//Configure area generator
var area = d3.svg.area()
.x(function(d) {
return xScale(dateFormat.parse(d.x));
})
.y0(function(d) {
return yScale(d.y0); //Updated
})
.y1(function(d) {
return yScale(d.y0 + d.y); //Updated
});
//Easy colors accessible via a 20-step ordinal scale
// var color = d3.scale.category10();
var color = d3.scale.ordinal()
// .domain(["New York", "San Francisco", "Austin"])
.range(["#8dd3c7","#ffffb3","#bebada","#fb8072","#80b1d3","#fdb462","#b3de69","#fccde5","#d9d9d9","#bc80bd","#ccebc5","#ffed6f"]);
//Create the empty SVG image
var svg = d3.select("#mainGraph")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load data
d3.csv("resCollegeEnergy.csv", function(data) {
//Uncomment to log the newly loaded data to the console
//console.log(data);
//Data is loaded in, but we need to restructure it.
//Remember, each line requires an array of x/y pairs;
//that is, an array of arrays, like so:
//
// [ [x: 1, y: 1], [x: 2, y: 2], [x: 3, y: 3] ]
//
//Our x value will be the year, and y will be the amount
//of CO2. We also need to know which college belongs to
//each line, so we will build an array of objects that is
//structured like this:
//
/*
[
{
college: "Australia",
emissions: [
{ x: "1961", y: 90589.568 },
{ x: "1962", y: 94912.961 },
{ x: "1963", y: 101029.517 },
…
]
},
{
college: "Bermuda",
emissions: [
{ x: "1961", y: 176.016 },
{ x: "1962", y: 157.681 },
{ x: "1963", y: 150.347 },
…
]
},
…
]
*/
//
//Note that this is an array of objects. Each object
//contains two values, 'college' and 'emissions'.
//The 'emissions' value is itself an array, containing
//more objects, each one holding x and y values.
//
//The x (year) values have to be strings in this case,
//because the date format function expects a string
//to parse into a Date object.
// New array with all the years, for referencing later
// Replace with something that extracts all column headers and puts them into a list of strings for future reference. Should use a data parser
var months = ["August-11","September-11","October-11","November-11","December-11","January-12","February-12","March-12","April-12","May-12","June-12","July-12","August-12","September-12","October-12","November-12","December-12","January-13","February-13","March-13","April-13","May-13","June-13","July-13","August-13","September-13","October-13","November-13","December-13","January-14","February-14","March-14","April-14","May-14","June-14","July-14","August-14","September-14","October-14","November-14","December-14","January-15","February-15","March-15","April-15","May-15","June-15","July-15","August-15","September-15"];
//Create a new, empty array to hold our restructured dataset
var dataset = [];
// Restructure the Data
//Loop once for each row in data
for (var i = 0; i < data.length; i++) {
//Create new object with this college's name and empty array
dataset[i] = {
college: data[i].collegeName,
energy: []
};
//Loop through all the months
for (var j = 0; j < months.length; j++) {
//Default value, used in case no value is present
var amount = null;
// If value is not empty, load in our data
if (data[i][months[j]]) {
amount = +data[i][months[j]];
}
//Add a new object to the emissions data array for this college
dataset[i].energy.push({
x: months[j],
y: amount
});
}
}
//Stack the data!
stack(dataset);
//Uncomment to log the original data to the console
console.log(data);
//Uncomment to log the newly restructured dataset to the console
console.log(dataset);
//Now that the data is ready, we can check its
//min and max values to set our scales' domains!
xScale.domain([
d3.min(months, function(d) {
return dateFormat.parse(d);
}),
d3.max(months, function(d) {
return dateFormat.parse(d);
})
]);
//Need to recalcluate the max value for yScale
//differently, now that everything is stacked.
//Loop once for month, and get total energy used per sq foot on campus
var totals = [];
for (var l = 0; l < months.length; l++) {
totals[i] = 0;
for (var k = 0; k< dataset.length; k++ ) {
totals[i] += dataset[k].energy[l].y;
}
}
yScale.domain([1.4* d3.max(totals), 0 ]);
//Areas
//Make a path for each college
var paths = svg.selectAll("path")
.data(dataset)
.enter()
.append("path")
.attr("class", "area")
.attr("d", function(d) {
//Calculate path based on only d.energy array,
return area(d.energy);
})
.attr("stroke", "none")
.attr("fill", function(d, i) {
return color(i);
});
//Create axes
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
d3.selectAll("path")
.on("mouseover", function(d) {
d3.select("#captionbox")
.text(d.college );
});
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js