Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. var width = 300;
  2. var height = 300;
  3. var twoPi = 2 * Math.PI; // Full circle
  4. var formatPercent = d3.format(".0%");
  5.  
  6. var data = [{
  7. "range": "0-20",
  8. "count": 20
  9. }, {
  10. "range": "21-40",
  11. "count": 10
  12. }, {
  13. "range": "41-60",
  14. "count": 17
  15. }, {
  16. "range": "61-80",
  17. "count": 49
  18. }, {
  19. "range": "81-100",
  20. "count": 90
  21. }];
  22.  
  23. var max = d3.max(data, function(d) {
  24. return +d.count;
  25. });
  26.  
  27. var percent = d3.max(data, function(d) {
  28. return +d.count / 10;
  29. });
  30.  
  31. var radiusBackground = .25;
  32. var radiusForeground = .25;
  33. var gap = 28;
  34. var maxCount = max + percent;
  35.  
  36. var bounds = path.bounds(collection),
  37. topLeft = bounds[0],
  38. bottomRight = bounds[1];
  39.  
  40. var svg = d3.select(map.getPanes().overlayPane).append("svg")
  41. .attr("width", bottomRight[0] - topLeft[0])
  42. .attr("height", bottomRight[1] - topLeft[1]);
  43.  
  44. var g = svg.append("g")
  45. .attr("transform", "translate(" + width / 10 + "," + height / 10 + ")")
  46. .attr("class", "leaflet-zoom-hide");
  47.  
  48. var transform = d3.geo.transform({
  49. point: projectPoint
  50. });
  51. var d3path = d3.geo.path().projection(transform);
  52.  
  53. function projectPoint(x, y) {
  54. console.log(x,y);
  55. var point = map.latLngToLayerPoint(new L.LatLng(y, x));
  56. console.log(point);
  57. this.stream.point(point.x, point.y);
  58. }
  59.  
  60. g.selectAll(".twoPi")
  61. .data(data).enter()
  62. .append("path")
  63. .attr("class", "twoPi")
  64. .each(full);
  65.  
  66. g.selectAll(".outerPath")
  67. .data(data).enter()
  68. .append("path")
  69. .attr("class", "outerPath")
  70. .each(drawArc);
  71.  
  72. function full() {
  73.  
  74. var arc = d3.svg.arc()
  75. .startAngle(0)
  76. .endAngle(twoPi)
  77. .innerRadius(0 + gap * radiusBackground)
  78. .outerRadius(26 + gap * radiusBackground);
  79.  
  80. d3.select(this)
  81. .attr("transform", "translate(" + (width / 2.5) + "," + (height / 2.5) + ")")
  82. .attr("d", arc)
  83. .style("opacity", "0.5");
  84. radiusBackground++;
  85. }
  86.  
  87. function drawArc(d, i) {
  88.  
  89. var arc = d3.svg.arc()
  90. .startAngle(0)
  91. .endAngle(twoPi * (d.count / maxCount))
  92. .innerRadius(0 + gap * radiusForeground)
  93. .outerRadius(26 + gap * radiusForeground);
  94.  
  95. d3.select(this)
  96. .attr("transform", "translate(" + (width / 2.5) + "," + (height / 2.5) + ")")
  97. .attr("d", arc)
  98. .attr("id", "path" + i)
  99. .on("mouseover", function() {
  100. d3.select(this)
  101. .style("fill", "red")
  102. .style("cursor", "pointer");
  103. })
  104. .on("mouseout", function() {
  105. d3.select(this)
  106. .style("fill", "#8FAAE5");
  107. })
  108. radiusForeground++;
  109.  
  110. var text = g.append("text")
  111. .attr("x", 12 + radiusForeground)
  112. .attr("dy", 20)
  113. .style("pointer-events", "none");
  114.  
  115. text.append("textPath")
  116. .attr("fill", "white")
  117. .attr("xlink:href", "#path" + i)
  118. .text(d.count);
  119. }
  120.  
  121. L.mapbox.accessToken = 'ACCESS TOKEN HERE';
  122.  
  123. var map = L.mapbox.map('map', 'NAME HERE',{ zoomControl: false });
  124. var myLayer = L.mapbox.featureLayer().addTo(map);
  125.  
  126. var geojson = [
  127. {
  128. "type": "Feature",
  129. "geometry": {
  130. "type": "Point",
  131. "coordinates": [-2.9858672618865962,53.405470601865126]
  132. },
  133. "properties": {
  134. "icon": {
  135. "className": "sensor-icon",
  136. //"html": "▼",
  137. "iconSize": null
  138. }
  139. }
  140. },
  141. ];
  142.  
  143. myLayer.on('layeradd', function(e) {
  144. var marker = e.layer,
  145. feature = marker.feature;
  146. marker.setIcon(L.divIcon(feature.properties.icon));
  147. });
  148.  
  149. myLayer.setGeoJSON(geojson);
  150.  
  151. new L.Control.Zoom({ position: 'bottomleft' }).addTo(map);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement