graphic.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 pathTool from 'zrender/lib/tool/path.js';
  41. import * as matrix from 'zrender/lib/core/matrix.js';
  42. import * as vector from 'zrender/lib/core/vector.js';
  43. import Path from 'zrender/lib/graphic/Path.js';
  44. import Transformable from 'zrender/lib/core/Transformable.js';
  45. import ZRImage from 'zrender/lib/graphic/Image.js';
  46. import Group from 'zrender/lib/graphic/Group.js';
  47. import ZRText from 'zrender/lib/graphic/Text.js';
  48. import Circle from 'zrender/lib/graphic/shape/Circle.js';
  49. import Ellipse from 'zrender/lib/graphic/shape/Ellipse.js';
  50. import Sector from 'zrender/lib/graphic/shape/Sector.js';
  51. import Ring from 'zrender/lib/graphic/shape/Ring.js';
  52. import Polygon from 'zrender/lib/graphic/shape/Polygon.js';
  53. import Polyline from 'zrender/lib/graphic/shape/Polyline.js';
  54. import Rect from 'zrender/lib/graphic/shape/Rect.js';
  55. import Line from 'zrender/lib/graphic/shape/Line.js';
  56. import BezierCurve from 'zrender/lib/graphic/shape/BezierCurve.js';
  57. import Arc from 'zrender/lib/graphic/shape/Arc.js';
  58. import CompoundPath from 'zrender/lib/graphic/CompoundPath.js';
  59. import LinearGradient from 'zrender/lib/graphic/LinearGradient.js';
  60. import RadialGradient from 'zrender/lib/graphic/RadialGradient.js';
  61. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  62. import OrientedBoundingRect from 'zrender/lib/core/OrientedBoundingRect.js';
  63. import Point from 'zrender/lib/core/Point.js';
  64. import IncrementalDisplayable from 'zrender/lib/graphic/IncrementalDisplayable.js';
  65. import * as subPixelOptimizeUtil from 'zrender/lib/graphic/helper/subPixelOptimize.js';
  66. import { extend, isArrayLike, map, defaults, isString, keys, each, hasOwn, isArray, isNumber, clone, assert } from 'zrender/lib/core/util.js';
  67. import { getECData } from './innerStore.js';
  68. import { updateProps, initProps, removeElement, removeElementWithFadeOut, isElementRemoved } from '../animation/basicTransition.js';
  69. import { mathMin, mathMax, mathAbs } from './number.js';
  70. /**
  71. * @deprecated export for compatitable reason
  72. */
  73. export { updateProps, initProps, removeElement, removeElementWithFadeOut, isElementRemoved };
  74. var _customShapeMap = {};
  75. export var XY = ['x', 'y'];
  76. export var WH = ['width', 'height'];
  77. /**
  78. * Extend shape with parameters
  79. */
  80. export function extendShape(opts) {
  81. return Path.extend(opts);
  82. }
  83. var extendPathFromString = pathTool.extendFromString;
  84. /**
  85. * Extend path
  86. */
  87. export function extendPath(pathData, opts) {
  88. return extendPathFromString(pathData, opts);
  89. }
  90. /**
  91. * Register a user defined shape.
  92. * The shape class can be fetched by `getShapeClass`
  93. * This method will overwrite the registered shapes, including
  94. * the registered built-in shapes, if using the same `name`.
  95. * The shape can be used in `custom series` and
  96. * `graphic component` by declaring `{type: name}`.
  97. *
  98. * @param name
  99. * @param ShapeClass Can be generated by `extendShape`.
  100. */
  101. export function registerShape(name, ShapeClass) {
  102. _customShapeMap[name] = ShapeClass;
  103. }
  104. /**
  105. * Find shape class registered by `registerShape`. Usually used in
  106. * fetching user defined shape.
  107. *
  108. * [Caution]:
  109. * (1) This method **MUST NOT be used inside echarts !!!**, unless it is prepared
  110. * to use user registered shapes.
  111. * Because the built-in shape (see `getBuiltInShape`) will be registered by
  112. * `registerShape` by default. That enables users to get both built-in
  113. * shapes as well as the shapes belonging to themsleves. But users can overwrite
  114. * the built-in shapes by using names like 'circle', 'rect' via calling
  115. * `registerShape`. So the echarts inner featrues should not fetch shapes from here
  116. * in case that it is overwritten by users, except that some features, like
  117. * `custom series`, `graphic component`, do it deliberately.
  118. *
  119. * (2) In the features like `custom series`, `graphic component`, the user input
  120. * `{tpye: 'xxx'}` does not only specify shapes but also specify other graphic
  121. * elements like `'group'`, `'text'`, `'image'` or event `'path'`. Those names
  122. * are reserved names, that is, if some user registers a shape named `'image'`,
  123. * the shape will not be used. If we intending to add some more reserved names
  124. * in feature, that might bring break changes (disable some existing user shape
  125. * names). But that case probably rarely happens. So we don't make more mechanism
  126. * to resolve this issue here.
  127. *
  128. * @param name
  129. * @return The shape class. If not found, return nothing.
  130. */
  131. export function getShapeClass(name) {
  132. if (_customShapeMap.hasOwnProperty(name)) {
  133. return _customShapeMap[name];
  134. }
  135. }
  136. /**
  137. * Create a path element from path data string
  138. * @param pathData
  139. * @param opts
  140. * @param rect
  141. * @param layout 'center' or 'cover' default to be cover
  142. */
  143. export function makePath(pathData, opts, rect, layout) {
  144. var path = pathTool.createFromString(pathData, opts);
  145. if (rect) {
  146. if (layout === 'center') {
  147. rect = centerGraphic(rect, path.getBoundingRect());
  148. }
  149. resizePath(path, rect);
  150. }
  151. return path;
  152. }
  153. /**
  154. * Create a image element from image url
  155. * @param imageUrl image url
  156. * @param opts options
  157. * @param rect constrain rect
  158. * @param layout 'center' or 'cover'. Default to be 'cover'
  159. */
  160. export function makeImage(imageUrl, rect, layout) {
  161. var zrImg = new ZRImage({
  162. style: {
  163. image: imageUrl,
  164. x: rect.x,
  165. y: rect.y,
  166. width: rect.width,
  167. height: rect.height
  168. },
  169. onload: function (img) {
  170. if (layout === 'center') {
  171. var boundingRect = {
  172. width: img.width,
  173. height: img.height
  174. };
  175. zrImg.setStyle(centerGraphic(rect, boundingRect));
  176. }
  177. }
  178. });
  179. return zrImg;
  180. }
  181. /**
  182. * Get position of centered element in bounding box.
  183. *
  184. * @param rect element local bounding box
  185. * @param boundingRect constraint bounding box
  186. * @return element position containing x, y, width, and height
  187. */
  188. function centerGraphic(rect, boundingRect) {
  189. // Set rect to center, keep width / height ratio.
  190. var aspect = boundingRect.width / boundingRect.height;
  191. var width = rect.height * aspect;
  192. var height;
  193. if (width <= rect.width) {
  194. height = rect.height;
  195. } else {
  196. width = rect.width;
  197. height = width / aspect;
  198. }
  199. var cx = rect.x + rect.width / 2;
  200. var cy = rect.y + rect.height / 2;
  201. return {
  202. x: cx - width / 2,
  203. y: cy - height / 2,
  204. width: width,
  205. height: height
  206. };
  207. }
  208. export var mergePath = pathTool.mergePath;
  209. /**
  210. * Resize a path to fit the rect
  211. * @param path
  212. * @param rect
  213. */
  214. export function resizePath(path, rect) {
  215. if (!path.applyTransform) {
  216. return;
  217. }
  218. var pathRect = path.getBoundingRect();
  219. var m = pathRect.calculateTransform(rect);
  220. path.applyTransform(m);
  221. }
  222. /**
  223. * Sub pixel optimize line for canvas
  224. */
  225. export function subPixelOptimizeLine(shape, lineWidth) {
  226. subPixelOptimizeUtil.subPixelOptimizeLine(shape, shape, {
  227. lineWidth: lineWidth
  228. });
  229. return shape;
  230. }
  231. /**
  232. * Sub pixel optimize rect for canvas
  233. */
  234. export function subPixelOptimizeRect(shape, style) {
  235. subPixelOptimizeUtil.subPixelOptimizeRect(shape, shape, style);
  236. return shape;
  237. }
  238. /**
  239. * Sub pixel optimize for canvas
  240. *
  241. * @param position Coordinate, such as x, y
  242. * @param lineWidth Should be nonnegative integer.
  243. * @param positiveOrNegative Default false (negative).
  244. * @return Optimized position.
  245. */
  246. export var subPixelOptimize = subPixelOptimizeUtil.subPixelOptimize;
  247. /**
  248. * Get transform matrix of target (param target),
  249. * in coordinate of its ancestor (param ancestor)
  250. *
  251. * @param target
  252. * @param [ancestor]
  253. */
  254. export function getTransform(target, ancestor) {
  255. var mat = matrix.identity([]);
  256. while (target && target !== ancestor) {
  257. matrix.mul(mat, target.getLocalTransform(), mat);
  258. target = target.parent;
  259. }
  260. return mat;
  261. }
  262. /**
  263. * Apply transform to an vertex.
  264. * @param target [x, y]
  265. * @param transform Can be:
  266. * + Transform matrix: like [1, 0, 0, 1, 0, 0]
  267. * + {position, rotation, scale}, the same as `zrender/Transformable`.
  268. * @param invert Whether use invert matrix.
  269. * @return [x, y]
  270. */
  271. export function applyTransform(target, transform, invert) {
  272. if (transform && !isArrayLike(transform)) {
  273. transform = Transformable.getLocalTransform(transform);
  274. }
  275. if (invert) {
  276. transform = matrix.invert([], transform);
  277. }
  278. return vector.applyTransform([], target, transform);
  279. }
  280. /**
  281. * @param direction 'left' 'right' 'top' 'bottom'
  282. * @param transform Transform matrix: like [1, 0, 0, 1, 0, 0]
  283. * @param invert Whether use invert matrix.
  284. * @return Transformed direction. 'left' 'right' 'top' 'bottom'
  285. */
  286. export function transformDirection(direction, transform, invert) {
  287. // Pick a base, ensure that transform result will not be (0, 0).
  288. var hBase = transform[4] === 0 || transform[5] === 0 || transform[0] === 0 ? 1 : mathAbs(2 * transform[4] / transform[0]);
  289. var vBase = transform[4] === 0 || transform[5] === 0 || transform[2] === 0 ? 1 : mathAbs(2 * transform[4] / transform[2]);
  290. var vertex = [direction === 'left' ? -hBase : direction === 'right' ? hBase : 0, direction === 'top' ? -vBase : direction === 'bottom' ? vBase : 0];
  291. vertex = applyTransform(vertex, transform, invert);
  292. return mathAbs(vertex[0]) > mathAbs(vertex[1]) ? vertex[0] > 0 ? 'right' : 'left' : vertex[1] > 0 ? 'bottom' : 'top';
  293. }
  294. function isNotGroup(el) {
  295. return !el.isGroup;
  296. }
  297. function isPath(el) {
  298. return el.shape != null;
  299. }
  300. /**
  301. * Apply group transition animation from g1 to g2.
  302. * If no animatableModel, no animation.
  303. */
  304. export function groupTransition(g1, g2, animatableModel) {
  305. if (!g1 || !g2) {
  306. return;
  307. }
  308. function getElMap(g) {
  309. var elMap = {};
  310. g.traverse(function (el) {
  311. if (isNotGroup(el) && el.anid) {
  312. elMap[el.anid] = el;
  313. }
  314. });
  315. return elMap;
  316. }
  317. function getAnimatableProps(el) {
  318. var obj = {
  319. x: el.x,
  320. y: el.y,
  321. rotation: el.rotation
  322. };
  323. if (isPath(el)) {
  324. obj.shape = clone(el.shape);
  325. }
  326. return obj;
  327. }
  328. var elMap1 = getElMap(g1);
  329. g2.traverse(function (el) {
  330. if (isNotGroup(el) && el.anid) {
  331. var oldEl = elMap1[el.anid];
  332. if (oldEl) {
  333. var newProp = getAnimatableProps(el);
  334. el.attr(getAnimatableProps(oldEl));
  335. updateProps(el, newProp, animatableModel, getECData(el).dataIndex);
  336. }
  337. }
  338. });
  339. }
  340. export function clipPointsByRect(points, rect) {
  341. // FIXME: This way might be incorrect when graphic clipped by a corner
  342. // and when element has a border.
  343. return map(points, function (point) {
  344. var x = point[0];
  345. x = mathMax(x, rect.x);
  346. x = mathMin(x, rect.x + rect.width);
  347. var y = point[1];
  348. y = mathMax(y, rect.y);
  349. y = mathMin(y, rect.y + rect.height);
  350. return [x, y];
  351. });
  352. }
  353. /**
  354. * Return a new clipped rect. If rect size are negative, return undefined.
  355. */
  356. export function clipRectByRect(targetRect, rect) {
  357. var x = mathMax(targetRect.x, rect.x);
  358. var x2 = mathMin(targetRect.x + targetRect.width, rect.x + rect.width);
  359. var y = mathMax(targetRect.y, rect.y);
  360. var y2 = mathMin(targetRect.y + targetRect.height, rect.y + rect.height);
  361. // If the total rect is cliped, nothing, including the border,
  362. // should be painted. So return undefined.
  363. if (x2 >= x && y2 >= y) {
  364. return {
  365. x: x,
  366. y: y,
  367. width: x2 - x,
  368. height: y2 - y
  369. };
  370. }
  371. }
  372. export function createIcon(iconStr,
  373. // Support 'image://' or 'path://' or direct svg path.
  374. opt, rect) {
  375. var innerOpts = extend({
  376. rectHover: true
  377. }, opt);
  378. var style = innerOpts.style = {
  379. strokeNoScale: true
  380. };
  381. rect = rect || {
  382. x: -1,
  383. y: -1,
  384. width: 2,
  385. height: 2
  386. };
  387. if (iconStr) {
  388. return iconStr.indexOf('image://') === 0 ? (style.image = iconStr.slice(8), defaults(style, rect), new ZRImage(innerOpts)) : makePath(iconStr.replace('path://', ''), innerOpts, rect, 'center');
  389. }
  390. }
  391. /**
  392. * Return `true` if the given line (line `a`) and the given polygon
  393. * are intersect.
  394. * Note that we do not count colinear as intersect here because no
  395. * requirement for that. We could do that if required in future.
  396. */
  397. export function linePolygonIntersect(a1x, a1y, a2x, a2y, points) {
  398. for (var i = 0, p2 = points[points.length - 1]; i < points.length; i++) {
  399. var p = points[i];
  400. if (lineLineIntersect(a1x, a1y, a2x, a2y, p[0], p[1], p2[0], p2[1])) {
  401. return true;
  402. }
  403. p2 = p;
  404. }
  405. }
  406. /**
  407. * Return `true` if the given two lines (line `a` and line `b`)
  408. * are intersect.
  409. * Note that we do not count colinear as intersect here because no
  410. * requirement for that. We could do that if required in future.
  411. */
  412. export function lineLineIntersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y) {
  413. // let `vec_m` to be `vec_a2 - vec_a1` and `vec_n` to be `vec_b2 - vec_b1`.
  414. var mx = a2x - a1x;
  415. var my = a2y - a1y;
  416. var nx = b2x - b1x;
  417. var ny = b2y - b1y;
  418. // `vec_m` and `vec_n` are parallel iff
  419. // existing `k` such that `vec_m = k · vec_n`, equivalent to `vec_m X vec_n = 0`.
  420. var nmCrossProduct = crossProduct2d(nx, ny, mx, my);
  421. if (nearZero(nmCrossProduct)) {
  422. return false;
  423. }
  424. // `vec_m` and `vec_n` are intersect iff
  425. // existing `p` and `q` in [0, 1] such that `vec_a1 + p * vec_m = vec_b1 + q * vec_n`,
  426. // such that `q = ((vec_a1 - vec_b1) X vec_m) / (vec_n X vec_m)`
  427. // and `p = ((vec_a1 - vec_b1) X vec_n) / (vec_n X vec_m)`.
  428. var b1a1x = a1x - b1x;
  429. var b1a1y = a1y - b1y;
  430. var q = crossProduct2d(b1a1x, b1a1y, mx, my) / nmCrossProduct;
  431. if (q < 0 || q > 1) {
  432. return false;
  433. }
  434. var p = crossProduct2d(b1a1x, b1a1y, nx, ny) / nmCrossProduct;
  435. if (p < 0 || p > 1) {
  436. return false;
  437. }
  438. return true;
  439. }
  440. /**
  441. * Cross product of 2-dimension vector.
  442. */
  443. function crossProduct2d(x1, y1, x2, y2) {
  444. return x1 * y2 - x2 * y1;
  445. }
  446. function nearZero(val) {
  447. return val <= 1e-6 && val >= -1e-6;
  448. }
  449. /**
  450. * NOTE:
  451. * A negative-width/height rect (due to negative margins) is not supported;
  452. * it will be clampped to zero width/height.
  453. * Although negative-width/height rects can be defined reasonably following the
  454. * similar sense in CSS, but they are rarely used, hard to understand and complicated.
  455. *
  456. * @param rect Assume its width/height >= 0 if existing.
  457. * x/y/width/height is allowed to be NaN,
  458. * for the case that only x/width or y/height is intended to be computed.
  459. * @param delta
  460. * If be `number[]`, should be `[top, right, bottom, left]`,
  461. * which can be used in padding or margin case.
  462. * @see `normalizeCssArray` in `util/format.ts`
  463. * If be `number`, it means [delta, delta, delta, delta],
  464. * which can be used in lineWidth (borderWith) case,
  465. * [NOTICE]: commonly pass lineWidth / 2, following the convention that border is
  466. * half inside half outside of the rect.
  467. * @param shrinkOrExpand
  468. * `true` - shrink if `delta[i]` is positive, commmonly used in `padding` case.
  469. * `false` - expand if `delta[i]` is positive, commmonly used in `margin` case. (default)
  470. * @param noNegative
  471. * `true` - negative `delta[i]` will be clampped to 0.
  472. * `false` - No clamp to `delta`. (default).
  473. * @return The input `rect`.
  474. */
  475. export function expandOrShrinkRect(rect, delta, shrinkOrExpand, noNegative, minSize // by default [0, 0].
  476. ) {
  477. if (delta == null) {
  478. return rect;
  479. } else if (isNumber(delta)) {
  480. _tmpExpandRectDelta[0] = _tmpExpandRectDelta[1] = _tmpExpandRectDelta[2] = _tmpExpandRectDelta[3] = delta;
  481. } else {
  482. if (process.env.NODE_ENV !== 'production') {
  483. assert(delta.length === 4);
  484. }
  485. _tmpExpandRectDelta[0] = delta[0];
  486. _tmpExpandRectDelta[1] = delta[1];
  487. _tmpExpandRectDelta[2] = delta[2];
  488. _tmpExpandRectDelta[3] = delta[3];
  489. }
  490. if (noNegative) {
  491. _tmpExpandRectDelta[0] = mathMax(0, _tmpExpandRectDelta[0]);
  492. _tmpExpandRectDelta[1] = mathMax(0, _tmpExpandRectDelta[1]);
  493. _tmpExpandRectDelta[2] = mathMax(0, _tmpExpandRectDelta[2]);
  494. _tmpExpandRectDelta[3] = mathMax(0, _tmpExpandRectDelta[3]);
  495. }
  496. if (shrinkOrExpand) {
  497. _tmpExpandRectDelta[0] = -_tmpExpandRectDelta[0];
  498. _tmpExpandRectDelta[1] = -_tmpExpandRectDelta[1];
  499. _tmpExpandRectDelta[2] = -_tmpExpandRectDelta[2];
  500. _tmpExpandRectDelta[3] = -_tmpExpandRectDelta[3];
  501. }
  502. expandRectOnOneDimension(rect, _tmpExpandRectDelta, 'x', 'width', 3, 1, minSize && minSize[0] || 0);
  503. expandRectOnOneDimension(rect, _tmpExpandRectDelta, 'y', 'height', 0, 2, minSize && minSize[1] || 0);
  504. return rect;
  505. }
  506. var _tmpExpandRectDelta = [0, 0, 0, 0];
  507. function expandRectOnOneDimension(rect, delta, xy, wh, ltIdx, rbIdx, minSize) {
  508. var deltaSum = delta[rbIdx] + delta[ltIdx];
  509. var oldSize = rect[wh];
  510. rect[wh] += deltaSum;
  511. minSize = mathMax(0, mathMin(minSize, oldSize));
  512. if (rect[wh] < minSize) {
  513. rect[wh] = minSize;
  514. // Try to make the position of the zero rect reasonable in most visual cases.
  515. rect[xy] += delta[ltIdx] >= 0 ? -delta[ltIdx] : delta[rbIdx] >= 0 ? oldSize + delta[rbIdx] : mathAbs(deltaSum) > 1e-8 ? (oldSize - minSize) * delta[ltIdx] / deltaSum : 0;
  516. } else {
  517. rect[xy] -= delta[ltIdx];
  518. }
  519. }
  520. export function setTooltipConfig(opt) {
  521. var itemTooltipOption = opt.itemTooltipOption;
  522. var componentModel = opt.componentModel;
  523. var itemName = opt.itemName;
  524. var itemTooltipOptionObj = isString(itemTooltipOption) ? {
  525. formatter: itemTooltipOption
  526. } : itemTooltipOption;
  527. var mainType = componentModel.mainType;
  528. var componentIndex = componentModel.componentIndex;
  529. var formatterParams = {
  530. componentType: mainType,
  531. name: itemName,
  532. $vars: ['name']
  533. };
  534. formatterParams[mainType + 'Index'] = componentIndex;
  535. var formatterParamsExtra = opt.formatterParamsExtra;
  536. if (formatterParamsExtra) {
  537. each(keys(formatterParamsExtra), function (key) {
  538. if (!hasOwn(formatterParams, key)) {
  539. formatterParams[key] = formatterParamsExtra[key];
  540. formatterParams.$vars.push(key);
  541. }
  542. });
  543. }
  544. var ecData = getECData(opt.el);
  545. ecData.componentMainType = mainType;
  546. ecData.componentIndex = componentIndex;
  547. ecData.tooltipConfig = {
  548. name: itemName,
  549. option: defaults({
  550. content: itemName,
  551. encodeHTMLContent: true,
  552. formatterParams: formatterParams
  553. }, itemTooltipOptionObj)
  554. };
  555. }
  556. function traverseElement(el, cb) {
  557. var stopped;
  558. // TODO
  559. // Polyfill for fixing zrender group traverse don't visit it's root issue.
  560. if (el.isGroup) {
  561. stopped = cb(el);
  562. }
  563. if (!stopped) {
  564. el.traverse(cb);
  565. }
  566. }
  567. export function traverseElements(els, cb) {
  568. if (els) {
  569. if (isArray(els)) {
  570. for (var i = 0; i < els.length; i++) {
  571. traverseElement(els[i], cb);
  572. }
  573. } else {
  574. traverseElement(els, cb);
  575. }
  576. }
  577. }
  578. /**
  579. * After a boundingRect applying a `transform`, whether to be still parallel screen X and Y.
  580. */
  581. export function isBoundingRectAxisAligned(transform) {
  582. return !transform || mathAbs(transform[1]) < AXIS_ALIGN_EPSILON && mathAbs(transform[2]) < AXIS_ALIGN_EPSILON || mathAbs(transform[0]) < AXIS_ALIGN_EPSILON && mathAbs(transform[3]) < AXIS_ALIGN_EPSILON;
  583. }
  584. var AXIS_ALIGN_EPSILON = 1e-5;
  585. /**
  586. * Create or copy to the existing bounding rect to avoid modifying `source`.
  587. *
  588. * @usage
  589. * out.rect = ensureCopyRect(out.rect, sourceRect);
  590. */
  591. export function ensureCopyRect(target, source) {
  592. return target ? BoundingRect.copy(target, source) : source.clone();
  593. }
  594. /**
  595. * Create or copy to the existing transform to avoid modifying `source`.
  596. *
  597. * [CAUTION]: transform is `NullUndefined` if no transform, following convention of zrender,
  598. * and enable to bypass some unnecessary calculation, since in most cases there is no transform.
  599. *
  600. * @usage
  601. * out.transform = ensureCopyTransform(out.transform, sourceTransform);
  602. */
  603. export function ensureCopyTransform(target, source) {
  604. return source ? matrix.copy(target || matrix.create(), source) : undefined;
  605. }
  606. export function retrieveZInfo(model) {
  607. return {
  608. z: model.get('z') || 0,
  609. zlevel: model.get('zlevel') || 0
  610. };
  611. }
  612. /**
  613. * Assume all of the elements has the same `z` and `zlevel`.
  614. */
  615. export function calcZ2Range(el) {
  616. var max = -Infinity;
  617. var min = Infinity;
  618. traverseElement(el, function (el) {
  619. visitEl(el);
  620. visitEl(el.getTextContent());
  621. visitEl(el.getTextGuideLine());
  622. });
  623. function visitEl(el) {
  624. if (!el || el.isGroup) {
  625. return;
  626. }
  627. var currentStates = el.currentStates;
  628. if (currentStates.length) {
  629. for (var idx = 0; idx < currentStates.length; idx++) {
  630. calcZ2(el.states[currentStates[idx]]);
  631. }
  632. }
  633. calcZ2(el);
  634. }
  635. function calcZ2(entity) {
  636. if (entity) {
  637. var z2 = entity.z2;
  638. // Consider z2 may be NullUndefined
  639. if (z2 > max) {
  640. max = z2;
  641. }
  642. if (z2 < min) {
  643. min = z2;
  644. }
  645. }
  646. }
  647. if (min > max) {
  648. min = max = 0;
  649. }
  650. return {
  651. min: min,
  652. max: max
  653. };
  654. }
  655. export function traverseUpdateZ(el, z, zlevel) {
  656. doUpdateZ(el, z, zlevel, -Infinity);
  657. }
  658. function doUpdateZ(el, z, zlevel,
  659. // FIXME: Ideally all the labels should be above all the glyphs by default,
  660. // e.g. in graph, edge labels should be above node elements.
  661. // Currently impl does not guarantee that.
  662. maxZ2) {
  663. // `ignoreModelZ` is used to intentionally lift elements to cover other elements,
  664. // where maxZ2 (for label.z2) should also not be counted for its parents.
  665. if (el.ignoreModelZ) {
  666. return maxZ2;
  667. }
  668. // Group may also have textContent
  669. var label = el.getTextContent();
  670. var labelLine = el.getTextGuideLine();
  671. var isGroup = el.isGroup;
  672. if (isGroup) {
  673. // set z & zlevel of children elements of Group
  674. var children = el.childrenRef();
  675. for (var i = 0; i < children.length; i++) {
  676. maxZ2 = mathMax(doUpdateZ(children[i], z, zlevel, maxZ2), maxZ2);
  677. }
  678. } else {
  679. // not Group
  680. el.z = z;
  681. el.zlevel = zlevel;
  682. maxZ2 = mathMax(el.z2 || 0, maxZ2);
  683. }
  684. // always set z and zlevel if label/labelLine exists
  685. if (label) {
  686. label.z = z;
  687. label.zlevel = zlevel;
  688. // lift z2 of text content
  689. // TODO if el.emphasis.z2 is spcefied, what about textContent.
  690. isFinite(maxZ2) && (label.z2 = maxZ2 + 2);
  691. }
  692. if (labelLine) {
  693. var textGuideLineConfig = el.textGuideLineConfig;
  694. labelLine.z = z;
  695. labelLine.zlevel = zlevel;
  696. isFinite(maxZ2) && (labelLine.z2 = maxZ2 + (textGuideLineConfig && textGuideLineConfig.showAbove ? 1 : -1));
  697. }
  698. return maxZ2;
  699. }
  700. // Register built-in shapes. These shapes might be overwritten
  701. // by users, although we do not recommend that.
  702. registerShape('circle', Circle);
  703. registerShape('ellipse', Ellipse);
  704. registerShape('sector', Sector);
  705. registerShape('ring', Ring);
  706. registerShape('polygon', Polygon);
  707. registerShape('polyline', Polyline);
  708. registerShape('rect', Rect);
  709. registerShape('line', Line);
  710. registerShape('bezierCurve', BezierCurve);
  711. registerShape('arc', Arc);
  712. export { Group, ZRImage as Image, ZRText as Text, Circle, Ellipse, Sector, Ring, Polygon, Polyline, Rect, Line, BezierCurve, Arc, IncrementalDisplayable, CompoundPath, LinearGradient, RadialGradient, BoundingRect, OrientedBoundingRect, Point, Path };