Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 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="map"></div>
  58. <div id="tooltip" class="hidden">
  59. <p><strong>The store name is:</strong></p>
  60. <p><span id="value"></span></p>
  61. </div>
  62.  
  63. <script>
  64. var height = 600;
  65. var width = 900, centered;
  66.  
  67. var projection = d3.geo.albersUsa()
  68. .scale(1200)
  69. .translate([width/2, height/2]);
  70. var path = d3.geo.path().projection(projection);
  71.  
  72. var svg = d3.select("#map")
  73. .append("svg")
  74. .attr("width", width)
  75. .attr("height", height);
  76.  
  77. var g = svg.append("g");
  78.  
  79. d3.json('us.json', function(us) {
  80. console.log("usa", us);
  81.  
  82. var map = svg.append('g').attr('class', 'boundary');
  83. usa = map.selectAll('path')
  84. .data(topojson.feature(us, us.objects.states).features);
  85.  
  86. usa.enter()
  87. .append('path')
  88. .attr('d', path)
  89. .attr('fill', 'gray');
  90.  
  91.  
  92. d3.json('newstorelocations.json', function (locations){
  93. console.log('stores', locations);
  94.  
  95. svg.selectAll('circle')
  96. .data(locations)
  97. .enter()
  98. .append('circle')
  99. .attr('cx', function(d) {return projection([d.lon, d.lat])[0]})
  100. .attr('cy', function(d) {return projection([d.lon, d.lat])[1]})
  101. .attr('r', 4)
  102. .on("mouseover", function(d) {
  103. console.log("binish", d);
  104.  
  105. var xPosition = (d3.select(this).attr('x', function(d) {return projection([d.lon, d.lat])[0]}));
  106. var yPosition = (d3.select(this).attr('y', function(d) {return projection([d.lon, d.lat])[1]}));
  107.  
  108. d3.select("#tooltip")
  109. .style("left", xPosition + "px")
  110. .style("top", yPosition + "px")
  111. .select("#value")
  112. .text(function(d) {return d.name});
  113.  
  114. d3.select("#tooltip").classed("hidden", false);
  115.  
  116. })
  117. .on("mouseout", function() {
  118.  
  119. //Hide the tooltip
  120. d3.select("#tooltip").classed("hidden", true);
  121.  
  122. });
  123. });
  124. });
  125. </script>
  126. </body>
  127. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement