GeoSVGResource.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 { parseSVG, makeViewBoxTransform } from 'zrender/lib/tool/parseSVG.js';
  41. import Group from 'zrender/lib/graphic/Group.js';
  42. import Rect from 'zrender/lib/graphic/shape/Rect.js';
  43. import { assert, createHashMap, each } from 'zrender/lib/core/util.js';
  44. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  45. import { parseXML } from 'zrender/lib/tool/parseXML.js';
  46. import { GeoSVGRegion } from './Region.js';
  47. /**
  48. * "region available" means that: enable users to set attribute `name="xxx"` on those tags
  49. * to make it be a region.
  50. * 1. region styles and its label styles can be defined in echarts opton:
  51. * ```js
  52. * geo: {
  53. * regions: [{
  54. * name: 'xxx',
  55. * itemStyle: { ... },
  56. * label: { ... }
  57. * }, {
  58. * ...
  59. * },
  60. * ...]
  61. * };
  62. * ```
  63. * 2. name can be duplicated in different SVG tag. All of the tags with the same name share
  64. * a region option. For exampel if there are two <path> representing two lung lobes. They have
  65. * no common parents but both of them need to display label "lung" inside.
  66. */
  67. var REGION_AVAILABLE_SVG_TAG_MAP = createHashMap(['rect', 'circle', 'line', 'ellipse', 'polygon', 'polyline', 'path',
  68. // <text> <tspan> are also enabled because some SVG might paint text itself,
  69. // but still need to trigger events or tooltip.
  70. 'text', 'tspan',
  71. // <g> is also enabled because this case: if multiple tags share one name
  72. // and need label displayed, every tags will display the name, which is not
  73. // expected. So we can put them into a <g name="xxx">. Thereby only one label
  74. // displayed and located based on the bounding rect of the <g>.
  75. 'g']);
  76. var GeoSVGResource = /** @class */function () {
  77. function GeoSVGResource(mapName, svg) {
  78. this.type = 'geoSVG';
  79. // All used graphics. key: hostKey, value: root
  80. this._usedGraphicMap = createHashMap();
  81. // All unused graphics.
  82. this._freedGraphics = [];
  83. this._mapName = mapName;
  84. // Only perform parse to XML object here, which might be time
  85. // consiming for large SVG.
  86. // Although convert XML to zrender element is also time consiming,
  87. // if we do it here, the clone of zrender elements has to be
  88. // required. So we do it once for each geo instance, util real
  89. // performance issues call for optimizing it.
  90. this._parsedXML = parseXML(svg);
  91. }
  92. GeoSVGResource.prototype.load = function /* nameMap: NameMap */
  93. () {
  94. // In the "load" stage, graphic need to be built to
  95. // get boundingRect for geo coordinate system.
  96. var firstGraphic = this._firstGraphic;
  97. // Create the return data structure only when first graphic created.
  98. // Because they will be used in geo coordinate system update stage,
  99. // and `regions` will be mounted at `geo` coordinate system,
  100. // in which there is no "view" info, so that it should better not to
  101. // make references to graphic elements.
  102. if (!firstGraphic) {
  103. firstGraphic = this._firstGraphic = this._buildGraphic(this._parsedXML);
  104. this._freedGraphics.push(firstGraphic);
  105. this._boundingRect = this._firstGraphic.boundingRect.clone();
  106. // PENDING: `nameMap` will not be supported until some real requirement come.
  107. // if (nameMap) {
  108. // named = applyNameMap(named, nameMap);
  109. // }
  110. var _a = createRegions(firstGraphic.named),
  111. regions = _a.regions,
  112. regionsMap = _a.regionsMap;
  113. this._regions = regions;
  114. this._regionsMap = regionsMap;
  115. }
  116. return {
  117. boundingRect: this._boundingRect,
  118. regions: this._regions,
  119. regionsMap: this._regionsMap
  120. };
  121. };
  122. GeoSVGResource.prototype._buildGraphic = function (svgXML) {
  123. var result;
  124. var rootFromParse;
  125. try {
  126. result = svgXML && parseSVG(svgXML, {
  127. ignoreViewBox: true,
  128. ignoreRootClip: true
  129. }) || {};
  130. rootFromParse = result.root;
  131. assert(rootFromParse != null);
  132. } catch (e) {
  133. throw new Error('Invalid svg format\n' + e.message);
  134. }
  135. // Note: we keep the covenant that the root has no transform. So always add an extra root.
  136. var root = new Group();
  137. root.add(rootFromParse);
  138. root.isGeoSVGGraphicRoot = true;
  139. // [THE_RULE_OF_VIEWPORT_AND_VIEWBOX]
  140. //
  141. // Consider: `<svg width="..." height="..." viewBox="...">`
  142. // - the `width/height` we call it `svgWidth/svgHeight` for short.
  143. // - `(0, 0, svgWidth, svgHeight)` defines the viewport of the SVG, or say,
  144. // "viewport boundingRect", or `boundingRect` for short.
  145. // - `viewBox` defines the transform from the real content ot the viewport.
  146. // `viewBox` has the same unit as the content of SVG.
  147. // If `viewBox` exists, a transform is defined, so the unit of `svgWidth/svgHeight` become
  148. // different from the content of SVG. Otherwise, they are the same.
  149. //
  150. // If both `svgWidth/svgHeight/viewBox` are specified in a SVG file, the transform rule will be:
  151. // 0. `boundingRect` is `(0, 0, svgWidth, svgHeight)`. Set it to Geo['_rect'] (View['_rect']).
  152. // 1. Make a transform from `viewBox` to `boundingRect`.
  153. // Note: only support `preserveAspectRatio 'xMidYMid'` here. That is, this transform will preserve
  154. // the aspect ratio.
  155. // 2. Make a transform from boundingRect to Geo['_viewRect'] (View['_viewRect'])
  156. // (`Geo`/`View` will do this job).
  157. // Note: this transform might not preserve aspect radio, which depending on how users specify
  158. // viewRect in echarts option (e.g., `geo.left/top/width/height` will not preserve aspect ratio,
  159. // but `geo.layoutCenter/layoutSize` will preserve aspect ratio).
  160. //
  161. // If `svgWidth/svgHeight` not specified, we use `viewBox` as the `boundingRect` to make the SVG
  162. // layout look good.
  163. //
  164. // If neither `svgWidth/svgHeight` nor `viewBox` are not specified, we calculate the boundingRect
  165. // of the SVG content and use them to make SVG layout look good.
  166. var svgWidth = result.width;
  167. var svgHeight = result.height;
  168. var viewBoxRect = result.viewBoxRect;
  169. var boundingRect = this._boundingRect;
  170. if (!boundingRect) {
  171. var bRectX = void 0;
  172. var bRectY = void 0;
  173. var bRectWidth = void 0;
  174. var bRectHeight = void 0;
  175. if (svgWidth != null) {
  176. bRectX = 0;
  177. bRectWidth = svgWidth;
  178. } else if (viewBoxRect) {
  179. bRectX = viewBoxRect.x;
  180. bRectWidth = viewBoxRect.width;
  181. }
  182. if (svgHeight != null) {
  183. bRectY = 0;
  184. bRectHeight = svgHeight;
  185. } else if (viewBoxRect) {
  186. bRectY = viewBoxRect.y;
  187. bRectHeight = viewBoxRect.height;
  188. }
  189. // If both viewBox and svgWidth/svgHeight not specified,
  190. // we have to determine how to layout those element to make them look good.
  191. if (bRectX == null || bRectY == null) {
  192. var calculatedBoundingRect = rootFromParse.getBoundingRect();
  193. if (bRectX == null) {
  194. bRectX = calculatedBoundingRect.x;
  195. bRectWidth = calculatedBoundingRect.width;
  196. }
  197. if (bRectY == null) {
  198. bRectY = calculatedBoundingRect.y;
  199. bRectHeight = calculatedBoundingRect.height;
  200. }
  201. }
  202. boundingRect = this._boundingRect = new BoundingRect(bRectX, bRectY, bRectWidth, bRectHeight);
  203. }
  204. if (viewBoxRect) {
  205. var viewBoxTransform = makeViewBoxTransform(viewBoxRect, boundingRect);
  206. // Only support `preserveAspectRatio 'xMidYMid'`
  207. rootFromParse.scaleX = rootFromParse.scaleY = viewBoxTransform.scale;
  208. rootFromParse.x = viewBoxTransform.x;
  209. rootFromParse.y = viewBoxTransform.y;
  210. }
  211. // SVG needs to clip based on `viewBox`. And some SVG files really rely on this feature.
  212. // They do not strictly confine all of the content inside a display rect, but deliberately
  213. // use a `viewBox` to define a displayable rect.
  214. // PENDING:
  215. // The drawback of the `setClipPath` here is: the region label (genereted by echarts) near the
  216. // edge might also be clipped, because region labels are put as `textContent` of the SVG path.
  217. root.setClipPath(new Rect({
  218. shape: boundingRect.plain()
  219. }));
  220. var named = [];
  221. each(result.named, function (namedItem) {
  222. if (REGION_AVAILABLE_SVG_TAG_MAP.get(namedItem.svgNodeTagLower) != null) {
  223. named.push(namedItem);
  224. setSilent(namedItem.el);
  225. }
  226. });
  227. return {
  228. root: root,
  229. boundingRect: boundingRect,
  230. named: named
  231. };
  232. };
  233. /**
  234. * Consider:
  235. * (1) One graphic element can not be shared by different `geoView` running simultaneously.
  236. * Notice, also need to consider multiple echarts instances share a `mapRecord`.
  237. * (2) Converting SVG to graphic elements is time consuming.
  238. * (3) In the current architecture, `load` should be called frequently to get boundingRect,
  239. * and it is called without view info.
  240. * So we maintain graphic elements in this module, and enables `view` to use/return these
  241. * graphics from/to the pool with it's uid.
  242. */
  243. GeoSVGResource.prototype.useGraphic = function (hostKey /* , nameMap: NameMap */) {
  244. var usedRootMap = this._usedGraphicMap;
  245. var svgGraphic = usedRootMap.get(hostKey);
  246. if (svgGraphic) {
  247. return svgGraphic;
  248. }
  249. svgGraphic = this._freedGraphics.pop()
  250. // use the first boundingRect to avoid duplicated boundingRect calculation.
  251. || this._buildGraphic(this._parsedXML);
  252. usedRootMap.set(hostKey, svgGraphic);
  253. // PENDING: `nameMap` will not be supported until some real requirement come.
  254. // `nameMap` can only be obtained from echarts option.
  255. // The original `named` must not be modified.
  256. // if (nameMap) {
  257. // svgGraphic = extend({}, svgGraphic);
  258. // svgGraphic.named = applyNameMap(svgGraphic.named, nameMap);
  259. // }
  260. return svgGraphic;
  261. };
  262. GeoSVGResource.prototype.freeGraphic = function (hostKey) {
  263. var usedRootMap = this._usedGraphicMap;
  264. var svgGraphic = usedRootMap.get(hostKey);
  265. if (svgGraphic) {
  266. usedRootMap.removeKey(hostKey);
  267. this._freedGraphics.push(svgGraphic);
  268. }
  269. };
  270. return GeoSVGResource;
  271. }();
  272. export { GeoSVGResource };
  273. function setSilent(el) {
  274. // Only named element has silent: false, other elements should
  275. // act as background and has no user interaction.
  276. el.silent = false;
  277. // text|tspan will be converted to group.
  278. if (el.isGroup) {
  279. el.traverse(function (child) {
  280. child.silent = false;
  281. });
  282. }
  283. }
  284. function createRegions(named) {
  285. var regions = [];
  286. var regionsMap = createHashMap();
  287. // Create resions only for the first graphic.
  288. each(named, function (namedItem) {
  289. // Region has feature to calculate center for tooltip or other features.
  290. // If there is a <g name="xxx">, the center should be the center of the
  291. // bounding rect of the g.
  292. if (namedItem.namedFrom != null) {
  293. return;
  294. }
  295. var region = new GeoSVGRegion(namedItem.name, namedItem.el);
  296. // PENDING: if `nameMap` supported, this region can not be mounted on
  297. // `this`, but can only be created each time `load()` called.
  298. regions.push(region);
  299. // PENDING: if multiple tag named with the same name, only one will be
  300. // found by `_regionsMap`. `_regionsMap` is used to find a coordinate
  301. // by name. We use `region.getCenter()` as the coordinate.
  302. regionsMap.set(namedItem.name, region);
  303. });
  304. return {
  305. regions: regions,
  306. regionsMap: regionsMap
  307. };
  308. }
  309. // PENDING: `nameMap` will not be supported until some real requirement come.
  310. // /**
  311. // * Use the alias in geoNameMap.
  312. // * The input `named` must not be modified.
  313. // */
  314. // function applyNameMap(
  315. // named: GeoSVGGraphicRecord['named'],
  316. // nameMap: NameMap
  317. // ): GeoSVGGraphicRecord['named'] {
  318. // const result = [] as GeoSVGGraphicRecord['named'];
  319. // for (let i = 0; i < named.length; i++) {
  320. // let regionGraphic = named[i];
  321. // const name = regionGraphic.name;
  322. // if (nameMap && nameMap.hasOwnProperty(name)) {
  323. // regionGraphic = extend({}, regionGraphic);
  324. // regionGraphic.name = name;
  325. // }
  326. // result.push(regionGraphic);
  327. // }
  328. // return result;
  329. // }