Guest User

Untitled

a guest
Jan 16th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. <html>
  2. <head>
  3. <style>
  4. .chart rect {
  5. fill: steelblue;
  6. }
  7.  
  8. .chart rect:hover {
  9. fill: turquoise;
  10. }
  11.  
  12. .chart .rectM {
  13. stroke: green;
  14. stroke-width: 1;
  15. fill: green;
  16. fill-opacity: .2;
  17. }
  18.  
  19. .chart .rectM:hover {
  20. fill: green;
  21. fill-opacity: .5;
  22. }
  23.  
  24. .chart text {
  25. font: 10px sans-serif;
  26. }
  27.  
  28. .chart .title {
  29. font: 15px sans-serif;
  30. }
  31.  
  32. .axis path,
  33. .axis line {
  34. fill: none;
  35. stroke: #000;
  36. shape-rendering: crispEdges;
  37. }
  38. </style>
  39.  
  40. <script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
  41. </head>
  42. <body>
  43. <div class="chart"></div>
  44. <script>
  45. var height = 400;
  46. var width = 600;
  47. var margin = 40;
  48. var data =[];
  49. for(var i = 0; i < 42; i++) {
  50. data.push({
  51. x: Math.random() * 400,
  52. y: Math.random() * 100,
  53. c: Math.round(Math.random() * 5),
  54. size: Math.random() * 200,
  55. });
  56. }
  57.  
  58. var labelX = 'X';
  59. var labelY = 'Y';
  60. var svg = d3.select('.chart')
  61. .append('svg')
  62. .attr('class', 'chart')
  63. .attr("width", width + margin + margin)
  64. .attr("height", height + margin + margin)
  65. .append("g")
  66. .attr("transform", "translate(" + margin + "," + margin + ")");
  67.  
  68. var x = d3.scale.linear()
  69. .domain([d3.min(data, function (d) { return d.x; }), d3.max(data, function (d) { return d.x; })])
  70. .range([0, width]);
  71.  
  72. var y = d3.scale.linear()
  73. .domain([d3.min(data, function (d) { return d.y; }), d3.max(data, function (d) { return d.y; })])
  74. .range([height, 0]);
  75.  
  76. var scale = d3.scale.sqrt()
  77. .domain([d3.min(data, function (d) { return d.size; }), d3.max(data, function (d) { return d.size; })])
  78. .range([1, 20]);
  79.  
  80. var opacity = d3.scale.sqrt()
  81. .domain([d3.min(data, function (d) { return d.size; }), d3.max(data, function (d) { return d.size; })])
  82. .range([1, .5]);
  83.  
  84. var color = d3.scale.category10();
  85.  
  86. var xAxis = d3.svg.axis().scale(x);
  87. var yAxis = d3.svg.axis().scale(y).orient("left");
  88.  
  89. svg.append("g")
  90. .attr("class", "y axis")
  91. .call(yAxis)
  92. .append("text")
  93. .attr("transform", "rotate(-90)")
  94. .attr("x", 20)
  95. .attr("y", -margin)
  96. .attr("dy", ".71em")
  97. .style("text-anchor", "end")
  98. .text(labelY);
  99. // x axis and label
  100. svg.append("g")
  101. .attr("class", "x axis")
  102. .attr("transform", "translate(0," + height + ")")
  103. .call(xAxis)
  104. .append("text")
  105. .attr("x", width + 20)
  106. .attr("y", margin - 10)
  107. .attr("dy", ".71em")
  108. .style("text-anchor", "end")
  109. .text(labelX);
  110.  
  111. svg.selectAll("circle")
  112. .data(data)
  113. .enter()
  114. .insert("circle")
  115. .attr("cx", width / 2)
  116. .attr("cy", height / 2)
  117. .attr("opacity", function (d) { return opacity(d.size); })
  118. .attr("r", function (d) { return scale(d.size); })
  119. .style("fill", function (d) { return color(d.c); })
  120. .on('mouseover', function (d, i) {
  121. fade(d.c, .1);
  122. })
  123. .on('mouseout', function (d, i) {
  124. fadeOut();
  125. })
  126. .transition()
  127. .delay(function (d, i) { return x(d.x) - y(d.y); })
  128. .duration(500)
  129. .attr("cx", function (d) { return x(d.x); })
  130. .attr("cy", function (d) { return y(d.y); })
  131. .ease("bounce");
  132.  
  133.  
  134. function fade(c, opacity) {
  135. svg.selectAll("circle")
  136. .filter(function (d) {
  137. return d.c != c;
  138. })
  139. .transition()
  140. .style("opacity", opacity);
  141. }
  142.  
  143. function fadeOut() {
  144. svg.selectAll("circle")
  145. .transition()
  146. .style("opacity", function (d) { opacity(d.size); });
  147. }
  148. </script>
  149. </body>
  150. </html>
Add Comment
Please, Sign In to add comment