title.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { ChartInternal } from './core'
  2. ChartInternal.prototype.initTitle = function() {
  3. var $$ = this
  4. $$.title = $$.svg
  5. .append('text')
  6. .text($$.config.title_text)
  7. .attr('class', $$.CLASS.title)
  8. }
  9. ChartInternal.prototype.redrawTitle = function() {
  10. var $$ = this
  11. $$.title.attr('x', $$.xForTitle.bind($$)).attr('y', $$.yForTitle.bind($$))
  12. }
  13. ChartInternal.prototype.xForTitle = function() {
  14. var $$ = this,
  15. config = $$.config,
  16. position = config.title_position || 'left',
  17. x
  18. if (position.indexOf('right') >= 0) {
  19. x =
  20. $$.currentWidth -
  21. $$.getTextRect(
  22. $$.title.node().textContent,
  23. $$.CLASS.title,
  24. $$.title.node()
  25. ).width -
  26. config.title_padding.right
  27. } else if (position.indexOf('center') >= 0) {
  28. x = Math.max(
  29. ($$.currentWidth -
  30. $$.getTextRect(
  31. $$.title.node().textContent,
  32. $$.CLASS.title,
  33. $$.title.node()
  34. ).width) /
  35. 2,
  36. 0
  37. )
  38. } else {
  39. // left
  40. x = config.title_padding.left
  41. }
  42. return x
  43. }
  44. ChartInternal.prototype.yForTitle = function() {
  45. var $$ = this
  46. return (
  47. $$.config.title_padding.top +
  48. $$.getTextRect($$.title.node().textContent, $$.CLASS.title, $$.title.node())
  49. .height
  50. )
  51. }
  52. ChartInternal.prototype.getTitlePadding = function() {
  53. var $$ = this
  54. return $$.yForTitle() + $$.config.title_padding.bottom
  55. }