Advertisement
Guest User

Untitled

a guest
May 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <script src="http://d3js.org/d3.v3.min.js"></script>
  5.     <script src="http://d3js.org/topojson.v1.min.js"></script>
  6.   </head>
  7.   <body>
  8.     <script>
  9.       var width = 960;
  10.       var height = 500;
  11.       var projection = d3.geo.albers();
  12.       var path = d3.geo.path().projection(projection);
  13.       var svg = d3
  14.         .select("body")
  15.         .append("svg")
  16.         .attr("width", width)
  17.         .attr("height", height);
  18.     var voteData = [];
  19.    
  20.    
  21.      
  22.       d3.csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018/2018-10-09/voter_turnout.csv", function(data) {
  23.         data.forEach(function(d) {
  24.           if (
  25.             d.year == 2014 &&
  26.            (d.state != "United States (Excl. Louisiana)" ||
  27.             d.state != "United States")
  28.          ) {
  29.            voteData.push(d);
  30.           }
  31.         });
  32.        
  33.        
  34.  
  35.         d3.json("us-states.json", function(geojson) {
  36.        
  37.         for (var i = 0; i < voteData.length; i++) {
  38.             var stateName = voteData[i].state;
  39.             var stateData = voteData[i].votes;
  40.             for (var j = 0; j < geojson.features.length; j++) {
  41.                 var state = geojson.features[j].properties.name;
  42.                 if(stateName == state) {
  43.                     geojson.features[j].properties.votes = stateData;
  44.                     break;
  45.                 }
  46.             }
  47.         }
  48.         console.log(d3.min(voteData, function(d) { return d.votes;}))
  49.        
  50.                
  51.        var states = svg
  52.          .selectAll("path.state")
  53.          .data(geojson.features)
  54.          .enter()
  55.          .append("path")
  56.          .attr("d", path)
  57.         .style("stroke", "#fff")
  58.         .style("stroke-width", "1")
  59.          .style("fill-opacity", function(d) {
  60.            return opacity(d.votes);
  61.          });
  62.          
  63.    
  64.      
  65.       });
  66.      
  67.       });
  68.    </script>
  69.   </body>
  70. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement