Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. <meta charset="utf-8">
  2. <style>
  3.  
  4. .active {
  5. stroke: #000;
  6. stroke-width: 2px;
  7. }
  8.  
  9. </style>
  10. <svg width="100%" height="100%"></svg>
  11. <script src="https://d3js.org/d3.v4.js"></script>
  12. <script src="https://d3js.org/d3-scale-chromatic.v0.3.js"></script>
  13.  
  14. <script type="text/javascript">
  15.  
  16. var svg = d3.select("svg"),
  17. width = +svg.attr("width"),
  18. height = +svg.attr("height"),
  19. radius = 32;
  20.  
  21. var symbol = d3.symbol().type(d3.symbolTriangle)
  22. .size(function(d){
  23. return Math.random() * 10000;
  24. });
  25.  
  26. var data = d3.range(20).map(function(){ return Math.random() * 200; })
  27. .map(function(d, i){
  28. return {
  29. x: i * 50,
  30. y: d * 4
  31. };
  32. })
  33.  
  34. var color = d3.scaleOrdinal()
  35. .range(d3.schemeCategory20);
  36.  
  37. var path = svg
  38. .selectAll('path')
  39. .data(data)
  40. .enter()
  41. .append('path')
  42. .attr('d', symbol)
  43. .attr('stroke', 'black')
  44. .attr('fill',function(d, i) { return color(i); })
  45. .attr('transform', function(d){ return 'translate(' + d.x +','+ d.y +')'; })
  46. .call(d3.drag()
  47. .on("start", dragstarted)
  48. .on("drag", dragged)
  49. .on("end", dragended))
  50. .on("dblclick",removeAndAdd);
  51.  
  52. function removeAndAdd(){
  53. //d3.select(this).remove();
  54.  
  55. svg.attr('transform', function(d){
  56. console.log(this.y);
  57. var x = d.x + Math.random()*100;
  58. var y = d.y + Math.random()*100;
  59.  
  60. return 'translate(' + x +','+ y +')'; })
  61. .call(d3.drag()
  62. .on("start", dragstarted)
  63. .on("drag", dragged)
  64. .on("end", dragended))
  65. .on("dblclick",removeAndAdd);
  66.  
  67. }
  68.  
  69. function dragstarted(d) {
  70. d3.select(this).raise().classed("active", true);
  71. }
  72.  
  73. function dragged(d) {
  74. d3.select(this).attr('transform', function(d){ return 'translate(' + d3.event.x +','+ d3.event.y +')'; });
  75. }
  76.  
  77. function dragended(d) {
  78. d3.select(this).classed("active", false);
  79. }
  80.  
  81. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement