Guest User

Untitled

a guest
Nov 18th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import * as d3 from 'd3'
  2. global.d3 = d3
  3.  
  4. const data = [
  5. {"name": "Eve", "parent": ""},
  6. {"name": "Cain", "parent": "Eve"},
  7. {"name": "Seth", "parent": "Eve"},
  8. {"name": "Enos", "parent": "Seth"},
  9. {"name": "Noam", "parent": "Seth"},
  10. {"name": "Abel", "parent": "Eve"},
  11. {"name": "Awan", "parent": "Eve"},
  12. {"name": "Enoch", "parent": "Awan"},
  13. {"name": "Azura", "parent": "Eve"}
  14. ]
  15.  
  16. const width = 600
  17. const height = 600
  18.  
  19. const root = d3.stratify().id(d => d.name).parentId(d => d.parent)(data)
  20. const tree = d3.tree().size([width, height-150])
  21. .separation((a, b) => (a.parent == b.parent ? 1 : 2) / a.depth)
  22.  
  23. global.root = root
  24. global.tree = tree(root)
  25.  
  26. const container = d3.select('#chart')
  27. .append('svg')
  28. .attr('width', width)
  29. .attr('height', height)
  30. .append('g')
  31. .attr("transform", "translate(40, 0)")
  32.  
  33. const link = d3.linkHorizontal()
  34. .x(d => d.y)
  35. .y(d => d.x)
  36.  
  37. container.selectAll('path')
  38. .data(root.links())
  39. .enter()
  40. .append('path')
  41. .attr('d', link)
  42. .attr('fill', 'none')
  43. .attr('stroke', 'brown')
  44. .attr('stroke-width', 2)
  45.  
  46. const node = container.selectAll('g')
  47. .data(root.descendants())
  48. .enter()
  49. .append('g')
  50. .attr('transform', d => 'translate(' + d.y + ',' + d.x + ')')
  51.  
  52. const circle = node.append('circle')
  53. .attr('r', 4.5)
  54. .attr('stroke', 'black')
  55. .attr('stroke-width', '1')
  56. .attr('fill', 'yellow')
  57.  
  58. node.append('text')
  59. .text(n => n.data.name)
Add Comment
Please, Sign In to add comment