viewHelper.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import * as zrUtil from 'zrender/lib/core/util.js';
  41. import * as graphic from '../../util/graphic.js';
  42. import * as textContain from 'zrender/lib/contain/text.js';
  43. import * as formatUtil from '../../util/format.js';
  44. import * as matrix from 'zrender/lib/core/matrix.js';
  45. import * as axisHelper from '../../coord/axisHelper.js';
  46. import AxisBuilder from '../axis/AxisBuilder.js';
  47. import { createTextStyle } from '../../label/labelStyle.js';
  48. export function buildElStyle(axisPointerModel) {
  49. var axisPointerType = axisPointerModel.get('type');
  50. var styleModel = axisPointerModel.getModel(axisPointerType + 'Style');
  51. var style;
  52. if (axisPointerType === 'line') {
  53. style = styleModel.getLineStyle();
  54. style.fill = null;
  55. } else if (axisPointerType === 'shadow') {
  56. style = styleModel.getAreaStyle();
  57. style.stroke = null;
  58. }
  59. return style;
  60. }
  61. /**
  62. * @param {Function} labelPos {align, verticalAlign, position}
  63. */
  64. export function buildLabelElOption(elOption, axisModel, axisPointerModel, api, labelPos) {
  65. var value = axisPointerModel.get('value');
  66. var text = getValueLabel(value, axisModel.axis, axisModel.ecModel, axisPointerModel.get('seriesDataIndices'), {
  67. precision: axisPointerModel.get(['label', 'precision']),
  68. formatter: axisPointerModel.get(['label', 'formatter'])
  69. });
  70. var labelModel = axisPointerModel.getModel('label');
  71. var paddings = formatUtil.normalizeCssArray(labelModel.get('padding') || 0);
  72. var font = labelModel.getFont();
  73. var textRect = textContain.getBoundingRect(text, font);
  74. var position = labelPos.position;
  75. var width = textRect.width + paddings[1] + paddings[3];
  76. var height = textRect.height + paddings[0] + paddings[2]; // Adjust by align.
  77. var align = labelPos.align;
  78. align === 'right' && (position[0] -= width);
  79. align === 'center' && (position[0] -= width / 2);
  80. var verticalAlign = labelPos.verticalAlign;
  81. verticalAlign === 'bottom' && (position[1] -= height);
  82. verticalAlign === 'middle' && (position[1] -= height / 2); // Not overflow ec container
  83. confineInContainer(position, width, height, api);
  84. var bgColor = labelModel.get('backgroundColor');
  85. if (!bgColor || bgColor === 'auto') {
  86. bgColor = axisModel.get(['axisLine', 'lineStyle', 'color']);
  87. }
  88. elOption.label = {
  89. // shape: {x: 0, y: 0, width: width, height: height, r: labelModel.get('borderRadius')},
  90. x: position[0],
  91. y: position[1],
  92. style: createTextStyle(labelModel, {
  93. text: text,
  94. font: font,
  95. fill: labelModel.getTextColor(),
  96. padding: paddings,
  97. backgroundColor: bgColor
  98. }),
  99. // Label should be over axisPointer.
  100. z2: 10
  101. };
  102. } // Do not overflow ec container
  103. function confineInContainer(position, width, height, api) {
  104. var viewWidth = api.getWidth();
  105. var viewHeight = api.getHeight();
  106. position[0] = Math.min(position[0] + width, viewWidth) - width;
  107. position[1] = Math.min(position[1] + height, viewHeight) - height;
  108. position[0] = Math.max(position[0], 0);
  109. position[1] = Math.max(position[1], 0);
  110. }
  111. export function getValueLabel(value, axis, ecModel, seriesDataIndices, opt) {
  112. value = axis.scale.parse(value);
  113. var text = axis.scale.getLabel({
  114. value: value
  115. }, {
  116. // If `precision` is set, width can be fixed (like '12.00500'), which
  117. // helps to debounce when when moving label.
  118. precision: opt.precision
  119. });
  120. var formatter = opt.formatter;
  121. if (formatter) {
  122. var params_1 = {
  123. value: axisHelper.getAxisRawValue(axis, {
  124. value: value
  125. }),
  126. axisDimension: axis.dim,
  127. axisIndex: axis.index,
  128. seriesData: []
  129. };
  130. zrUtil.each(seriesDataIndices, function (idxItem) {
  131. var series = ecModel.getSeriesByIndex(idxItem.seriesIndex);
  132. var dataIndex = idxItem.dataIndexInside;
  133. var dataParams = series && series.getDataParams(dataIndex);
  134. dataParams && params_1.seriesData.push(dataParams);
  135. });
  136. if (zrUtil.isString(formatter)) {
  137. text = formatter.replace('{value}', text);
  138. } else if (zrUtil.isFunction(formatter)) {
  139. text = formatter(params_1);
  140. }
  141. }
  142. return text;
  143. }
  144. export function getTransformedPosition(axis, value, layoutInfo) {
  145. var transform = matrix.create();
  146. matrix.rotate(transform, transform, layoutInfo.rotation);
  147. matrix.translate(transform, transform, layoutInfo.position);
  148. return graphic.applyTransform([axis.dataToCoord(value), (layoutInfo.labelOffset || 0) + (layoutInfo.labelDirection || 1) * (layoutInfo.labelMargin || 0)], transform);
  149. }
  150. export function buildCartesianSingleLabelElOption(value, elOption, layoutInfo, axisModel, axisPointerModel, api) {
  151. // @ts-ignore
  152. var textLayout = AxisBuilder.innerTextLayout(layoutInfo.rotation, 0, layoutInfo.labelDirection);
  153. layoutInfo.labelMargin = axisPointerModel.get(['label', 'margin']);
  154. buildLabelElOption(elOption, axisModel, axisPointerModel, api, {
  155. position: getTransformedPosition(axisModel.axis, value, layoutInfo),
  156. align: textLayout.textAlign,
  157. verticalAlign: textLayout.textVerticalAlign
  158. });
  159. }
  160. export function makeLineShape(p1, p2, xDimIndex) {
  161. xDimIndex = xDimIndex || 0;
  162. return {
  163. x1: p1[xDimIndex],
  164. y1: p1[1 - xDimIndex],
  165. x2: p2[xDimIndex],
  166. y2: p2[1 - xDimIndex]
  167. };
  168. }
  169. export function makeRectShape(xy, wh, xDimIndex) {
  170. xDimIndex = xDimIndex || 0;
  171. return {
  172. x: xy[xDimIndex],
  173. y: xy[1 - xDimIndex],
  174. width: wh[xDimIndex],
  175. height: wh[1 - xDimIndex]
  176. };
  177. }
  178. export function makeSectorShape(cx, cy, r0, r, startAngle, endAngle) {
  179. return {
  180. cx: cx,
  181. cy: cy,
  182. r0: r0,
  183. r: r,
  184. startAngle: startAngle,
  185. endAngle: endAngle,
  186. clockwise: true
  187. };
  188. }