Skip to content

Instantly share code, notes, and snippets.

@hydrosquall
Last active November 8, 2015 06:30
Show Gist options
  • Save hydrosquall/d44c35f236463c56d378 to your computer and use it in GitHub Desktop.
Save hydrosquall/d44c35f236463c56d378 to your computer and use it in GitHub Desktop.
d3 journalism course week 2 - stacked per sqft area chart
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dormitory Energy Usage</title>
<script type="text/javascript" src="http://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="http://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>
collegeName 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
BERKELEY 0.009046 0.00761 0.01251 0.015627 0.015959 0.017366 0.013559 0.011555 0.010479 0.007397 0.006673 0.005347 0.00836 0.005954 0.009366 0.013613 0.014294 0.020643 0.019338 0.015912 0.01202 0.009647 0.006349 0.012641 0.010897 0.010254 0.007879 0.013944 0.015423 0.016541 0.015092 0.01632 0.010807 0.008374 0.006304 0.008477 0.007315 0.009264 0.011546 0.012005 0.012957 0.016684 0.015172 0.014053 0.00986 0.008162 0.007153 0.006981 0.009329 0.009232
CALHOUN 0.010677 0.010568 0.009607 0.009282 0.011157 0.013756 0.012195 0.009806 0.009039 0.007225 0.008178 0.010404 0.012082 0.010805 0.009602 0.01125 0.011895 0.018398 0.019472 0.016957 0.012717 0.010572 0.010783 0.011333 0.010177 0.008853 0.008444 0.009177 0.010751 0.012957 0.01377 0.014314 0.011463 0.009609 0.009007 0.008746 0.007873 0.008805 0.00868 0.011808 0.011708 0.013313 0.011789 0.01182 0.009435 0.006681 0.005164 0.004573 0.008667 0.009594
DAVENPORT 0.009209 0.008954 0.007562 0.009042 0.010196 0.012287 0.010216 0.008155 0.007392 0.007284 0.006867 0.005928 0.006797 0.00781 0.008003 0.009771 0.010748 0.013771 0.013038 0.011031 0.008041 0.006937 0.007031 0.009186 0.008327 0.008129 0.007713 0.010227 0.012328 0.016398 0.014503 0.014036 0.01026 0.008264 0.00805 0.008803 0.008022 0.008944 0.008525 0.010797 0.011564 0.016944 0.018625 0.014454 0.009042 0.006759 0.005679 0.010432 0.008702 0.010663
EZRA_STILES 0.002778 0.006369 0.006311 0.009157 0.012072 0.00905 0.008512 0.009266 0.008866 0.009676 0.009232 0.009379 0.009572 0.008947 0.007497 0.010457 0.012044 0.012678 0.011643 0.011963 0.009392 0.008802 0.00965 0.012325 0.009219 0.009384 0.008671 0.010316 0.01134 0.011794 0.012418 0.01296 0.011816 0.009328 0.007914 0.009873 0.009256 0.009171 0.009146 0.011989 0.012238 0.013606 0.013086 0.013521 0.011162 0.008992 0.008694 0.011446 0.011213 0.011502
JONATHAN_EDWARDS 0.010371 0.009786 0.008073 0.008919 0.009806 0.010615 0.009775 0.009046 0.008398 0.007011 0.00619 0.005939 0.006447 0.009447 0.008773 0.009537 0.009654 0.010607 0.009915 0.009704 0.008084 0.007974 0.008636 0.012741 0.011632 0.009876 0.008585 0.009443 0.009669 0.011608 0.011148 0.013991 0.009182 0.008545 0.009407 0.012565 0.011425 0.009359 0.009162 0.010008 0.011018 0.013187 0.012504 0.011758 0.008917 0.007955 0.009329 0.011154 0.010855 0.010638
MORSE 0.006245 0.006744 0.008256 0.009826 0.012743 0.009259 0.008814 0.009442 0.009184 0.009767 0.009283 0.012085 0.009584 0.009037 0.007699 0.013432 0.012926 0.013424 0.012913 0.012857 0.010545 0.009492 0.0105 0.013334 0.009905 0.010693 0.00983 0.01172 0.012338 0.012805 0.012416 0.013719 0.012882 0.009946 0.00871 0.010806 0.009876 0.010314 0.010311 0.013095 0.013048 0.014356 0.014361 0.014418 0.012441 0.009678 0.009465 0.012474 0.011861 0.012418
PIERSON 0.006635 0.006765 0.006314 0.007156 0.00847 0.009826 0.008925 0.007296 0.00689 0.005405 0.00588 0.007833 0.007611 0.007307 0.007182 0.008899 0.008965 0.010966 0.010131 0.009369 0.007419 0.00665 0.00492 0.006298 0.0063 0.007818 0.007553 0.008712 0.009956 0.014303 0.012434 0.011 0.008046 0.006555 0.006462 0.007589 0.00563 0.006433 0.006902 0.008789 0.009463 0.013082 0.014733 0.011613 0.00755 0.005975 0.004573 0.005659 0.006312 0.007193
SAYBROOK 0.004848 0.006284 0.006981 0.009604 0.010914 0.016422 0.00886 0.006373 0.003929 0.006313 0.006169 0.00673 0.007547 0.007321 0.00614 0.011039 0.012141 0.014987 0.014748 0.011743 0.008796 0.004003 0.005061 0.006476 0.007806 0.007372 0.007193 0.010527 0.011408 0.010017 0.01083 0.008898 0.008274 0.006168 0.004119 0.004257 0.005 0.006298 0.007256 0.009914 0.010449 0.012359 0.012171 0.012214 0.009175 0.007617 0.004525 0.004844 0.006421 0.006766
SILLIMAN 0.009736 0.009366 0.009192 0.010715 0.01282 0.015198 0.013458 0.011795 0.0107 0.007552 0.007169 0.009037 0.00883 0.008457 0.008706 0.01201 0.01212 0.013755 0.013096 0.011412 0.008071 0.006381 0.006932 0.009178 0.007582 0.007832 0.007652 0.010344 0.011705 0.014159 0.013161 0.012962 0.010152 0.006835 0.004256 0.007441 0.006205 0.007026 0.007469 0.010109 0.011473 0.014345 0.014847 0.012714 0.009509 0.00776 0.008091 0.007653 0.007183 0.007182
TIMOTHY_DWIGHT 0.006472 0.006532 0.009045 0.010755 0.011874 0.013222 0.011247 0.009084 0.007508 0.007011 0.005968 0.006665 0.004302 0.004823 0.005798 0.008416 0.008411 0.011478 0.009733 0.009497 0.006766 0.004881 0.003462 0.003536 0.003421 0.004035 0.005474 0.007491 0.008268 0.007497 0.007796 0.007912 0.006668 0.004638 0.003281 0.00328 0.003483 0.004745 0.005391 0.007006 0.008686 0.01107 0.011743 0.010627 0.007262 0.003667 0.003471 0.004145 0.003911 0.004429
TRUMBULL 0.009605 0.009448 0.008275 0.007329 0.006806 0.007282 0.006622 0.006952 0.006406 0.006331 0.006842 0.009355 0.008991 0.008125 0.007666 0.006396 0.005815 0.006673 0.007017 0.006274 0.006109 0.006002 0.006493 0.007639 0.006448 0.007691 0.00665 0.006177 0.006316 0.007523 0.007355 0.00648 0.006068 0.005658 0.005794 0.008849 0.008359 0.008764 0.007054 0.006009 0.006073 0.008979 0.011097 0.010615 0.009194 0.005278 0.005718 0.006813 0.010351 0.011628
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment