Guest User

Untitled

a guest
Dec 9th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. var json0 = tributary["geojson0"];
  2. var json1 = tributary["geojson1"];
  3.  
  4. var tooltip, shapes;
  5.  
  6. var categories = _.uniq(_.map(json0.features, function(d) { return d.properties.category; }));
  7. var colorScale = d3.scale.ordinal().range(['#5b88a5', '#87af20', '#ffc240', "#DD8000", "#E06A87"]);
  8.  
  9. var color = function(d, i) {
  10. return colorScale(d.properties.category);
  11. }
  12.  
  13. var xy = d3.geo.albers()
  14. .scale(1200)
  15. .translate([437, 355]);
  16.  
  17. var path = d3.geo.path()
  18. .projection(xy);
  19.  
  20.  
  21. var categoryLabels = {
  22. 'none': 'Unclaimed',
  23. 'other_country': 'Other Countries',
  24. 'disputed': 'Disputed Areas',
  25. 'territory': 'US Territories',
  26. 'state': 'US State'
  27. };
  28.  
  29. // =============
  30. // FUNCTIONS
  31.  
  32. var showTooltip = function(d, i) {
  33. var text = d.properties.label;
  34. if (d.properties.country) {
  35. text += ' (' + d.properties.country + ')';
  36. }
  37.  
  38. // show tooltip
  39. tooltip.text(text)
  40. .style('visibility', null);
  41.  
  42. d3.select(this).style('fill', "#EBDFCD");
  43.  
  44. moveTooltip(d, i);
  45. };
  46.  
  47. var moveTooltip = function(d, i) {
  48. // move tooltip
  49. var m = d3.svg.mouse(this);
  50. tooltip.attr('transform', 'translate(' + m + ')');
  51. };
  52.  
  53. var drawMap = function(js) {
  54. console.log(path, color);
  55.  
  56. shapes = g.selectAll('path.shapes')
  57. .data(js, function(d) { console.log(d);return d.id } )
  58. .transition()
  59. .duration(1000)
  60. .attr('d', path);
  61.  
  62. shapes.enter()
  63. .append('path')
  64. .attr('class', 'shape')
  65. .attr('d', path)
  66. .style('fill', color)
  67. .style('stroke', "#FFFFFF")
  68. .on('mouseover', showTooltip)
  69. .on('mousemove', moveTooltip)
  70. .on('mouseout', function(d, i) {
  71. tooltip.style('visibility', 'hidden');
  72. d3.select(this).style('fill', color)
  73. })
  74. .on('click', function() {
  75. drawMap(json1.features);
  76. });
  77.  
  78. };
  79.  
  80.  
  81. // ========= LEGEND
  82. var yscale = d3.scale.ordinal()
  83. .domain(d3.range(0, categories.length))
  84. .rangeBands([87, 277]);
  85.  
  86. var legend = g.selectAll('g.legenditem')
  87. .data(categories)
  88. .enter().append('g')
  89. .attr('class', 'legenditem')
  90. .attr('transform', function(d, i) {
  91. return 'translate(' + [930, yscale(i)] + ')';
  92. })
  93. .attr('width', 300)
  94. .attr('height', yscale.rangeBand());
  95.  
  96.  
  97. legend.append('text')
  98. .text(function(d, i) {
  99. return categoryLabels[d];
  100. })
  101. .attr('transform', 'translate(' + [26, 15] + ')')
  102.  
  103.  
  104. legend.append('rect')
  105. .attr('width', 20)
  106. .attr('height', 20)
  107. .style('fill', colorScale)
  108.  
  109.  
  110. tooltip = g.append('text');
  111.  
  112.  
  113. drawMap(json0.features);
Add Comment
Please, Sign In to add comment