Created
September 30, 2015 23:42
-
-
Save ranimolla/9119eb8ea75cf94eec6d to your computer and use it in GitHub Desktop.
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
Fuel | generation | 1/1/01 | 3/1/01 | 6/1/01 | 9/1/01 | 1/1/02 | 3/1/02 | 6/1/02 | 9/1/02 | 1/1/03 | 3/1/03 | 6/1/03 | 9/1/03 | 1/1/04 | 3/1/04 | 6/1/04 | 9/1/04 | 1/1/05 | 3/1/05 | 6/1/05 | 9/1/05 | 1/1/06 | 3/1/06 | 6/1/06 | 9/1/06 | 1/1/07 | 3/1/07 | 6/1/07 | 9/1/07 | 1/1/08 | 3/1/08 | 6/1/08 | 9/1/08 | 1/1/09 | 3/1/09 | 6/1/09 | 9/1/09 | 1/1/10 | 3/1/10 | 6/1/10 | 9/1/10 | 1/1/11 | 3/1/11 | 6/1/11 | 9/1/11 | 1/1/12 | 3/1/12 | 6/1/12 | 9/1/12 | 1/1/13 | 3/1/13 | 6/1/13 | 9/1/13 | 1/1/14 | 3/1/14 | 6/1/14 | 9/1/14 | 1/1/15 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
coal | mWh generation | 482.291606 | 454.879372 | 516.3348 | 450.450164 | 458.892491 | 458.378691 | 528.516458 | 487.342714 | 493.297409 | 454.507588 | 532.094143 | 493.837609 | 496.4489319 | 466.1292397 | 523.9260866 | 491.7962911 | 496.4446213 | 471.9081978 | 545.3207271 | 499.1994996 | 489.1773847 | 468.1294669 | 538.8661553 | 494.3381275 | 499.1533241 | 477.2757064 | 544.5798768 | 495.4466762 | 510.2851021 | 472.9422043 | 528.6662966 | 473.9076436 | 448.3712629 | 405.6948018 | 458.6390002 | 443.1991882 | 470.7699532 | 435.7158679 | 506.0914909 | 434.7129666 | 443.9590679 | 419.6450351 | 488.8075026 | 381.0183997 | 348.4885835 | 343.5291233 | 438.2199257 | 383.805312 | 392.2851863 | 369.6303986 | 435.4033429 | 383.7957881 | 437.7346694 | 366.6839701 | 425.3728142 | 355.9051595 | 368.4698363 | |
natural gas | mWh generation | 124.720004 | 154.380105 | 211.621086 | 148.407924 | 143.935727 | 165.052664 | 236.555267 | 145.462089 | 140.421345 | 149.021297 | 219.277915 | 141.186983 | 148.3744338 | 178.5294654 | 224.9647039 | 158.2314134 | 148.1472164 | 182.4777825 | 270.9615474 | 159.3737081 | 146.1376521 | 202.7366716 | 287.3585283 | 180.2079177 | 175.3004241 | 208.1339989 | 307.3527171 | 205.802651 | 194.8124928 | 209.9365217 | 279.1304015 | 199.1011831 | 196.7317098 | 213.5095018 | 303.2600044 | 207.4774645 | 203.8014763 | 230.5778939 | 328.779647 | 224.5382165 | 206.1254371 | 235.9629073 | 331.2184538 | 240.3821311 | 273.6213194 | 317.7779119 | 378.6110151 | 255.883929 | 253.567383 | 261.467088 | 343.9906111 | 265.8104777 | 244.32504 | 263.6839261 | 342.726362 | 271.1921949 | 291.2312833 | |
nuclear | mWh generation | 192.120196 | 185.538569 | 200.933987 | 190.233556 | 195.624784 | 187.84115 | 205.679305 | 190.918848 | 190.086214 | 183.158278 | 202.260277 | 188.227926 | 198.193225 | 191.27174 | 208.97558 | 190.087842 | 192.313703 | 184.598749 | 209.19054 | 195.883373 | 198.248035 | 188.734377 | 210.844916 | 189.391308 | 203.536131 | 191.249053 | 213.068023 | 198.571546 | 200.581423 | 192.477983 | 213.989203 | 199.159826 | 205.570267 | 194.538595 | 210.94537 | 187.800353 | 202.449432 | 192.5707596 | 212.858766 | 199.089343 | 203.193489 | 176.830386 | 210.53227 | 199.648222 | 197.956822 | 183.092461 | 203.242322 | 185.039644 | 195.836591 | 186.044727 | 207.682588 | 199.452567 | 198.100115 | 187.47025 | 210.603372 | 200.893721 | 202.278843 | |
conventional hydroelectric | mWh generation | 56.802126 | 57.916259 | 52.248914 | 49.993746 | 62.995428 | 79.122147 | 63.641353 | 58.569903 | 64.582212 | 82.740518 | 66.29558 | 62.188018 | 66.81120382 | 70.16003796 | 65.43550832 | 66.01055827 | 68.81500628 | 77.11985952 | 64.88620435 | 59.5001848 | 76.82294796 | 89.13141 | 64.36920693 | 58.92285078 | 68.77479973 | 72.75416756 | 57.16084575 | 48.82016127 | 61.23695535 | 78.63250659 | 62.96255608 | 51.99936709 | 63.12978097 | 84.56285708 | 60.32387864 | 65.42857749 | 63.85856512 | 74.02970647 | 61.90119997 | 60.41359783 | 80.79679126 | 95.93244692 | 78.42580452 | 64.19986075 | 69.29913866 | 81.59539021 | 67.12902553 | 58.21666898 | 65.78135075 | 80.93126541 | 65.84903941 | 56.00372716 | 63.30403285 | 77.27366391 | 59.95096922 | 58.22028227 | 71.74450631 | |
wind | mWh generation | 1.352612 | 1.989358 | 1.702427 | 1.692934 | 2.377436 | 3.228373 | 2.602974 | 2.145495 | 2.413819 | 3.146293 | 2.663879 | 2.963474 | 3.31223818 | 4.39371402 | 3.30470417 | 3.13308439 | 3.65867593 | 5.2408576 | 4.02732031 | 4.88369519 | 6.66322353 | 6.98221048 | 5.48939489 | 7.4543081 | 8.01919992 | 8.74446023 | 7.72445868 | 9.96180859 | 12.90695405 | 15.70594235 | 10.38425856 | 16.36594512 | 18.90206258 | 19.31907903 | 15.07012282 | 20.59486727 | 20.87527133 | 26.51100227 | 20.51524843 | 26.75072407 | 29.54670275 | 35.1788867 | 21.83125237 | 33.61975681 | 38.71081932 | 37.22206537 | 26.08011946 | 38.80869871 | 44.56974831 | 47.46308218 | 32.40157838 | 43.40533658 | 49.74582714 | 49.93767432 | 33.78178021 | 48.32610755 | 45.58267764 | |
biomass | mWh generation | 12.23492098 | 12.07471809 | 12.87810493 | 12.56031297 | 12.63144354 | 13.45940834 | 14.18193178 | 13.43597076 | 13.08504528 | 12.90132676 | 13.84103749 | 13.51367782 | 13.23895969 | 13.11887897 | 13.74689702 | 13.43271741 | 13.35577156 | 13.21257646 | 14.10890175 | 13.59955988 | 13.67728576 | 13.07947867 | 14.27797727 | 13.82587894 | 13.60092569 | 13.344483 | 14.43223203 | 14.1609373 | 13.71460133 | 13.45133741 | 14.29293741 | 13.57473551 | 13.14484716 | 13.01945199 | 14.37644604 | 13.95198902 | 13.58767865 | 13.68029021 | 14.64092236 | 14.18047512 | 13.81404707 | 13.51974496 | 14.89537545 | 14.44166252 | 14.18771697 | 13.57086348 | 15.02247292 | 14.84111258 | 14.73085993 | 14.5560025 | 15.88411403 | 15.68705005 | 15.70295051 | 15.67490567 | 16.64781772 | 16.29325308 | 15.54900717 | |
wood and wood-derived fuels | mWh generation | 8.740903 | 8.451965 | 9.106038 | 8.900999 | 9.060071 | 9.753024 | 10.122329 | 9.729616 | 9.254114 | 8.991964 | 9.750382 | 9.532636 | 9.46151605 | 9.16272571 | 9.8532386 | 9.63940302 | 9.6760887 | 9.30974828 | 10.09664224 | 9.77393741 | 9.67436077 | 9.15318218 | 10.18239189 | 9.75216122 | 9.65693021 | 9.34970603 | 10.149432 | 9.85795579 | 9.47098075 | 8.97102091 | 9.72837251 | 9.12947888 | 8.77190329 | 8.39649993 | 9.64291975 | 9.23881531 | 9.11100511 | 8.91874898 | 9.78397094 | 9.35843464 | 9.30750564 | 8.82236495 | 9.9241544 | 9.3950418 | 9.45912169 | 8.722411 | 9.94959125 | 9.66800515 | 9.78254904 | 9.36730342 | 10.52361966 | 10.35406484 | 10.66526861 | 10.34347818 | 11.14628188 | 10.89483304 | 10.56782411 | |
petroleum liquids | mWh generation | 37.907115 | 30.875122 | 29.717178 | 16.147601 | 16.759526 | 19.627528 | 23.371108 | 18.942394 | 30.604905 | 23.688349 | 28.492274 | 19.948395 | 29.30774276 | 25.09639629 | 26.59154906 | 19.3951673 | 22.30813561 | 18.83638831 | 33.90426412 | 24.79081819 | 9.90935149 | 10.00150972 | 14.73546628 | 9.81385068 | 16.13499665 | 11.53277931 | 13.47403273 | 8.36270919 | 7.68176798 | 8.3246233 | 8.51979788 | 7.39124436 | 9.32351495 | 5.80072027 | 6.24985802 | 4.59798811 | 5.71049384 | 5.72639188 | 7.06385436 | 4.83578362 | 4.39554754 | 4.18872992 | 4.12786777 | 3.37424597 | 3.05906729 | 3.34969113 | 3.71085296 | 3.2829174 | 3.85326526 | 3.09598612 | 3.49854271 | 3.37223584 | 9.98963323 | 2.80679126 | 3.05237794 | 2.85914042 | 8.27164601 | |
solar | mWh generation | 0.050566 | 0.210955 | 0.242096 | 0.039138 | 0.079282 | 0.199041 | 0.21307 | 0.063438 | 0.08107437 | 0.218953 | 0.180256 | 0.053718 | 0.07640075 | 0.22655362 | 0.21558022 | 0.05662042 | 0.05990617 | 0.22789221 | 0.20928219 | 0.05321377 | 0.06587199 | 0.19251151 | 0.19863929 | 0.05068322 | 0.08057405 | 0.22311438 | 0.2291686 | 0.07893597 | 0.12673054 | 0.3210486 | 0.3087571 | 0.10777856 | 0.11574077 | 0.31325327 | 0.33247294 | 0.12971242 | 0.11868944 | 0.44150429 | 0.45535539 | 0.19663245 | 0.24737972 | 0.57744843 | 0.6059841 | 0.3868834 | 0.46178859 | 1.30838779 | 1.42902381 | 1.12747505 | 1.36186549 | 2.2901303 | 2.72171767 | 2.66190693 | 3.12452923 | 5.54530963 | 5.62981275 | 4.0218025 | 5.02782307 | |
other biomass | mWh generation | 3.49401798 | 3.62275309 | 3.77206693 | 3.65931397 | 3.57137254 | 3.70638434 | 4.05960278 | 3.70635476 | 3.83093128 | 3.90936276 | 4.09065549 | 3.98104182 | 3.77744364 | 3.95615326 | 3.89365842 | 3.79331439 | 3.67968286 | 3.90282818 | 4.01225951 | 3.82562247 | 4.00292499 | 3.92629649 | 4.09558538 | 4.07371772 | 3.94399548 | 3.99477697 | 4.28280003 | 4.30298151 | 4.24362058 | 4.4803165 | 4.5645649 | 4.44525663 | 4.37294387 | 4.62295206 | 4.73352629 | 4.71317371 | 4.47667354 | 4.76154123 | 4.85695142 | 4.82204048 | 4.50654143 | 4.69738001 | 4.97122105 | 5.04662072 | 4.72859528 | 4.84845248 | 5.07288167 | 5.17310743 | 4.94831089 | 5.18869908 | 5.36049437 | 5.33298521 | 5.0376819 | 5.33142749 | 5.50153584 | 5.39842004 | 4.98118306 | |
geothermal | mWh generation | 3.492392 | 3.254199 | 3.484781 | 3.509129 | 3.664646 | 3.481408 | 3.684424 | 3.660831 | 3.600851 | 3.557941 | 3.650914 | 3.614525 | 3.74990434 | 3.5936229 | 3.72307676 | 3.74437055 | 3.51941072 | 3.69949903 | 3.74914172 | 3.72369374 | 3.601674 | 3.42386805 | 3.7697698 | 3.77271715 | 3.62223697 | 3.55086109 | 3.72268635 | 3.74142861 | 3.54632187 | 3.73733861 | 3.78515602 | 3.77116006 | 3.75692436 | 3.66608917 | 3.72340087 | 3.86224362 | 3.77747365 | 3.81447505 | 3.82309742 | 3.80416702 | 3.89943451 | 3.77117346 | 3.76921848 | 3.87624155 | 3.74173119 | 3.82977506 | 3.92446927 | 4.0664501 | 3.99633183 | 3.86012413 | 3.95861832 | 3.9595998 | 4.09153951 | 4.13828935 | 4.13431627 | 4.26428939 | 4.22426065 | |
other gases | mWh generation | 2.163282 | 2.215596 | 2.455158 | 2.205437 | 2.586303 | 2.808165 | 3.241176 | 2.827041 | 3.681219 | 3.849888 | 3.885556 | 4.183357 | 3.66736999 | 3.98093619 | 3.91502573 | 3.68909876 | 3.41672806 | 3.46190151 | 3.47796967 | 3.10754426 | 3.50417454 | 3.64253255 | 3.71175921 | 3.31834158 | 3.36941424 | 3.49135593 | 3.40551273 | 3.18707111 | 3.08495527 | 3.19741024 | 3.14429682 | 2.28021355 | 2.425195 | 2.40633783 | 3.00017455 | 2.80039917 | 2.74422164 | 2.92412224 | 2.977671 | 2.66677204 | 2.68260738 | 2.80496522 | 3.18968589 | 2.88894184 | 3.13710326 | 3.03176103 | 2.99563702 | 2.73308377 | 3.18187467 | 3.15519688 | 3.37794723 | 3.13791838 | 2.55047385 | 2.68240262 | 3.2376147 | 3.10734694 | 3.05728555 | |
other | mWh generation | 2.79477302 | 2.92122491 | 3.07390407 | 3.11644003 | 2.97752146 | 3.20525066 | 4.07519922 | 3.26893724 | 3.00956572 | 3.16407424 | 4.17601851 | 3.69484818 | 3.34636019 | 3.57811236 | 3.61380329 | 3.69412566 | 3.06030017 | 3.11730728 | 3.58868466 | 3.0547668 | 3.16439083 | 3.25930271 | 3.3067461 | 3.24395948 | 2.95891386 | 3.03222752 | 3.11408432 | 3.12590505 | 2.86929599 | 3.14638395 | 3.10509665 | 2.68288822 | 2.79444857 | 3.0604719 | 3.09241674 | 2.98099718 | 2.9254397 | 3.20871222 | 3.42049641 | 3.30069323 | 3.28028756 | 3.58660224 | 3.67098632 | 3.61572334 | 3.34879315 | 3.45630397 | 3.53065571 | 3.4513138 | 3.26100038 | 3.25847101 | 3.59925101 | 3.4687994 | 2.92212069 | 3.13284161 | 3.31420266 | 3.20678894 | 2.93821398 | |
petroleum coke | mWh generation | 2.279236 | 2.201083 | 2.896762 | 2.856124 | 3.812037 | 4.128777 | 4.354097 | 3.571927 | 3.030051 | 3.944588 | 4.85032 | 4.846759 | 5.22398169 | 5.02276425 | 5.26150205 | 5.24595328 | 5.54845351 | 5.61354187 | 5.93564803 | 5.28776725 | 5.16349626 | 4.87497797 | 5.27167911 | 4.39608249 | 4.15848882 | 4.19799858 | 4.14216937 | 3.73580321 | 3.63689248 | 3.435735 | 3.6186254 | 3.63392559 | 3.44713263 | 3.45764048 | 3.60299017 | 2.45666998 | 3.48101605 | 3.54323063 | 3.7056734 | 2.99456949 | 4.18818314 | 3.20723593 | 4.04436798 | 2.65606683 | 2.86119021 | 1.95105561 | 2.60702513 | 2.36774173 | 2.91557705 | 3.54313388 | 3.94844478 | 2.93725813 | 3.33653253 | 2.98057335 | 2.98778594 | 2.47603323 | 2.88878566 |
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 lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Conditional Styling</title> | |
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script> | |
<style type="text/css"> | |
body { | |
background-color: white; | |
font-family: Helvetica, Arial, sans-serif; | |
} | |
h1 { | |
font-size: 24px; | |
margin: 0; | |
} | |
p { | |
font-size: 14px; | |
margin: 10px 0 0 0; | |
} | |
svg { | |
background-color: white; | |
} | |
circle:hover { | |
fill: orange; | |
} | |
path { | |
stroke: gray; | |
stroke-width: 0.5; | |
} | |
g.highlight path { | |
stroke: steelblue; | |
stroke-width: 2; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: black; | |
stroke-width: 1; | |
shape-rendering: crispEdges; | |
} | |
.axis text { | |
font-family: sans-serif; | |
font-size: 11px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Electricity Source</h1> | |
<p>U.S. “coal” vs. “wind“ vs. other types of electricity production by quarter in megawatthours. Source: <a href="http://www.eia.gov/electricity/data/browser/">Energy Information Administration</a>, 2001-2015</p> | |
<script type="text/javascript"> | |
//Dimensions and padding | |
var w = 700; | |
var h = 600; | |
var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left | |
//Set up date formatting and years | |
var dateFormat = d3.time.format("%m/%d/%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(15) | |
.tickFormat(function(d) { | |
return dateFormat(d); | |
}); | |
var yAxis = d3.svg.axis() | |
.scale(yScale) | |
.orient("left"); | |
//Configure line generator | |
var line = d3.svg.line() | |
.x(function(d) { | |
return xScale(dateFormat.parse(d.myDates)); | |
}) | |
.y(function(d) { | |
return yScale(+d.amount); | |
}); | |
//Create the empty SVG image | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
//Load USA data | |
d3.csv("generation.csv", function(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] ] | |
// | |
//We, however, are using 'year' as x and 'amount' as y. | |
//We also need to know which country belongs to each | |
//line, so we will build an array of objects that is | |
//structured like this: | |
/* | |
[ | |
{ | |
country: "Australia", | |
emissions: [ | |
{ year: 1961, amount: 90589.568 }, | |
{ year: 1962, amount: 94912.961 }, | |
{ year: 1963, amount: 101029.517 }, | |
… | |
] | |
}, | |
{ | |
country: "Bermuda", | |
emissions: [ | |
{ year: 1961, amount: 176.016 }, | |
{ year: 1962, amount: 157.681 }, | |
{ year: 1963, amount: 150.347 }, | |
… | |
] | |
}, | |
… | |
] | |
*/ | |
//Note that this is an array of objects. Each object | |
//contains two values, 'country' and 'emissions'. | |
//The 'emissions' value is itself an array, containing | |
//more objects, each one holding 'year' and 'amount' values. | |
//New array with all the years, for referencing later | |
var myDates = ["1/1/01", "3/1/01", "6/1/01", "9/1/01", "1/1/02", "3/1/02", "6/1/02", "9/1/02", "1/1/03", "3/1/03", "6/1/03", "9/1/03", "1/1/04", "3/1/04", "6/1/04", "9/1/04", "1/1/05", "3/1/05", "6/1/05", "9/1/05", "1/1/06", "3/1/06", "6/1/06", "9/1/06", "1/1/07", "3/1/07", "6/1/07", "9/1/07", "1/1/08", "3/1/08," "6/1/08", "9/1/08", "1/1/09", "3/1/09", "6/1/09", "9/1/09", "1/1/10", "3/1/10", "6/1/10", "9/1/10", "1/1/11", "3/1/11", "6/1/11", "9/1/11", "1/1/12", "3/1/12", "6/1/12", "9/1/12", "1/1/13", "3/1/13", "6/1/13", "9/1/13", "1/1/14", "3/1/14", "6/1/14", "9/1/14", "1/1/15"]; | |
//Create a new, empty array to hold our restructured dataset | |
var dataset = []; | |
//Loop once for each row in data | |
for (var i = 0; i < data.length; i++) { | |
//Create new object with this country's name and empty array | |
dataset[i] = { | |
fuel: data[i].fuelName, | |
generation: [] | |
}; | |
//Loop through all the years | |
for (var j = 0; j < year.length; j++) { | |
// If value is not empty | |
if (data[i][year[j]]) { | |
//Add a new object to the emissions data array | |
//for this country | |
dataset[i].generation.push({ | |
year: year[j], | |
amount: data[i][year[j]] | |
}); | |
} | |
} | |
} | |
//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); | |
//Set scale domains | |
xScale.domain([ | |
d3.min(myDates, function(d) { | |
return dateFormat.parse(d); | |
}), | |
d3.max(myDates, function(d) { | |
return dateFormat.parse(d); | |
}) | |
]); | |
yScale.domain([ | |
d3.max(dataset, function(d) { | |
return d3.max(d.generations, function(d) { | |
return +d.amount; | |
}); | |
}), | |
0 | |
]); | |
//Make a group for each country | |
var groups = svg.selectAll("g") | |
.data(dataset) | |
.enter() | |
.append("g") | |
.classed("highlight", function(d) { | |
if (d.fuel == "wind" || | |
d.fuel == "solar") { | |
return true; | |
} else { | |
return false; | |
} | |
}); | |
//Append a title with the country name (so we get easy tooltips) | |
groups.append("title") | |
.text(function(d) { | |
return d.fuel; | |
}); | |
//Within each group, create a new line/path, | |
//binding just the emissions data to each one | |
groups.selectAll("path") | |
.data(function(d) { | |
return [ d.generation ]; | |
}) | |
.enter() | |
.append("path") | |
.attr("class", "line") | |
.attr("d", line) | |
.attr("fill", "none") | |
.attr("stroke-width", 2); | |
//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); | |
}); | |
//End USA data load function | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment