title.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. var echarts = require("../echarts");
  21. var graphic = require("../util/graphic");
  22. var _layout = require("../util/layout");
  23. var getLayoutRect = _layout.getLayoutRect;
  24. /*
  25. * Licensed to the Apache Software Foundation (ASF) under one
  26. * or more contributor license agreements. See the NOTICE file
  27. * distributed with this work for additional information
  28. * regarding copyright ownership. The ASF licenses this file
  29. * to you under the Apache License, Version 2.0 (the
  30. * "License"); you may not use this file except in compliance
  31. * with the License. You may obtain a copy of the License at
  32. *
  33. * http://www.apache.org/licenses/LICENSE-2.0
  34. *
  35. * Unless required by applicable law or agreed to in writing,
  36. * software distributed under the License is distributed on an
  37. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  38. * KIND, either express or implied. See the License for the
  39. * specific language governing permissions and limitations
  40. * under the License.
  41. */
  42. // Model
  43. echarts.extendComponentModel({
  44. type: 'title',
  45. layoutMode: {
  46. type: 'box',
  47. ignoreSize: true
  48. },
  49. defaultOption: {
  50. // 一级层叠
  51. zlevel: 0,
  52. // 二级层叠
  53. z: 6,
  54. show: true,
  55. text: '',
  56. // 超链接跳转
  57. // link: null,
  58. // 仅支持self | blank
  59. target: 'blank',
  60. subtext: '',
  61. // 超链接跳转
  62. // sublink: null,
  63. // 仅支持self | blank
  64. subtarget: 'blank',
  65. // 'center' ¦ 'left' ¦ 'right'
  66. // ¦ {number}(x坐标,单位px)
  67. left: 0,
  68. // 'top' ¦ 'bottom' ¦ 'center'
  69. // ¦ {number}(y坐标,单位px)
  70. top: 0,
  71. // 水平对齐
  72. // 'auto' | 'left' | 'right' | 'center'
  73. // 默认根据 left 的位置判断是左对齐还是右对齐
  74. // textAlign: null
  75. //
  76. // 垂直对齐
  77. // 'auto' | 'top' | 'bottom' | 'middle'
  78. // 默认根据 top 位置判断是上对齐还是下对齐
  79. // textVerticalAlign: null
  80. // textBaseline: null // The same as textVerticalAlign.
  81. backgroundColor: 'rgba(0,0,0,0)',
  82. // 标题边框颜色
  83. borderColor: '#ccc',
  84. // 标题边框线宽,单位px,默认为0(无边框)
  85. borderWidth: 0,
  86. // 标题内边距,单位px,默认各方向内边距为5,
  87. // 接受数组分别设定上右下左边距,同css
  88. padding: 5,
  89. // 主副标题纵向间隔,单位px,默认为10,
  90. itemGap: 10,
  91. textStyle: {
  92. fontSize: 18,
  93. fontWeight: 'bolder',
  94. color: '#333'
  95. },
  96. subtextStyle: {
  97. color: '#aaa'
  98. }
  99. }
  100. }); // View
  101. echarts.extendComponentView({
  102. type: 'title',
  103. render: function (titleModel, ecModel, api) {
  104. this.group.removeAll();
  105. if (!titleModel.get('show')) {
  106. return;
  107. }
  108. var group = this.group;
  109. var textStyleModel = titleModel.getModel('textStyle');
  110. var subtextStyleModel = titleModel.getModel('subtextStyle');
  111. var textAlign = titleModel.get('textAlign');
  112. var textVerticalAlign = zrUtil.retrieve2(titleModel.get('textBaseline'), titleModel.get('textVerticalAlign'));
  113. var textEl = new graphic.Text({
  114. style: graphic.setTextStyle({}, textStyleModel, {
  115. text: titleModel.get('text'),
  116. textFill: textStyleModel.getTextColor()
  117. }, {
  118. disableBox: true
  119. }),
  120. z2: 10
  121. });
  122. var textRect = textEl.getBoundingRect();
  123. var subText = titleModel.get('subtext');
  124. var subTextEl = new graphic.Text({
  125. style: graphic.setTextStyle({}, subtextStyleModel, {
  126. text: subText,
  127. textFill: subtextStyleModel.getTextColor(),
  128. y: textRect.height + titleModel.get('itemGap'),
  129. textVerticalAlign: 'top'
  130. }, {
  131. disableBox: true
  132. }),
  133. z2: 10
  134. });
  135. var link = titleModel.get('link');
  136. var sublink = titleModel.get('sublink');
  137. var triggerEvent = titleModel.get('triggerEvent', true);
  138. textEl.silent = !link && !triggerEvent;
  139. subTextEl.silent = !sublink && !triggerEvent;
  140. if (link) {
  141. textEl.on('click', function () {
  142. window.open(link, '_' + titleModel.get('target'));
  143. });
  144. }
  145. if (sublink) {
  146. subTextEl.on('click', function () {
  147. window.open(sublink, '_' + titleModel.get('subtarget'));
  148. });
  149. }
  150. textEl.eventData = subTextEl.eventData = triggerEvent ? {
  151. componentType: 'title',
  152. componentIndex: titleModel.componentIndex
  153. } : null;
  154. group.add(textEl);
  155. subText && group.add(subTextEl); // If no subText, but add subTextEl, there will be an empty line.
  156. var groupRect = group.getBoundingRect();
  157. var layoutOption = titleModel.getBoxLayoutParams();
  158. layoutOption.width = groupRect.width;
  159. layoutOption.height = groupRect.height;
  160. var layoutRect = getLayoutRect(layoutOption, {
  161. width: api.getWidth(),
  162. height: api.getHeight()
  163. }, titleModel.get('padding')); // Adjust text align based on position
  164. if (!textAlign) {
  165. // Align left if title is on the left. center and right is same
  166. textAlign = titleModel.get('left') || titleModel.get('right');
  167. if (textAlign === 'middle') {
  168. textAlign = 'center';
  169. } // Adjust layout by text align
  170. if (textAlign === 'right') {
  171. layoutRect.x += layoutRect.width;
  172. } else if (textAlign === 'center') {
  173. layoutRect.x += layoutRect.width / 2;
  174. }
  175. }
  176. if (!textVerticalAlign) {
  177. textVerticalAlign = titleModel.get('top') || titleModel.get('bottom');
  178. if (textVerticalAlign === 'center') {
  179. textVerticalAlign = 'middle';
  180. }
  181. if (textVerticalAlign === 'bottom') {
  182. layoutRect.y += layoutRect.height;
  183. } else if (textVerticalAlign === 'middle') {
  184. layoutRect.y += layoutRect.height / 2;
  185. }
  186. textVerticalAlign = textVerticalAlign || 'top';
  187. }
  188. group.attr('position', [layoutRect.x, layoutRect.y]);
  189. var alignStyle = {
  190. textAlign: textAlign,
  191. textVerticalAlign: textVerticalAlign
  192. };
  193. textEl.setStyle(alignStyle);
  194. subTextEl.setStyle(alignStyle); // Render background
  195. // Get groupRect again because textAlign has been changed
  196. groupRect = group.getBoundingRect();
  197. var padding = layoutRect.margin;
  198. var style = titleModel.getItemStyle(['color', 'opacity']);
  199. style.fill = titleModel.get('backgroundColor');
  200. var rect = new graphic.Rect({
  201. shape: {
  202. x: groupRect.x - padding[3],
  203. y: groupRect.y - padding[0],
  204. width: groupRect.width + padding[1] + padding[3],
  205. height: groupRect.height + padding[0] + padding[2],
  206. r: titleModel.get('borderRadius')
  207. },
  208. style: style,
  209. subPixelOptimize: true,
  210. silent: true
  211. });
  212. group.add(rect);
  213. }
  214. });