Advertisement
Guest User

Untitled

a guest
May 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.89 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
  5.     <style>
  6.       .axis {
  7.         font: 10px sans-serif;
  8.       }
  9.  
  10.       .axis path,
  11.       .axis line {
  12.         fill: none;
  13.         stroke: black;
  14.         shape-rendering: crispEdges;
  15.       }
  16.       svg {
  17.         background: white;
  18.       }
  19.       div.tooltip {
  20.         position: absolute;
  21.         text-align: center;
  22.         width: 150px;
  23.         height: 30px;
  24.         font: 12px sans-serif;
  25.         background: rgb(216, 213, 213);
  26.         border-radius: 8px;
  27.       }
  28.     </style>
  29.   </head>
  30.   <body>
  31.     <h1>VIZ GRAF</h1>
  32.     <script>
  33.       var votes = [];
  34.       var eligibleVotes = [];
  35.       var years = [];
  36.       var dataUSA = [];
  37.  
  38.       var margin = { left: 70, right: 40, top: 20, bottom: 50 };
  39.       var width = 800 - margin.left - margin.right;
  40.       var height = 500 - margin.top - margin.bottom;
  41.  
  42.       d3.csv("voter_turnout.csv", function(data) {
  43.         data.forEach(function(d) {
  44.           if (
  45.             d.state == "United States" ||
  46.             d.state == "United States (Excl. Louisiana)"
  47.           ) {
  48.             votes.push(d.votes);
  49.             eligibleVotes.push(d.eligible_voters);
  50.             years.push(d.year);
  51.             dataUSA.push(d);
  52.           }
  53.         });
  54.  
  55.         var x = d3.scale
  56.           .linear()
  57.           .domain([d3.min(years), d3.max(years)])
  58.           .range([0, width]);
  59.         var y = d3.scale
  60.           .linear()
  61.           .domain([0, d3.max(eligibleVotes)])
  62.           .range([height, 0]);
  63.         var xAxis = d3.svg
  64.           .axis()
  65.           .scale(x)
  66.           .orient("bottom")
  67.           .tickValues(years.reverse());
  68.         var yAxis = d3.svg
  69.           .axis()
  70.           .scale(y)
  71.           .orient("left")
  72.           .ticks(10);
  73.  
  74.         var svg = d3
  75.           .select("body")
  76.           .append("svg")
  77.           .attr("width", width + margin.left + margin.right)
  78.           .attr("height", height + margin.bottom + margin.top)
  79.           .append("g")
  80.           .attr(
  81.             "transform",
  82.             "translate(" + margin.left + "," + margin.top + ")"
  83.           );
  84.         svg
  85.           .append("g")
  86.           .attr("class", "x axis")
  87.           .attr("transform", "translate(0," + height + ")")
  88.           .call(xAxis)
  89.           .selectAll("text")
  90.           .style("text-anchor", "middle");
  91.         svg
  92.           .append("g")
  93.           .attr("class", "y axis")
  94.           .call(yAxis)
  95.           .append("text")
  96.           .attr("transform", "rotate(-90)")
  97.           .attr("y", 6);
  98.  
  99.         var valueline1 = d3.svg
  100.           .line()
  101.           .interpolate("linear")
  102.           .x(function(d) {
  103.             return x(d.year);
  104.           })
  105.           .y(function(d) {
  106.             return y(d.eligible_voters);
  107.           });
  108.  
  109.         var linechart = svg
  110.           .append("path")
  111.           .attr("class", "line")
  112.           .attr("d", valueline1(dataUSA))
  113.           .style("stroke", "blue")
  114.           .style("fill", "none");
  115.  
  116.         var div = d3
  117.           .select("body")
  118.           .append("div")
  119.           .attr("class", "tooltip")
  120.           .style("opacity", 0);
  121.  
  122.         svg
  123.           .selectAll("dot")
  124.           .data(dataUSA)
  125.           .enter()
  126.           .append("circle")
  127.           .attr("opacity", 0.9)
  128.           .attr("fill", "lightblue")
  129.           .attr("r", 4)
  130.           .attr("cx", function(d) {
  131.             return x(d.year);
  132.           })
  133.           .attr("cy", function(d) {
  134.             return y(d.eligible_voters);
  135.           })
  136.           .on("mouseover", function(d) {
  137.             div
  138.               .transition()
  139.               .duration(200)
  140.               .style("opacity", 0.8);
  141.             div
  142.               .html(
  143.                 "Year: " +
  144.                   d.year +
  145.                   "<br/>" +
  146.                   "Eligible voters: " +
  147.                   d.eligible_voters
  148.               )
  149.               .style("left", d3.event.pageX + "px")
  150.               .style("top", d3.event.pageY + 20 + "px");
  151.           })
  152.           .on("mouseout", function(d) {
  153.             div
  154.               .transition()
  155.               .duration(500)
  156.               .style("opacity", 0);
  157.           });
  158.  
  159.           var valueline2 = d3.svg
  160.           .line()
  161.           .interpolate("basis")
  162.           .x(function(d) {
  163.             return x(d.year);
  164.           })
  165.           .y(function(d) {
  166.             if(d.votes == "NA"){
  167.               return y(74825832);
  168.             } else {
  169.               return y(d.votes);
  170.             }
  171.           });
  172.           var linechart2 = svg
  173.           .append("path")
  174.           .attr("class", "line")
  175.           .attr("d", valueline2(dataUSA))
  176.           .style("stroke", "red")
  177.           .style("fill", "none");
  178.           svg
  179.           .selectAll("dot")
  180.           .data(dataUSA)
  181.           .enter()
  182.           .append("circle")
  183.           .attr("opacity", 0.4)
  184.           .attr("fill", "red")
  185.           .attr("r", 4)
  186.           .attr("cx", function(d) {
  187.             return x(d.year);
  188.           })
  189.           .attr("cy", function(d) {
  190.             if(d.votes == "NA"){
  191.               return y(74825832);
  192.             } else {
  193.               return y(d.votes);
  194.             }
  195.            
  196.           })
  197.           .on("mouseover", function(d) {
  198.             div
  199.               .transition()
  200.               .duration(200)
  201.               .style("opacity", 0.8);
  202.             div
  203.               .html(
  204.                 "Year: " +
  205.                   d.year +
  206.                   "<br/>" +
  207.                   "Voters: " +
  208.                   d.votes
  209.               )
  210.               .style("left", d3.event.pageX + "px")
  211.               .style("top", d3.event.pageY + 20 + "px");
  212.           })
  213.           .on("mouseout", function(d) {
  214.             div
  215.               .transition()
  216.               .duration(500)
  217.               .style("opacity", 0);
  218.           });
  219.       });
  220.     </script>
  221.   </body>
  222. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement