Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function map(){
  2.  
  3.     var zoom = d3.behavior.zoom()
  4.         .scaleExtent([1, 8])
  5.         .on("zoom", move);
  6.  
  7.     var mapDiv = $("#map");
  8.  
  9.     var margin = {top: 20, right: 20, bottom: 20, left: 20},
  10.         width = mapDiv.width() - margin.right - margin.left,
  11.         height = mapDiv.height() - margin.top - margin.bottom;
  12.  
  13.     //initialize color scale
  14.     //...
  15.     //var c20 = d3.scale.category20c();
  16.    
  17.     //initialize tooltip
  18.     //...
  19.  
  20.     var projection = d3.geo.mercator()
  21.         .center([8.25, 56.8 ])
  22.         .scale(700);
  23.  
  24.     var svg = d3.select("#map").append("svg")
  25.         .attr("width", width)
  26.         .attr("height", height)
  27.         .call(zoom);
  28.  
  29.     var path = d3.geo.path()
  30.         .projection(projection);
  31.  
  32.     g = svg.append("g");
  33.    
  34.     var tip = d3.tip()
  35.       .attr('class', 'd3-tip')
  36.       .offset([-10, 0])
  37.       .html(function(d) {
  38.           //console.log("här");
  39.         return "<strong>Country:</strong> <span style='color:red'>" + d.country + "</span>";
  40.       })
  41.  
  42.  
  43.     // load data and draw the map
  44.     d3.json("data/world-topo.json", function(error, world) {
  45.         var countries = topojson.feature(world, world.objects.countries).features;
  46.  
  47.         //load summary data
  48.         //...
  49.         d3.csv("data/Earthquakes_Europe-1901-1983.csv", function(data) {
  50.  
  51.             self.data = data;
  52.        
  53.        
  54.        
  55.         draw(countries, self.data);
  56.        
  57.         });
  58.        
  59.     });
  60.    
  61.  
  62.     svg.call(tip);
  63.     function draw(countries, data)
  64.     {
  65.        
  66.         var country = g.selectAll(".country").data(countries);      //G = grafiskt element som byggs på (?)
  67.         country.enter().insert("path")
  68.             .attr("class", "country")
  69.             .attr("d", path)
  70.             .attr("id", function(d) { return d.id; })
  71.             .attr("title", function(d) { return d.properties.name; })
  72.  
  73.             .style('stroke-width', 1)
  74.             .style("fill",  function(d) { return d3.rgb(128, 152, 230);})
  75.             .style("stroke", "white")
  76.             //tooltip
  77.             .on("mousemove", function(d) {
  78.                d3.select(this)
  79.                .style("fill",  function(d) { return d3.rgb(61, 97, 215);})
  80.             })
  81.             .on("mouseout",  function(d) {
  82.                 d3.select(this)
  83.                .style("fill",  function(d) { return d3.rgb(128, 152, 230);})
  84.             })
  85.             //selection
  86.             .on("click",  function(d) {
  87.                 //...
  88.                 //selFeature(d);
  89.             }
  90.            
  91.             );
  92.            
  93.             data.forEach(function (point) {
  94.                 d3.select("#nRadius").on("input", function() {
  95.                     update(+this.value, point);
  96.                 });
  97.  
  98.                 update(8, point);
  99.             });
  100.     }
  101.    
  102.     function addpoint(point) {
  103.        
  104.    
  105.  
  106.     }
  107.  
  108.     function update(min_intensity, point) {
  109.  
  110.         var gpoint = g.append("g").attr("class", "gpoint");
  111.  
  112.         var x = projection([point.x, point.y])[0];
  113.  
  114.         var y = projection([point.x, point.y])[1];
  115.  
  116.       // adjust the text on the range slider
  117.         d3.select("#nRadius-value").text(min_intensity);
  118.         d3.select("#nRadius").property("value", min_intensity);
  119.  
  120.         //console.log(point)
  121.  
  122.         if(point.intensity >= min_intensity){
  123.             gpoint.append("svg:circle")
  124.                 .attr("cx", x)
  125.                 .attr("cy", y)
  126.                 .attr("class", "point")
  127.                 .style("fill", d3.rgb(150,0,0))
  128.                 .attr("r", point.intensity * 0.2)
  129.                 .style("opacity", point.intensity * 0.1)
  130.                 //.append("svg:title").text(function(d) {return "Country: " + d.country;})
  131.                 //bootstrap columns
  132.                 .on("mousemove", function(d) {
  133.                    d3.select(this)
  134.                     .style("stroke",  function(d) { return d3.rgb(255, 255, 0);})
  135.                     .style("stroke-width",  function(d) { return 0.5});
  136.                 })
  137.                 .on("mouseout",  function(d) {
  138.                     d3.select(this)
  139.                    .style("stroke-width",  function(d) { return 0});
  140.                 })
  141.             //selection
  142.                 .on("click",  function(d, i) {
  143.                     //...
  144.                     d3.selectAll(".point").style("fill",  function(d) { return d3.rgb(150, 0, 0);})
  145.                         .style("opacity", 0.3);
  146.                    
  147.                     d3.select(this)
  148.                     .style("fill",  function(d) { return d3.rgb(255,255, 0);})
  149.                     .style("opacity", 1.0);
  150.  
  151.                     selFeature(point);
  152.                 })
  153.         }
  154.  
  155.     }
  156.    
  157.     //zoom and panning method
  158.     function move() {
  159.  
  160.         var t = d3.event.translate;
  161.         var s = d3.event.scale;
  162.        
  163.         zoom.translate(t);
  164.         g.style("stroke-width", 1 / s).attr("transform", "translate(" + t + ")scale(" + s + ")");
  165.        
  166.     }
  167.    
  168.     //method for selecting features of other components
  169.     function selFeature(value){
  170.         //...
  171.         //sp1.selectDot(value.properties["name"]);
  172.         //pc1.selectLine(value);
  173.         d3.select("#sp").selectAll('.dot').style("stroke", function(d){
  174.              return d["country"]==value? "red" : 'black' ;
  175.          })
  176.          //console.log(value)
  177.          elem = document.getElementById('info');
  178.             elem.innerHTML = "Place: " + value["place"] + "<br> Country: " + value["country"] + "<br> Magnitude: "
  179.              + value["magnitude"] + "<br> Intensity: " + value["intensity"];
  180.  
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement