Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 7.42 KB | None | 0 0
  1. <!--
  2.     Christian Barboza  
  3.     01420751   
  4.     CIS 468 Data Visualization
  5.     Professor Koop     
  6.     Assignment #3
  7. -->
  8.  
  9. <html>
  10.  
  11.     <head>
  12.         <script src="http://www.cis.umassd.edu/~dkoop/cis468-2017sp/a3/fifa-17-women.json"></script>
  13.         <script src="http://www.cis.umassd.edu/~dkoop/cis468-2017sp/a3/guardian-16-men.json" type="text/javascript"></script>
  14.         <script src="http://www.cis.umassd.edu/~dkoop/cis468-2017sp/a3/soccer-teammates-men.json"></script>
  15.         <script src="https://cdn.rawgit.com/johan/world.geo.json/master/countries.geo.json" type="text/javascript"></script>
  16.     </head>
  17.  
  18.     <style>
  19.         body {
  20.           background: #fcfcfa;
  21.         }
  22.  
  23.         .stroke {
  24.           fill: none;
  25.           stroke: black;
  26.           stroke-width: 1px;
  27.         }
  28.  
  29.         .fill {
  30.           fill: #fff;
  31.         }
  32.  
  33.         .graticule {
  34.           fill: none;
  35.           stroke: black;
  36.           stroke-width: 1px;
  37.         }
  38.  
  39.         .land {
  40.           fill: black;
  41.         }
  42.        
  43.         .country {
  44.             fill: white;
  45.             stroke: black;
  46.             stroke-width: 1px;
  47.         }
  48.        
  49.         .boundary {
  50.           fill: none;
  51.           stroke: black;
  52.           stroke-width: 1px;
  53.         }
  54.     </style>
  55.  
  56.     <body>
  57.         <script src='https://cdnjs.cloudflare.com/ajax/libs/d3/4.7.3/d3.min.js'></script>
  58.         <script src='https://cdnjs.cloudflare.com/ajax/libs/topojson/2.2.0/topojson.min.js'></script>
  59.        
  60.         <h1> Assignment 3 </h1>
  61.         <h2> Brennan Clark </h2>
  62.         <h2> #01420751 </h2>
  63.         <h2> Data Visualization (CIS468) </h2>
  64.         <label><em> This assignment is all my own work. I did not copy the code from any other source. </em></label>
  65.  
  66.         <h1>Women Soccer Players- Choropleth </h1>
  67.         <div id="women"></div>
  68.        
  69.         <h1>Men Soccer Players- Choropleth </h1>
  70.         <div id="men"></div>
  71.        
  72.         <h1> Force-Directed Graph </h1>
  73.         <div id="force"></div>
  74.        
  75.         <!--Women's Choropleth -->
  76.         <div id = "women">
  77.         </div>
  78.         <script>  
  79.             var width = 960, height = 580;
  80.            
  81.             var color = d3.scaleSequential(function (d) {
  82.                 return d3.interpolateReds(d/12);
  83.             });
  84.            
  85.             var projection = d3.geoMercator()
  86.                 .scale( 100 )
  87.                 .translate( [ width / 2, height / 2 ] )
  88.                 .precision( .1 );
  89.  
  90.             var path = d3.geoPath()
  91.                 .projection( projection );
  92.  
  93.             var svg = d3.select( "#women" ).append( "svg" )
  94.                 .attr( "width", width )
  95.                 .attr( "height", height );
  96.  
  97.             svg.append( "defs" ).append( "path" )
  98.                 .datum( { type: "Sphere" } )
  99.                 .attr( "id", "sphere" )
  100.                 .attr( "d", path );
  101.  
  102.             svg.append( "use" )
  103.                 .attr( "class", "stroke" )
  104.                 .attr( "xlink:href", "#sphere" );
  105.  
  106.             svg.append( "use" )
  107.                 .attr( "class", "fill" )
  108.                 .attr( "xlink:href", "#sphere" );
  109.        
  110.             d3.queue()    
  111.                     .defer(d3.json, "https://cdn.rawgit.com/johan/world.geo.json/master/countries.geo.json")
  112.                     .defer(d3.json, "http://www.cis.umassd.edu/~dkoop/cis468-2017sp/a3/fifa-17-women.json")
  113.                     .await(ready);
  114.    
  115.             function ready(error, map, data)
  116.             {  
  117.                 svg.append("g")
  118.                     .attr("class", "countries")
  119.                     .selectAll("path")
  120.                     .data(map.features)
  121.                     .enter().append("path")
  122.                         .attr("d", path)
  123.                         .attr("fill", function (d) { return color(data.filter(function (i) {return d.properties.name == i['Country'];}).length);})
  124.                         .attr("stroke","black");
  125.             }      
  126.         </script>
  127.        
  128.         <!--Men's Choropleth -->
  129.         <div id = "men">
  130.         </div>
  131.        
  132.         <script>     
  133.             var width = 960, height = 580;
  134.             var color = d3.scaleOrdinal(d3.schemeCategory10);
  135.  
  136.             var projection = d3.geoMercator()
  137.                 .scale(100) //Changing this makes the whole thing smaller, but still clips a bit of Russia out.
  138.                 .translate([width / 2, height / 2])
  139.                 .precision(.1);
  140.  
  141.             var path = d3.geoPath()
  142.                 .projection(projection);
  143.                
  144.             //Make sure it's in the men's soccer section
  145.             var svg = d3.select("#men").append("svg")
  146.                 .attr("width", width)
  147.                 .attr("height", height);
  148.  
  149.             svg.append("defs").append("path")
  150.                 .datum({type: "Sphere"})
  151.                 .attr("id", "sphere")
  152.                 .attr("d", path);
  153.  
  154.             svg.append("use")
  155.                 .attr("class", "stroke")
  156.                 .attr("xlink:href", "#sphere");
  157.  
  158.             svg.append("use")
  159.                 .attr("class", "fill")
  160.                 .attr("xlink:href", "#sphere");
  161.                
  162.        
  163.             //Different map than the given- this one had nicer values, but is slightly cut off
  164.             d3.json("https://giottojs.org/geo/world-110m.json", function(error, world) {
  165.               if (error) throw error;
  166.  
  167.                     var countries = topojson.feature(world, world.objects.countries).features,
  168.                         neighbors = topojson.neighbors(world.objects.countries.geometries);
  169.  
  170.               svg.selectAll(".country")
  171.                   .data(countries)
  172.                 .enter().insert("path", ".graticule")
  173.                   .attr("class", "country")
  174.                   .attr("d", path)
  175.                   //.style("fill", function(d, i) { return color(d.color = d3.max(neighbors[i], function(n) { return countries[n].color; }) + 1 | 0); });
  176.  
  177.               svg.insert("path", ".graticule")
  178.                   .datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
  179.                   .attr("class", "boundary")
  180.                   .attr("d", path);
  181.             });
  182.             /*
  183.             function ready(error, map, data)
  184.             {
  185.                 svg.append("g")
  186.                     .attr("class", "countries")
  187.                     .selectAll("path")
  188.                     .data(map.features)
  189.                     .enter().append("path")
  190.                         .attr("d", path)
  191.                         .attr("filll", function (d) { return color(data.filter(function (i)  { return d.properties.name == i[dataKey]; }).length;)})
  192.                         .attr("stroke", "black")   
  193.             }*/
  194.         </script>
  195.        
  196.         <!--Force-Directed Graph -->
  197.         <div id = "force">
  198.         </div>
  199.         <meta charset="utf-8">
  200.         <style>
  201.  
  202.         .links line {
  203.           stroke: #999;
  204.           stroke-opacity: 0.6;
  205.         }
  206.  
  207.         .nodes circle {
  208.           stroke: #fff;
  209.           stroke-width: 1.5px;
  210.         }
  211.  
  212.         </style>
  213.        
  214.         <!--Load the D3 library -->
  215.         <script src="https://d3js.org/d3.v4.min.js"></script>
  216.         <script>
  217.  
  218.         var svg = d3.select("svg"),
  219.             width = 960,
  220.             height = 600;
  221.  
  222.         var color = d3.scaleOrdinal(d3.schemeCategory20);
  223.  
  224.         var simulation = d3.forceSimulation()
  225.             .force("link", d3.forceLink().id(function(d) { return d.id; }))
  226.             .force("charge", d3.forceManyBody())
  227.             .force("center", d3.forceCenter(width / 2, height / 2));
  228.  
  229.         d3.json("miserables.json", function(error, graph) {
  230.           if (error) throw error;
  231.  
  232.           var link = svg.append("g")
  233.               .attr("class", "links")
  234.             .selectAll("line")
  235.             .data(graph.links)
  236.             .enter().append("line")
  237.               .attr("stroke-width", function(d) { return Math.sqrt(d.value); });
  238.  
  239.           var node = svg.append("g")
  240.               .attr("class", "nodes")
  241.             .selectAll("circle")
  242.             .data(graph.nodes)
  243.             .enter().append("circle")
  244.               .attr("r", 5)
  245.               .attr("fill", function(d) { return color(d.group); })
  246.               .call(d3.drag()
  247.                   .on("start", dragstarted)
  248.                   .on("drag", dragged)
  249.                   .on("end", dragended));
  250.  
  251.           node.append("title")
  252.               .text(function(d) { return d.id; });
  253.  
  254.           simulation
  255.               .nodes(graph.nodes)
  256.               .on("tick", ticked);
  257.  
  258.           simulation.force("link")
  259.               .links(graph.links);
  260.  
  261.           function ticked() {
  262.             link
  263.                 .attr("x1", function(d) { return d.source.x; })
  264.                 .attr("y1", function(d) { return d.source.y; })
  265.                 .attr("x2", function(d) { return d.target.x; })
  266.                 .attr("y2", function(d) { return d.target.y; });
  267.  
  268.             node
  269.                 .attr("cx", function(d) { return d.x; })
  270.                 .attr("cy", function(d) { return d.y; });
  271.           }
  272.         });
  273.  
  274.         function dragstarted(d) {
  275.           if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  276.           d.fx = d.x;
  277.           d.fy = d.y;
  278.         }
  279.  
  280.         function dragged(d) {
  281.           d.fx = d3.event.x;
  282.           d.fy = d3.event.y;
  283.         }
  284.  
  285.         function dragended(d) {
  286.           if (!d3.event.active) simulation.alphaTarget(0);
  287.           d.fx = null;
  288.           d.fy = null;
  289.         }
  290.  
  291.         </script>
  292.        
  293.        
  294.     </body>
  295.    
  296. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement