add-label.js 959 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import addTextLabel from './add-text-label'
  2. import addHtmlLabel from './add-html-label'
  3. import addSVGLabel from './add-svg-label'
  4. function addLabel (root, node, location) {
  5. const label = node.label
  6. const labelSvg = root.append('g')
  7. // Allow the label to be a string, a function that returns a DOM element, or
  8. // a DOM element itself.
  9. if (node.labelType === 'svg') {
  10. addSVGLabel(labelSvg, node)
  11. } else if (typeof label !== 'string' || node.labelType === 'html') {
  12. addHtmlLabel(labelSvg, node)
  13. } else {
  14. addTextLabel(labelSvg, node)
  15. }
  16. const labelBBox = labelSvg.node().getBBox()
  17. let y
  18. switch (location) {
  19. case 'top':
  20. y = (-node.height / 2)
  21. break
  22. case 'bottom':
  23. y = (node.height / 2) - labelBBox.height
  24. break
  25. default:
  26. y = (-labelBBox.height / 2)
  27. }
  28. labelSvg.attr('transform',
  29. 'translate(' + (-labelBBox.width / 2) + ',' + y + ')')
  30. return labelSvg
  31. }
  32. export default addLabel