viewHelper.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. import { calcBandWidth } from '../../coord/axisBand.js';
  49. import { mathMax, mathMin } from '../../util/number.js';
  50. export function buildElStyle(axisPointerModel) {
  51. var axisPointerType = axisPointerModel.get('type');
  52. var styleModel = axisPointerModel.getModel(axisPointerType + 'Style');
  53. var style;
  54. if (axisPointerType === 'line') {
  55. style = styleModel.getLineStyle();
  56. style.fill = null;
  57. } else if (axisPointerType === 'shadow') {
  58. style = styleModel.getAreaStyle();
  59. style.stroke = null;
  60. }
  61. return style;
  62. }
  63. /**
  64. * @param {Function} labelPos {align, verticalAlign, position}
  65. */
  66. export function buildLabelElOption(elOption, axisModel, axisPointerModel, api, labelPos) {
  67. var value = axisPointerModel.get('value');
  68. var text = getValueLabel(value, axisModel.axis, axisModel.ecModel, axisPointerModel.get('seriesDataIndices'), {
  69. precision: axisPointerModel.get(['label', 'precision']),
  70. formatter: axisPointerModel.get(['label', 'formatter'])
  71. });
  72. var labelModel = axisPointerModel.getModel('label');
  73. var paddings = formatUtil.normalizeCssArray(labelModel.get('padding') || 0);
  74. var font = labelModel.getFont();
  75. var textRect = textContain.getBoundingRect(text, font);
  76. var position = labelPos.position;
  77. var width = textRect.width + paddings[1] + paddings[3];
  78. var height = textRect.height + paddings[0] + paddings[2];
  79. // Adjust by align.
  80. var align = labelPos.align;
  81. align === 'right' && (position[0] -= width);
  82. align === 'center' && (position[0] -= width / 2);
  83. var verticalAlign = labelPos.verticalAlign;
  84. verticalAlign === 'bottom' && (position[1] -= height);
  85. verticalAlign === 'middle' && (position[1] -= height / 2);
  86. // Not overflow ec container
  87. confineInContainer(position, width, height, api);
  88. var bgColor = labelModel.get('backgroundColor');
  89. if (!bgColor || bgColor === 'auto') {
  90. bgColor = axisModel.get(['axisLine', 'lineStyle', 'color']);
  91. }
  92. elOption.label = {
  93. // shape: {x: 0, y: 0, width: width, height: height, r: labelModel.get('borderRadius')},
  94. x: position[0],
  95. y: position[1],
  96. style: createTextStyle(labelModel, {
  97. text: text,
  98. font: font,
  99. fill: labelModel.getTextColor(),
  100. padding: paddings,
  101. backgroundColor: bgColor
  102. }),
  103. // Label should be over axisPointer.
  104. z2: 10
  105. };
  106. }
  107. // Do not overflow ec container
  108. function confineInContainer(position, width, height, api) {
  109. var viewWidth = api.getWidth();
  110. var viewHeight = api.getHeight();
  111. position[0] = Math.min(position[0] + width, viewWidth) - width;
  112. position[1] = Math.min(position[1] + height, viewHeight) - height;
  113. position[0] = Math.max(position[0], 0);
  114. position[1] = Math.max(position[1], 0);
  115. }
  116. export function getValueLabel(value, axis, ecModel, seriesDataIndices, opt) {
  117. value = axis.scale.parse(value);
  118. var text = axis.scale.getLabel({
  119. value: value
  120. }, {
  121. // If `precision` is set, width can be fixed (like '12.00500'), which
  122. // helps to debounce when when moving label.
  123. precision: opt.precision
  124. });
  125. var formatter = opt.formatter;
  126. if (formatter) {
  127. var params_1 = {
  128. value: axisHelper.getAxisRawValue(axis, {
  129. value: value
  130. }),
  131. axisDimension: axis.dim,
  132. axisIndex: axis.index,
  133. seriesData: []
  134. };
  135. zrUtil.each(seriesDataIndices, function (idxItem) {
  136. var series = ecModel.getSeriesByIndex(idxItem.seriesIndex);
  137. var dataIndex = idxItem.dataIndexInside;
  138. var dataParams = series && series.getDataParams(dataIndex);
  139. dataParams && params_1.seriesData.push(dataParams);
  140. });
  141. if (zrUtil.isString(formatter)) {
  142. text = formatter.replace('{value}', text);
  143. } else if (zrUtil.isFunction(formatter)) {
  144. text = formatter(params_1);
  145. }
  146. }
  147. return text;
  148. }
  149. export function getTransformedPosition(axis, value, layoutInfo) {
  150. var transform = matrix.create();
  151. matrix.rotate(transform, transform, layoutInfo.rotation);
  152. matrix.translate(transform, transform, layoutInfo.position);
  153. return graphic.applyTransform([axis.dataToCoord(value), (layoutInfo.labelOffset || 0) + (layoutInfo.labelDirection || 1) * (layoutInfo.labelMargin || 0)], transform);
  154. }
  155. export function buildCartesianSingleLabelElOption(value, elOption, layoutInfo, axisModel, axisPointerModel, api) {
  156. var textLayout = AxisBuilder.innerTextLayout(layoutInfo.rotation, 0, layoutInfo.labelDirection);
  157. layoutInfo.labelMargin = axisPointerModel.get(['label', 'margin']);
  158. buildLabelElOption(elOption, axisModel, axisPointerModel, api, {
  159. position: getTransformedPosition(axisModel.axis, value, layoutInfo),
  160. align: textLayout.textAlign,
  161. verticalAlign: textLayout.textVerticalAlign
  162. });
  163. }
  164. export function makeLineShape(p1, p2, xDimIndex) {
  165. xDimIndex = xDimIndex || 0;
  166. return {
  167. x1: p1[xDimIndex],
  168. y1: p1[1 - xDimIndex],
  169. x2: p2[xDimIndex],
  170. y2: p2[1 - xDimIndex]
  171. };
  172. }
  173. export function makeRectShape(xy, wh, xDimIndex) {
  174. xDimIndex = xDimIndex || 0;
  175. return {
  176. x: xy[xDimIndex],
  177. y: xy[1 - xDimIndex],
  178. width: wh[xDimIndex],
  179. height: wh[1 - xDimIndex]
  180. };
  181. }
  182. export function makeSectorShape(cx, cy, r0, r, startAngle, endAngle) {
  183. return {
  184. cx: cx,
  185. cy: cy,
  186. r0: r0,
  187. r: r,
  188. startAngle: startAngle,
  189. endAngle: endAngle,
  190. clockwise: true
  191. };
  192. }
  193. export function calcAxisPointerShadowBandWidth(axis, seriesDataIndices, ecModel) {
  194. return calcBandWidth(axis, {
  195. fromStat: {
  196. sers: zrUtil.map(seriesDataIndices, function (item) {
  197. return ecModel.getSeriesByIndex(item.seriesIndex);
  198. })
  199. },
  200. min: 1
  201. }).w;
  202. }
  203. /**
  204. * Return a [min, max] in pixel clampped by `axisExtent`.
  205. */
  206. export function calcAxisPointerShadowEnds(val, axisExtent, bandWidth) {
  207. return [mathMax(mathMin(axisExtent[0], axisExtent[1]), val - bandWidth / 2), mathMin(val + bandWidth / 2, mathMax(axisExtent[0], axisExtent[1]))];
  208. }