Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title>Final Map</title>
  6. <script src="//d3js.org/d3.v3.min.js"></script>
  7. <script src="//d3js.org/topojson.v1.min.js"></script>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. .boundary {
  14. fill: none;
  15. stroke: #dfdfdf;
  16. stroke-linejoin: round;
  17. }
  18. #map {
  19. text-align: center;
  20. }
  21. circle {
  22. fill: blue;
  23. opacity:.9;
  24. }
  25. text{
  26. font-family: 'PT Sans', sans-serif;
  27. font-weight: 300;
  28. font-size: 12px;
  29. z-index: 900;
  30. }
  31. #tooltip {
  32. position: absolute;
  33. width: 200px;
  34. height: auto;
  35. padding: 10px;
  36. background-color: #ff9436;
  37. -webkit-border-radius: 10px;
  38. -moz-border-radius: 10px;
  39. border-radius: 10px;
  40. -webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
  41. -moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
  42. box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
  43. pointer-events: none;
  44. }
  45. #tooltip.hidden {
  46. display: none;
  47. }
  48. #tooltip p {
  49. margin: 0;
  50. font-family: sans-serif;
  51. font-size: 16px;
  52. line-height: 20px;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <div id="tooltip" >
  58. <p><strong>The store name is:</strong></p>
  59. <p><span id="value"></span></p>
  60. </div>
  61. <div id="map"></div>
  62. <script>
  63. var height = 600;
  64. var width = 900, centered;
  65.  
  66. var projection = d3.geo.albersUsa()
  67. .scale(1200)
  68. .translate([width/2, height/2]);
  69. var path = d3.geo.path().projection(projection);
  70.  
  71. var svg = d3.select("#map")
  72. .append("svg")
  73. .attr("width", width)
  74. .attr("height", height);
  75.  
  76. var g = svg.append("g");
  77.  
  78. d3.json('us.json', function(us) {
  79. console.log("usa", us);
  80.  
  81. var map = svg.append('g').attr('class', 'boundary');
  82. usa = map.selectAll('path')
  83. .data(topojson.feature(us, us.objects.states).features);
  84.  
  85. usa.enter()
  86. .append('path')
  87. .attr('d', path)
  88. .attr('fill', 'gray');
  89.  
  90.  
  91. d3.json('newstorelocations.json', function (locations){
  92. console.log('stores', locations);
  93.  
  94. svg.selectAll('circle')
  95. .data(locations)
  96. .enter()
  97. .append('circle')
  98. .attr('cx', function(d) {return projection([d.lon, d.lat])[0]})
  99. .attr('cy', function(d) {return projection([d.lon, d.lat])[1]})
  100. .attr('r', 4)
  101. .on("mouseover", function(d) {
  102. console.log("binish", d);
  103.  
  104. var xPosition = (d3.select(this).attr('x', function(d) {return projection([d.lon, d.lat])[0]}));
  105. var yPosition = (d3.select(this).attr('y', function(d) {return projection([d.lon, d.lat])[1]}));
  106.  
  107. d3.select(this)
  108. .style('fill','red')
  109.  
  110. d3.select("#tooltip")
  111. .style("left", xPosition + "px")
  112. .style("top", yPosition + "px")
  113. .select("#value")
  114. .text(d.name);
  115.  
  116. d3.select("#tooltip").classed("hidden", false);
  117.  
  118. })
  119. .on("mouseout", function() {
  120.  
  121. //Hide the tooltip
  122. d3.select(this)
  123. .style('fill','blue')
  124. d3.select("#tooltip").classed("hidden", true);
  125.  
  126.  
  127. });
  128. });
  129. });
  130. </script>
  131. </body>
  132. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement