viewHelper.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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];
  77. // Adjust by align.
  78. var align = labelPos.align;
  79. align === 'right' && (position[0] -= width);
  80. align === 'center' && (position[0] -= width / 2);
  81. var verticalAlign = labelPos.verticalAlign;
  82. verticalAlign === 'bottom' && (position[1] -= height);
  83. verticalAlign === 'middle' && (position[1] -= height / 2);
  84. // Not overflow ec container
  85. confineInContainer(position, width, height, api);
  86. var bgColor = labelModel.get('backgroundColor');
  87. if (!bgColor || bgColor === 'auto') {
  88. bgColor = axisModel.get(['axisLine', 'lineStyle', 'color']);
  89. }
  90. elOption.label = {
  91. // shape: {x: 0, y: 0, width: width, height: height, r: labelModel.get('borderRadius')},
  92. x: position[0],
  93. y: position[1],
  94. style: createTextStyle(labelModel, {
  95. text: text,
  96. font: font,
  97. fill: labelModel.getTextColor(),
  98. padding: paddings,
  99. backgroundColor: bgColor
  100. }),
  101. // Label should be over axisPointer.
  102. z2: 10
  103. };
  104. }
  105. // Do not overflow ec container
  106. function confineInContainer(position, width, height, api) {
  107. var viewWidth = api.getWidth();
  108. var viewHeight = api.getHeight();
  109. position[0] = Math.min(position[0] + width, viewWidth) - width;
  110. position[1] = Math.min(position[1] + height, viewHeight) - height;
  111. position[0] = Math.max(position[0], 0);
  112. position[1] = Math.max(position[1], 0);
  113. }
  114. export function getValueLabel(value, axis, ecModel, seriesDataIndices, opt) {
  115. value = axis.scale.parse(value);
  116. var text = axis.scale.getLabel({
  117. value: value
  118. }, {
  119. // If `precision` is set, width can be fixed (like '12.00500'), which
  120. // helps to debounce when when moving label.
  121. precision: opt.precision
  122. });
  123. var formatter = opt.formatter;
  124. if (formatter) {
  125. var params_1 = {
  126. value: axisHelper.getAxisRawValue(axis, {
  127. value: value
  128. }),
  129. axisDimension: axis.dim,
  130. axisIndex: axis.index,
  131. seriesData: []
  132. };
  133. zrUtil.each(seriesDataIndices, function (idxItem) {
  134. var series = ecModel.getSeriesByIndex(idxItem.seriesIndex);
  135. var dataIndex = idxItem.dataIndexInside;
  136. var dataParams = series && series.getDataParams(dataIndex);
  137. dataParams && params_1.seriesData.push(dataParams);
  138. });
  139. if (zrUtil.isString(formatter)) {
  140. text = formatter.replace('{value}', text);
  141. } else if (zrUtil.isFunction(formatter)) {
  142. text = formatter(params_1);
  143. }
  144. }
  145. return text;
  146. }
  147. export function getTransformedPosition(axis, value, layoutInfo) {
  148. var transform = matrix.create();
  149. matrix.rotate(transform, transform, layoutInfo.rotation);
  150. matrix.translate(transform, transform, layoutInfo.position);
  151. return graphic.applyTransform([axis.dataToCoord(value), (layoutInfo.labelOffset || 0) + (layoutInfo.labelDirection || 1) * (layoutInfo.labelMargin || 0)], transform);
  152. }
  153. export function buildCartesianSingleLabelElOption(value, elOption, layoutInfo, axisModel, axisPointerModel, api) {
  154. // @ts-ignore
  155. var textLayout = AxisBuilder.innerTextLayout(layoutInfo.rotation, 0, layoutInfo.labelDirection);
  156. layoutInfo.labelMargin = axisPointerModel.get(['label', 'margin']);
  157. buildLabelElOption(elOption, axisModel, axisPointerModel, api, {
  158. position: getTransformedPosition(axisModel.axis, value, layoutInfo),
  159. align: textLayout.textAlign,
  160. verticalAlign: textLayout.textVerticalAlign
  161. });
  162. }
  163. export function makeLineShape(p1, p2, xDimIndex) {
  164. xDimIndex = xDimIndex || 0;
  165. return {
  166. x1: p1[xDimIndex],
  167. y1: p1[1 - xDimIndex],
  168. x2: p2[xDimIndex],
  169. y2: p2[1 - xDimIndex]
  170. };
  171. }
  172. export function makeRectShape(xy, wh, xDimIndex) {
  173. xDimIndex = xDimIndex || 0;
  174. return {
  175. x: xy[xDimIndex],
  176. y: xy[1 - xDimIndex],
  177. width: wh[xDimIndex],
  178. height: wh[1 - xDimIndex]
  179. };
  180. }
  181. export function makeSectorShape(cx, cy, r0, r, startAngle, endAngle) {
  182. return {
  183. cx: cx,
  184. cy: cy,
  185. r0: r0,
  186. r: r,
  187. startAngle: startAngle,
  188. endAngle: endAngle,
  189. clockwise: true
  190. };
  191. }