Guest User

Untitled

a guest
Jan 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.89 KB | None | 0 0
  1. function update(year){
  2. slider.property("value", year);
  3. d3.select(".year").text(year);
  4. // us_data.style("fill", function(d) {
  5. // return color(d.properties.years[year][0].rate)
  6. // });
  7. }
  8.  
  9.  
  10. var slider = d3.select(".slider")
  11. .append("input")
  12. .attr("type", "range")
  13. .attr("min", 2009)
  14. .attr("max", 2013)
  15. .attr("step", 1)
  16. .on("input", function() {
  17. var year = this.value;
  18. update(year);
  19. });
  20.  
  21. update(2013);
  22.  
  23. <h2 id="vardatayear" class="year"></h2>
  24. <div class="slider"></div>
  25.  
  26. var honeyyear = document.getElementById("vardatayear").value || 2013;
  27.  
  28. d3.json( `data/HoneyProduction-${honeyyear}.json`, function( honey_data ){...
  29.  
  30. <!DOCTYPE html>
  31. <html lang="en">
  32. <head>
  33. <meta charset="UTF-8">
  34. <title>VI - Gonçalo Peres</title>
  35.  
  36. <link rel="stylesheet" type="text/css" href="css/style.css">
  37. <link rel="stylesheet" type="text/css"
  38. href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  39. </head>
  40. <body>
  41. <nav class="navbar navbar-default">
  42. <div class="container">
  43. <a class="navbar-brand" href="#"><img id="logo" src="img/logo.png"></a>
  44. <br>
  45. <div id="mySidenav" class="sidenav">
  46. <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  47. <a href="#">Contexto</a>
  48. <a href="#">1.ª Codificação Visual</a>
  49. <a href="#">2.ª Codificação Visual</a>
  50. <a href="#">3.ª Codificação Visual</a>
  51. </div>
  52. </div>
  53. <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776;</span>
  54. <br>
  55. <br>
  56. </nav>
  57. <div id="intro">
  58. <h2>1.ª Codificação Visual</h2>
  59. <span>Lorem ipsum.</span><br>
  60. <span>Lorem Ipsum.</span><br>
  61. </div>
  62. <div id="legenda">
  63. <h4>Legenda</h4>
  64. <span>Lorem ipsum.</span><br>
  65. <span>Lorem ipsum.</span><br>
  66. </div>
  67. <br>
  68. <div id="content">
  69. <h1>U.S. Department of Agriculture - Honey Production, 2009-2013</h1>
  70. <h2 id="vardatayear" class="year"></h2>
  71. <div class="slider"></div>
  72. <div id="map"></div>
  73. </div>
  74. <div id="chart"></div>
  75. <div id="buttons">
  76. <button type="button" class="panning up"><i class="fa fa-arrow-up"></i></button>
  77. <button type="button" class="panning down"><i class="fa fa-arrow-down"></i></button>
  78. <button type="button" class="panning left"><i class="fa fa-arrow-left"></i></button>
  79. <button type="button" class="panning right"><i class="fa fa-arrow-right"></i></button>
  80. <button type="button" class="zooming in"><i class="fa fa-plus"></i></button>
  81. <button type="button" class="zooming out"><i class="fa fa-minus"></i></button>
  82. </div>
  83.  
  84. <div id="note">
  85. <span>Gonçalo Peres | <b><a class="website" href="http://goncaloperes.com/" target="_blank">GoncaloPeres.com</a></b></span><br>
  86. <span>Data from: <a class="website" href="https://public.enigma.com/datasets/u-s-department-of-agriculture-honey-production-2013/41cf2441-e96f-4113-a02f-402d167a9cd8" target="_blank">Enigma Public</a></span>
  87. </div>
  88.  
  89. <script src="https://d3js.org/d3.v4.js"></script>
  90. <script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
  91. <script src="js/app.js"></script>
  92. </body>
  93.  
  94. // Width and height
  95. var chart_width = 800;
  96. var chart_height = 600;
  97. var color = d3.scaleQuantize().range([
  98. "#f7fbff","#f6faff","#f5fafe","#f5f9fe",
  99. "#f4f9fe","#f3f8fe","#f2f8fd","#f2f7fd",
  100. "#f1f7fd","#f0f6fd","#eff6fc","#eef5fc",
  101. "#eef5fc","#edf4fc","#ecf4fb","#ebf3fb",
  102. "#eaf3fb","#eaf2fb","#e9f2fa","#e8f1fa",
  103. "#e7f1fa","#e7f0fa","#e6f0f9","#e5eff9",
  104. "#e4eff9","#e3eef9","#e3eef8","#e2edf8",
  105. "#e1edf8","#e0ecf8","#e0ecf7","#dfebf7",
  106. "#deebf7","#ddeaf7","#ddeaf6","#dce9f6",
  107. "#dbe9f6","#dae8f6","#d9e8f5","#d9e7f5",
  108. "#d8e7f5","#d7e6f5","#d6e6f4","#d6e5f4",
  109. "#d5e5f4","#d4e4f4","#d3e4f3","#d2e3f3",
  110. "#d2e3f3","#d1e2f3","#d0e2f2","#cfe1f2",
  111. "#cee1f2","#cde0f1","#cce0f1","#ccdff1",
  112. "#cbdff1","#cadef0","#c9def0","#c8ddf0",
  113. "#c7ddef","#c6dcef","#c5dcef","#c4dbee",
  114. "#c3dbee","#c2daee","#c1daed","#c0d9ed",
  115. "#bfd9ec","#bed8ec","#bdd8ec","#bcd7eb",
  116. "#bbd7eb","#b9d6eb","#b8d5ea","#b7d5ea",
  117. "#b6d4e9","#b5d4e9","#b4d3e9","#b2d3e8",
  118. "#b1d2e8","#b0d1e7","#afd1e7","#add0e7",
  119. "#acd0e6","#abcfe6","#a9cfe5","#a8cee5",
  120. "#a7cde5","#a5cde4","#a4cce4","#a3cbe3",
  121. "#a1cbe3","#a0cae3","#9ec9e2","#9dc9e2",
  122. "#9cc8e1","#9ac7e1","#99c6e1","#97c6e0",
  123. "#96c5e0","#94c4df","#93c3df","#91c3df",
  124. "#90c2de","#8ec1de","#8dc0de","#8bc0dd",
  125. "#8abfdd","#88bedc","#87bddc","#85bcdc",
  126. "#84bbdb","#82bbdb","#81badb","#7fb9da",
  127. "#7eb8da","#7cb7d9","#7bb6d9","#79b5d9",
  128. "#78b5d8","#76b4d8","#75b3d7","#73b2d7",
  129. "#72b1d7","#70b0d6","#6fafd6","#6daed5",
  130. "#6caed5","#6badd5","#69acd4","#68abd4",
  131. "#66aad3","#65a9d3","#63a8d2","#62a7d2",
  132. "#61a7d1","#5fa6d1","#5ea5d0","#5da4d0",
  133. "#5ba3d0","#5aa2cf","#59a1cf","#57a0ce",
  134. "#569fce","#559ecd","#549ecd","#529dcc",
  135. "#519ccc","#509bcb","#4f9acb","#4d99ca",
  136. "#4c98ca","#4b97c9","#4a96c9","#4895c8",
  137. "#4794c8","#4693c7","#4592c7","#4492c6",
  138. "#4391c6","#4190c5","#408fc4","#3f8ec4",
  139. "#3e8dc3","#3d8cc3","#3c8bc2","#3b8ac2",
  140. "#3a89c1","#3988c1","#3787c0","#3686c0",
  141. "#3585bf","#3484bf","#3383be","#3282bd",
  142. "#3181bd","#3080bc","#2f7fbc","#2e7ebb",
  143. "#2d7dbb","#2c7cba","#2b7bb9","#2a7ab9",
  144. "#2979b8","#2878b8","#2777b7","#2676b6",
  145. "#2574b6","#2473b5","#2372b4","#2371b4",
  146. "#2270b3","#216fb3","#206eb2","#1f6db1",
  147. "#1e6cb0","#1d6bb0","#1c6aaf","#1c69ae",
  148. "#1b68ae","#1a67ad","#1966ac","#1865ab",
  149. "#1864aa","#1763aa","#1662a9","#1561a8",
  150. "#1560a7","#145fa6","#135ea5","#135da4",
  151. "#125ca4","#115ba3","#115aa2","#1059a1",
  152. "#1058a0","#0f579f","#0e569e","#0e559d",
  153. "#0e549c","#0d539a","#0d5299","#0c5198",
  154. "#0c5097","#0b4f96","#0b4e95","#0b4d93",
  155. "#0b4c92","#0a4b91","#0a4a90","#0a498e",
  156. "#0a488d","#09478c","#09468a","#094589",
  157. "#094487","#094386","#094285","#094183",
  158. "#084082","#083e80","#083d7f","#083c7d",
  159. "#083b7c","#083a7a","#083979","#083877",
  160. "#083776","#083674","#083573","#083471",
  161. "#083370","#08326e","#08316d","#08306b"
  162. ]);
  163.  
  164.  
  165. //Navbar
  166. function openNav() {
  167. document.getElementById("mySidenav").style.width = "100%";
  168. }
  169.  
  170. function closeNav() {
  171. document.getElementById("mySidenav").style.width = "0";
  172. }
  173.  
  174.  
  175. // Projection
  176. var projection = d3.geoAlbersUsa()
  177. .translate([ 0,0 ]);
  178. var path = d3.geoPath( projection );
  179. // .projection( projection );
  180.  
  181.  
  182. // Create SVG
  183. var svg = d3.select("#chart")
  184. .append("svg")
  185. .attr("width", chart_width)
  186. .attr("height", chart_height);
  187.  
  188. var zoom_map = d3.zoom()
  189. .scaleExtent([ 0.5, 3.0 ])
  190. .translateExtent([
  191. [ -1000, -500 ],
  192. [ 1000, 500 ]
  193. ])
  194. .on( 'zoom', function(){
  195. // console.log( d3.event );
  196. var offset = [
  197. d3.event.transform.x,
  198. d3.event.transform.y
  199. ];
  200. var scale = d3.event.transform.k * 2000;
  201.  
  202. projection.translate( offset )
  203. .scale( scale );
  204.  
  205. svg.selectAll( 'path' )
  206. .transition()
  207. .attr( 'd', path );
  208.  
  209. svg.selectAll( 'circle' )
  210. .transition()
  211. .attr( "cx", function(d) {
  212. return projection([d.longitude, d.latitude])[0];
  213. })
  214. .attr( "cy", function(d) {
  215. return projection([d.longitude, d.latitude])[1];
  216. });
  217. });
  218.  
  219. var map = svg.append( 'g' )
  220. .attr( 'id', 'map' )
  221. .call( zoom_map )
  222. .call(
  223. zoom_map.transform,
  224. d3.zoomIdentity
  225. .translate( chart_width / 2, chart_height / 2 )
  226. .scale( 1 )
  227. );
  228.  
  229. map.append( 'rect' )
  230. .attr( 'x', 0 )
  231. .attr( 'y', 0 )
  232. .attr( 'width', chart_width )
  233. .attr( 'height', chart_height )
  234. .attr( 'opacity', 0 );
  235.  
  236.  
  237. // Data
  238. var honeyyear = document.getElementById("vardatayear").value || 2013;
  239.  
  240. // if(!honeyyear){
  241. // honeyyear = 2013
  242. // }
  243.  
  244. // Select what are we analyzing
  245. // var honeyattribute = document.getElementById('some_input_id').value;
  246.  
  247. d3.json( `data/HoneyProduction-${honeyyear}.json`, function( honey_data ){
  248.  
  249. color.domain([
  250. d3.min( honey_data, function(d){
  251. return d.average_price_per_pound;
  252. }),
  253. d3.max( honey_data, function(d){
  254. return d.average_price_per_pound;
  255. })
  256. ]);
  257.  
  258. d3.json( 'data/us.json', function( us_data ){
  259. us_data.features.forEach(function(us_e, us_i){
  260. honey_data.forEach(function(h_e,h_i){
  261. if( us_e.properties.name !== h_e.state ){
  262. return null;
  263. }
  264.  
  265. us_data.features[us_i].properties.average_price_per_pound = parseFloat(h_e.average_price_per_pound);
  266. });
  267. });
  268.  
  269. // console.log(us_data);
  270.  
  271. map.selectAll( 'path' )
  272. .data( us_data.features )
  273. .enter()
  274. .append( 'path' )
  275. .attr( 'd', path )
  276. .attr( 'fill', function( d ){
  277. var average_price_per_pound = d.properties.average_price_per_pound;
  278. return average_price_per_pound ? color( average_price_per_pound ) : '#525252';
  279. })
  280. .attr( 'stroke', '#fff' )
  281. .attr( 'stroke-width', 1 )
  282. .append('title')
  283. // .text(d => console.log(d))
  284. .text(d => d.properties.average_price_per_pound);
  285.  
  286. // draw_cities();
  287.  
  288. });
  289. });
  290.  
  291. function draw_cities(){
  292. d3.json( 'data/us-city.json', function( city_data ){
  293. map.selectAll("circle")
  294. .data(city_data)
  295. .enter()
  296. .append( "circle" )
  297. .style( "fill", "#9D497A" )
  298. .style( "opacity", 0.8 )
  299. .attr( 'cx', function( d ){
  300. return projection([ d.longitude, d.latitude ])[0];
  301. })
  302. .attr( 'cy', function( d ){
  303. return projection([ d.longitude, d.latitude ])[1];
  304. })
  305. .attr( 'r', function(d){
  306. return Math.sqrt( parseInt(d.population) * 0.00005 );
  307. })
  308. .append( 'title' )
  309. .text(function(d){
  310. return d.city;
  311. });
  312. });
  313. }
  314.  
  315. d3.selectAll( '#buttons button.panning' ).on( 'click', function(){
  316. var x = 0;
  317. var y = 0;
  318. var distance = 100;
  319. var direction = d3.select( this ).attr( 'class' ).replace( 'panning ', '' );
  320.  
  321. if( direction === "up" ){
  322. y += distance; // Increase y offset
  323. }else if( direction === "down" ){
  324. y -= distance; // Decrease y offset
  325. }else if( direction === "left" ){
  326. x += distance; // Increase x offset
  327. }else if( direction === "right" ){
  328. x -= distance; // Decrease x offset
  329. }
  330.  
  331. map.transition()
  332. .call( zoom_map.translateBy, x, y );
  333. });
  334.  
  335. d3.selectAll( '#buttons button.zooming' ).on( 'click', function(){
  336. var scale = 1;
  337. var direction = d3.select(this).attr("class").replace( 'zooming ', '' );
  338.  
  339. if( direction === "in" ){
  340. scale = 1.25;
  341. }else if(direction === "out"){
  342. scale = 0.75;
  343. }
  344.  
  345. map.transition()
  346. .call(zoom_map.scaleBy, scale);
  347. });
  348.  
  349.  
  350.  
  351. // Slider
  352. function update(year){
  353. slider.property("value", year);
  354. d3.select(".year").text(year);
  355. // us_data.style("fill", function(d) {
  356. // return color(d.properties.years[year][0].rate)
  357. // });
  358. }
  359.  
  360.  
  361. var slider = d3.select(".slider")
  362. .append("input")
  363. .attr("type", "range")
  364. .attr("min", 2009)
  365. .attr("max", 2013)
  366. .attr("step", 1)
  367. .on("input", function() {
  368. var year = this.value;
  369. update(year);
  370. });
  371.  
  372. update(2013);
  373.  
  374. #intro {
  375. position: absolute;
  376. top: 250px;
  377. left: 125px;
  378. width: 180px;
  379. text-align: left;
  380. color: #B5B5B5;
  381. }
  382.  
  383.  
  384. #intro h2{
  385. font-family: Oswald;
  386. font-size: 25px;
  387. font-weight: 300;
  388. text-align: center;
  389. color: white;
  390. -webkit-margin-before: 0.5em;
  391. -webkit-margin-after: 0.3em;
  392. }
  393.  
  394. #legenda {
  395. position: absolute;
  396. top: 250px;
  397. right: 125px;
  398. width: 180px;
  399. text-align: left;
  400. color: #B5B5B5;
  401. }
  402.  
  403. #legenda h4{
  404. font-family: Oswald;
  405. font-size: 20px;
  406. font-weight: 300;
  407. text-align: center;
  408. color: white;
  409. -webkit-margin-before: 0.5em;
  410. -webkit-margin-after: 0.3em;
  411. }
  412.  
  413. body {
  414. font-size: 11px;
  415. font-weight: 400;
  416. font-family: 'Open Sans', sans-serif;
  417. text-align: center;
  418. vertical-align: middle;
  419. background: #111;
  420. fill: white;
  421. color: white;
  422. cursor:default;
  423. }
  424.  
  425. .navbar-brand {
  426. height: 60px;
  427. padding: 5px 0px;
  428. }
  429.  
  430. #chart{
  431. width: 800px;
  432. height: 600px;
  433. background-color: #111;
  434. margin: 25px auto;
  435. }
  436. #buttons{
  437. width: 800px;
  438. margin: 25px auto;
  439. text-align: center;
  440. }
  441. button{
  442. background-color: white;
  443. color: black;
  444. width: 100px;
  445. padding: 10px;
  446. font-size: 20px;
  447. text-align: center;
  448. border: 0;
  449. outline: 0;
  450. transition: all .2s linear;
  451. cursor: pointer;
  452. }
  453. button:hover{
  454. background-color: midnightblue;
  455. }
  456.  
  457. #note {
  458. top: -10px;
  459. left: 10px;
  460. font-size: 12px;
  461. font-weight: 300;
  462. color: #6E6E6E;
  463. text-align: center;
  464. }
  465.  
  466. .website {
  467. color: #6E6E6E;
  468. }
  469.  
  470. .sidenav {
  471. height: 100%;
  472. width: 0;
  473. position: fixed;
  474. z-index: 1;
  475. top: 0;
  476. left: 0;
  477. background-color: black;
  478. overflow-x: hidden;
  479. transition: 0.5s;
  480. padding-top: 60px;
  481. text-align:center;
  482. }
  483.  
  484. .sidenav a {
  485. padding: 8px 8px 8px 32px;
  486. text-decoration: none;
  487. font-size: 25px;
  488. color: #818181;
  489. display: block;
  490. transition: 0.3s;
  491. }
  492.  
  493. .sidenav a:hover{
  494. color: #f1f1f1;
  495. }
  496.  
  497. .sidenav .closebtn {
  498. position: absolute;
  499. top: 0;
  500. right: 25px;
  501. font-size: 36px;
  502. margin-left: 50px;
  503. }
  504.  
  505. @media screen and (max-height: 450px) {
  506. .sidenav {padding-top: 15px;}
  507. .sidenav a {font-size: 18px;}
  508. }
Add Comment
Please, Sign In to add comment