title.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. var echarts = require("../echarts");
  2. var graphic = require("../util/graphic");
  3. var _layout = require("../util/layout");
  4. var getLayoutRect = _layout.getLayoutRect;
  5. // Model
  6. echarts.extendComponentModel({
  7. type: 'title',
  8. layoutMode: {
  9. type: 'box',
  10. ignoreSize: true
  11. },
  12. defaultOption: {
  13. // 一级层叠
  14. zlevel: 0,
  15. // 二级层叠
  16. z: 6,
  17. show: true,
  18. text: '',
  19. // 超链接跳转
  20. // link: null,
  21. // 仅支持self | blank
  22. target: 'blank',
  23. subtext: '',
  24. // 超链接跳转
  25. // sublink: null,
  26. // 仅支持self | blank
  27. subtarget: 'blank',
  28. // 'center' ¦ 'left' ¦ 'right'
  29. // ¦ {number}(x坐标,单位px)
  30. left: 0,
  31. // 'top' ¦ 'bottom' ¦ 'center'
  32. // ¦ {number}(y坐标,单位px)
  33. top: 0,
  34. // 水平对齐
  35. // 'auto' | 'left' | 'right' | 'center'
  36. // 默认根据 left 的位置判断是左对齐还是右对齐
  37. // textAlign: null
  38. //
  39. // 垂直对齐
  40. // 'auto' | 'top' | 'bottom' | 'middle'
  41. // 默认根据 top 位置判断是上对齐还是下对齐
  42. // textBaseline: null
  43. backgroundColor: 'rgba(0,0,0,0)',
  44. // 标题边框颜色
  45. borderColor: '#ccc',
  46. // 标题边框线宽,单位px,默认为0(无边框)
  47. borderWidth: 0,
  48. // 标题内边距,单位px,默认各方向内边距为5,
  49. // 接受数组分别设定上右下左边距,同css
  50. padding: 5,
  51. // 主副标题纵向间隔,单位px,默认为10,
  52. itemGap: 10,
  53. textStyle: {
  54. fontSize: 18,
  55. fontWeight: 'bolder',
  56. color: '#333'
  57. },
  58. subtextStyle: {
  59. color: '#aaa'
  60. }
  61. }
  62. }); // View
  63. echarts.extendComponentView({
  64. type: 'title',
  65. render: function (titleModel, ecModel, api) {
  66. this.group.removeAll();
  67. if (!titleModel.get('show')) {
  68. return;
  69. }
  70. var group = this.group;
  71. var textStyleModel = titleModel.getModel('textStyle');
  72. var subtextStyleModel = titleModel.getModel('subtextStyle');
  73. var textAlign = titleModel.get('textAlign');
  74. var textBaseline = titleModel.get('textBaseline');
  75. var textEl = new graphic.Text({
  76. style: graphic.setTextStyle({}, textStyleModel, {
  77. text: titleModel.get('text'),
  78. textFill: textStyleModel.getTextColor()
  79. }, {
  80. disableBox: true
  81. }),
  82. z2: 10
  83. });
  84. var textRect = textEl.getBoundingRect();
  85. var subText = titleModel.get('subtext');
  86. var subTextEl = new graphic.Text({
  87. style: graphic.setTextStyle({}, subtextStyleModel, {
  88. text: subText,
  89. textFill: subtextStyleModel.getTextColor(),
  90. y: textRect.height + titleModel.get('itemGap'),
  91. textVerticalAlign: 'top'
  92. }, {
  93. disableBox: true
  94. }),
  95. z2: 10
  96. });
  97. var link = titleModel.get('link');
  98. var sublink = titleModel.get('sublink');
  99. textEl.silent = !link;
  100. subTextEl.silent = !sublink;
  101. if (link) {
  102. textEl.on('click', function () {
  103. window.open(link, '_' + titleModel.get('target'));
  104. });
  105. }
  106. if (sublink) {
  107. subTextEl.on('click', function () {
  108. window.open(sublink, '_' + titleModel.get('subtarget'));
  109. });
  110. }
  111. group.add(textEl);
  112. subText && group.add(subTextEl); // If no subText, but add subTextEl, there will be an empty line.
  113. var groupRect = group.getBoundingRect();
  114. var layoutOption = titleModel.getBoxLayoutParams();
  115. layoutOption.width = groupRect.width;
  116. layoutOption.height = groupRect.height;
  117. var layoutRect = getLayoutRect(layoutOption, {
  118. width: api.getWidth(),
  119. height: api.getHeight()
  120. }, titleModel.get('padding')); // Adjust text align based on position
  121. if (!textAlign) {
  122. // Align left if title is on the left. center and right is same
  123. textAlign = titleModel.get('left') || titleModel.get('right');
  124. if (textAlign === 'middle') {
  125. textAlign = 'center';
  126. } // Adjust layout by text align
  127. if (textAlign === 'right') {
  128. layoutRect.x += layoutRect.width;
  129. } else if (textAlign === 'center') {
  130. layoutRect.x += layoutRect.width / 2;
  131. }
  132. }
  133. if (!textBaseline) {
  134. textBaseline = titleModel.get('top') || titleModel.get('bottom');
  135. if (textBaseline === 'center') {
  136. textBaseline = 'middle';
  137. }
  138. if (textBaseline === 'bottom') {
  139. layoutRect.y += layoutRect.height;
  140. } else if (textBaseline === 'middle') {
  141. layoutRect.y += layoutRect.height / 2;
  142. }
  143. textBaseline = textBaseline || 'top';
  144. }
  145. group.attr('position', [layoutRect.x, layoutRect.y]);
  146. var alignStyle = {
  147. textAlign: textAlign,
  148. textVerticalAlign: textBaseline
  149. };
  150. textEl.setStyle(alignStyle);
  151. subTextEl.setStyle(alignStyle); // Render background
  152. // Get groupRect again because textAlign has been changed
  153. groupRect = group.getBoundingRect();
  154. var padding = layoutRect.margin;
  155. var style = titleModel.getItemStyle(['color', 'opacity']);
  156. style.fill = titleModel.get('backgroundColor');
  157. var rect = new graphic.Rect({
  158. shape: {
  159. x: groupRect.x - padding[3],
  160. y: groupRect.y - padding[0],
  161. width: groupRect.width + padding[1] + padding[3],
  162. height: groupRect.height + padding[0] + padding[2],
  163. r: titleModel.get('borderRadius')
  164. },
  165. style: style,
  166. silent: true
  167. });
  168. graphic.subPixelOptimizeRect(rect);
  169. group.add(rect);
  170. }
  171. });