title.js 7.4 KB

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