123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- import * as pathTool from 'zrender/lib/tool/path.js';
- import * as matrix from 'zrender/lib/core/matrix.js';
- import * as vector from 'zrender/lib/core/vector.js';
- import Path from 'zrender/lib/graphic/Path.js';
- import Transformable from 'zrender/lib/core/Transformable.js';
- import ZRImage from 'zrender/lib/graphic/Image.js';
- import Group from 'zrender/lib/graphic/Group.js';
- import ZRText from 'zrender/lib/graphic/Text.js';
- import Circle from 'zrender/lib/graphic/shape/Circle.js';
- import Ellipse from 'zrender/lib/graphic/shape/Ellipse.js';
- import Sector from 'zrender/lib/graphic/shape/Sector.js';
- import Ring from 'zrender/lib/graphic/shape/Ring.js';
- import Polygon from 'zrender/lib/graphic/shape/Polygon.js';
- import Polyline from 'zrender/lib/graphic/shape/Polyline.js';
- import Rect from 'zrender/lib/graphic/shape/Rect.js';
- import Line from 'zrender/lib/graphic/shape/Line.js';
- import BezierCurve from 'zrender/lib/graphic/shape/BezierCurve.js';
- import Arc from 'zrender/lib/graphic/shape/Arc.js';
- import CompoundPath from 'zrender/lib/graphic/CompoundPath.js';
- import LinearGradient from 'zrender/lib/graphic/LinearGradient.js';
- import RadialGradient from 'zrender/lib/graphic/RadialGradient.js';
- import BoundingRect from 'zrender/lib/core/BoundingRect.js';
- import OrientedBoundingRect from 'zrender/lib/core/OrientedBoundingRect.js';
- import Point from 'zrender/lib/core/Point.js';
- import IncrementalDisplayable from 'zrender/lib/graphic/IncrementalDisplayable.js';
- import * as subPixelOptimizeUtil from 'zrender/lib/graphic/helper/subPixelOptimize.js';
- import { extend, isArrayLike, map, defaults, isString, keys, each, hasOwn, isArray } from 'zrender/lib/core/util.js';
- import { getECData } from './innerStore.js';
- import { updateProps, initProps, removeElement, removeElementWithFadeOut, isElementRemoved } from '../animation/basicTransition.js';
- export { updateProps, initProps, removeElement, removeElementWithFadeOut, isElementRemoved };
- var mathMax = Math.max;
- var mathMin = Math.min;
- var _customShapeMap = {};
- export function extendShape(opts) {
- return Path.extend(opts);
- }
- var extendPathFromString = pathTool.extendFromString;
- export function extendPath(pathData, opts) {
- return extendPathFromString(pathData, opts);
- }
- export function registerShape(name, ShapeClass) {
- _customShapeMap[name] = ShapeClass;
- }
- export function getShapeClass(name) {
- if (_customShapeMap.hasOwnProperty(name)) {
- return _customShapeMap[name];
- }
- }
- export function makePath(pathData, opts, rect, layout) {
- var path = pathTool.createFromString(pathData, opts);
- if (rect) {
- if (layout === 'center') {
- rect = centerGraphic(rect, path.getBoundingRect());
- }
- resizePath(path, rect);
- }
- return path;
- }
- export function makeImage(imageUrl, rect, layout) {
- var zrImg = new ZRImage({
- style: {
- image: imageUrl,
- x: rect.x,
- y: rect.y,
- width: rect.width,
- height: rect.height
- },
- onload: function (img) {
- if (layout === 'center') {
- var boundingRect = {
- width: img.width,
- height: img.height
- };
- zrImg.setStyle(centerGraphic(rect, boundingRect));
- }
- }
- });
- return zrImg;
- }
- function centerGraphic(rect, boundingRect) {
-
- var aspect = boundingRect.width / boundingRect.height;
- var width = rect.height * aspect;
- var height;
- if (width <= rect.width) {
- height = rect.height;
- } else {
- width = rect.width;
- height = width / aspect;
- }
- var cx = rect.x + rect.width / 2;
- var cy = rect.y + rect.height / 2;
- return {
- x: cx - width / 2,
- y: cy - height / 2,
- width: width,
- height: height
- };
- }
- export var mergePath = pathTool.mergePath;
- export function resizePath(path, rect) {
- if (!path.applyTransform) {
- return;
- }
- var pathRect = path.getBoundingRect();
- var m = pathRect.calculateTransform(rect);
- path.applyTransform(m);
- }
- export function subPixelOptimizeLine(shape, lineWidth) {
- subPixelOptimizeUtil.subPixelOptimizeLine(shape, shape, {
- lineWidth: lineWidth
- });
- return shape;
- }
- export function subPixelOptimizeRect(param) {
- subPixelOptimizeUtil.subPixelOptimizeRect(param.shape, param.shape, param.style);
- return param;
- }
- export var subPixelOptimize = subPixelOptimizeUtil.subPixelOptimize;
- export function getTransform(target, ancestor) {
- var mat = matrix.identity([]);
- while (target && target !== ancestor) {
- matrix.mul(mat, target.getLocalTransform(), mat);
- target = target.parent;
- }
- return mat;
- }
- export function applyTransform(target, transform, invert) {
- if (transform && !isArrayLike(transform)) {
- transform = Transformable.getLocalTransform(transform);
- }
- if (invert) {
- transform = matrix.invert([], transform);
- }
- return vector.applyTransform([], target, transform);
- }
- export function transformDirection(direction, transform, invert) {
-
- var hBase = transform[4] === 0 || transform[5] === 0 || transform[0] === 0 ? 1 : Math.abs(2 * transform[4] / transform[0]);
- var vBase = transform[4] === 0 || transform[5] === 0 || transform[2] === 0 ? 1 : Math.abs(2 * transform[4] / transform[2]);
- var vertex = [direction === 'left' ? -hBase : direction === 'right' ? hBase : 0, direction === 'top' ? -vBase : direction === 'bottom' ? vBase : 0];
- vertex = applyTransform(vertex, transform, invert);
- return Math.abs(vertex[0]) > Math.abs(vertex[1]) ? vertex[0] > 0 ? 'right' : 'left' : vertex[1] > 0 ? 'bottom' : 'top';
- }
- function isNotGroup(el) {
- return !el.isGroup;
- }
- function isPath(el) {
- return el.shape != null;
- }
- export function groupTransition(g1, g2, animatableModel) {
- if (!g1 || !g2) {
- return;
- }
- function getElMap(g) {
- var elMap = {};
- g.traverse(function (el) {
- if (isNotGroup(el) && el.anid) {
- elMap[el.anid] = el;
- }
- });
- return elMap;
- }
- function getAnimatableProps(el) {
- var obj = {
- x: el.x,
- y: el.y,
- rotation: el.rotation
- };
- if (isPath(el)) {
- obj.shape = extend({}, el.shape);
- }
- return obj;
- }
- var elMap1 = getElMap(g1);
- g2.traverse(function (el) {
- if (isNotGroup(el) && el.anid) {
- var oldEl = elMap1[el.anid];
- if (oldEl) {
- var newProp = getAnimatableProps(el);
- el.attr(getAnimatableProps(oldEl));
- updateProps(el, newProp, animatableModel, getECData(el).dataIndex);
- }
- }
- });
- }
- export function clipPointsByRect(points, rect) {
-
-
- return map(points, function (point) {
- var x = point[0];
- x = mathMax(x, rect.x);
- x = mathMin(x, rect.x + rect.width);
- var y = point[1];
- y = mathMax(y, rect.y);
- y = mathMin(y, rect.y + rect.height);
- return [x, y];
- });
- }
- export function clipRectByRect(targetRect, rect) {
- var x = mathMax(targetRect.x, rect.x);
- var x2 = mathMin(targetRect.x + targetRect.width, rect.x + rect.width);
- var y = mathMax(targetRect.y, rect.y);
- var y2 = mathMin(targetRect.y + targetRect.height, rect.y + rect.height);
-
-
- if (x2 >= x && y2 >= y) {
- return {
- x: x,
- y: y,
- width: x2 - x,
- height: y2 - y
- };
- }
- }
- export function createIcon(iconStr,
- // Support 'image://' or 'path://' or direct svg path.
- opt, rect) {
- var innerOpts = extend({
- rectHover: true
- }, opt);
- var style = innerOpts.style = {
- strokeNoScale: true
- };
- rect = rect || {
- x: -1,
- y: -1,
- width: 2,
- height: 2
- };
- if (iconStr) {
- return iconStr.indexOf('image://') === 0 ? (style.image = iconStr.slice(8), defaults(style, rect), new ZRImage(innerOpts)) : makePath(iconStr.replace('path://', ''), innerOpts, rect, 'center');
- }
- }
- export function linePolygonIntersect(a1x, a1y, a2x, a2y, points) {
- for (var i = 0, p2 = points[points.length - 1]; i < points.length; i++) {
- var p = points[i];
- if (lineLineIntersect(a1x, a1y, a2x, a2y, p[0], p[1], p2[0], p2[1])) {
- return true;
- }
- p2 = p;
- }
- }
- export function lineLineIntersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y) {
-
- var mx = a2x - a1x;
- var my = a2y - a1y;
- var nx = b2x - b1x;
- var ny = b2y - b1y;
-
-
- var nmCrossProduct = crossProduct2d(nx, ny, mx, my);
- if (nearZero(nmCrossProduct)) {
- return false;
- }
-
-
-
-
- var b1a1x = a1x - b1x;
- var b1a1y = a1y - b1y;
- var q = crossProduct2d(b1a1x, b1a1y, mx, my) / nmCrossProduct;
- if (q < 0 || q > 1) {
- return false;
- }
- var p = crossProduct2d(b1a1x, b1a1y, nx, ny) / nmCrossProduct;
- if (p < 0 || p > 1) {
- return false;
- }
- return true;
- }
- function crossProduct2d(x1, y1, x2, y2) {
- return x1 * y2 - x2 * y1;
- }
- function nearZero(val) {
- return val <= 1e-6 && val >= -1e-6;
- }
- export function setTooltipConfig(opt) {
- var itemTooltipOption = opt.itemTooltipOption;
- var componentModel = opt.componentModel;
- var itemName = opt.itemName;
- var itemTooltipOptionObj = isString(itemTooltipOption) ? {
- formatter: itemTooltipOption
- } : itemTooltipOption;
- var mainType = componentModel.mainType;
- var componentIndex = componentModel.componentIndex;
- var formatterParams = {
- componentType: mainType,
- name: itemName,
- $vars: ['name']
- };
- formatterParams[mainType + 'Index'] = componentIndex;
- var formatterParamsExtra = opt.formatterParamsExtra;
- if (formatterParamsExtra) {
- each(keys(formatterParamsExtra), function (key) {
- if (!hasOwn(formatterParams, key)) {
- formatterParams[key] = formatterParamsExtra[key];
- formatterParams.$vars.push(key);
- }
- });
- }
- var ecData = getECData(opt.el);
- ecData.componentMainType = mainType;
- ecData.componentIndex = componentIndex;
- ecData.tooltipConfig = {
- name: itemName,
- option: defaults({
- content: itemName,
- encodeHTMLContent: true,
- formatterParams: formatterParams
- }, itemTooltipOptionObj)
- };
- }
- function traverseElement(el, cb) {
- var stopped;
-
-
- if (el.isGroup) {
- stopped = cb(el);
- }
- if (!stopped) {
- el.traverse(cb);
- }
- }
- export function traverseElements(els, cb) {
- if (els) {
- if (isArray(els)) {
- for (var i = 0; i < els.length; i++) {
- traverseElement(els[i], cb);
- }
- } else {
- traverseElement(els, cb);
- }
- }
- }
- registerShape('circle', Circle);
- registerShape('ellipse', Ellipse);
- registerShape('sector', Sector);
- registerShape('ring', Ring);
- registerShape('polygon', Polygon);
- registerShape('polyline', Polyline);
- registerShape('rect', Rect);
- registerShape('line', Line);
- registerShape('bezierCurve', BezierCurve);
- registerShape('arc', Arc);
- 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 };
|