MapDraw.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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';
  41. import RoamController from './RoamController';
  42. import * as roamHelper from '../../component/helper/roamHelper';
  43. import { onIrrelevantElement } from '../../component/helper/cursorHelper';
  44. import * as graphic from '../../util/graphic';
  45. import { enableHoverEmphasis, enableComponentHighDownFeatures, setDefaultStateProxy } from '../../util/states';
  46. import geoSourceManager from '../../coord/geo/geoSourceManager';
  47. import { getUID } from '../../util/component';
  48. import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle';
  49. import { getECData } from '../../util/innerStore';
  50. import { createOrUpdatePatternFromDecal } from '../../util/decal';
  51. import Displayable from 'zrender/lib/graphic/Displayable';
  52. import { makeInner } from '../../util/model';
  53. /**
  54. * Only these tags enable use `itemStyle` if they are named in SVG.
  55. * Other tags like <text> <tspan> <image> might not suitable for `itemStyle`.
  56. * They will not be considered to be styled until some requirements come.
  57. */
  58. var OPTION_STYLE_ENABLED_TAGS = ['rect', 'circle', 'line', 'ellipse', 'polygon', 'polyline', 'path'];
  59. var OPTION_STYLE_ENABLED_TAG_MAP = zrUtil.createHashMap(OPTION_STYLE_ENABLED_TAGS);
  60. var STATE_TRIGGER_TAG_MAP = zrUtil.createHashMap(OPTION_STYLE_ENABLED_TAGS.concat(['g']));
  61. var LABEL_HOST_MAP = zrUtil.createHashMap(OPTION_STYLE_ENABLED_TAGS.concat(['g']));
  62. var mapLabelRaw = makeInner();
  63. function getFixedItemStyle(model) {
  64. var itemStyle = model.getItemStyle();
  65. var areaColor = model.get('areaColor'); // If user want the color not to be changed when hover,
  66. // they should both set areaColor and color to be null.
  67. if (areaColor != null) {
  68. itemStyle.fill = areaColor;
  69. }
  70. return itemStyle;
  71. }
  72. var MapDraw =
  73. /** @class */
  74. function () {
  75. function MapDraw(api) {
  76. var group = new graphic.Group();
  77. this.uid = getUID('ec_map_draw');
  78. this._controller = new RoamController(api.getZr());
  79. this._controllerHost = {
  80. target: group
  81. };
  82. this.group = group;
  83. group.add(this._regionsGroup = new graphic.Group());
  84. group.add(this._svgGroup = new graphic.Group());
  85. }
  86. MapDraw.prototype.draw = function (mapOrGeoModel, ecModel, api, fromView, payload) {
  87. var isGeo = mapOrGeoModel.mainType === 'geo'; // Map series has data. GEO model that controlled by map series
  88. // will be assigned with map data. Other GEO model has no data.
  89. var data = mapOrGeoModel.getData && mapOrGeoModel.getData();
  90. isGeo && ecModel.eachComponent({
  91. mainType: 'series',
  92. subType: 'map'
  93. }, function (mapSeries) {
  94. if (!data && mapSeries.getHostGeoModel() === mapOrGeoModel) {
  95. data = mapSeries.getData();
  96. }
  97. });
  98. var geo = mapOrGeoModel.coordinateSystem;
  99. var regionsGroup = this._regionsGroup;
  100. var group = this.group;
  101. var transformInfo = geo.getTransformInfo();
  102. var transformInfoRaw = transformInfo.raw;
  103. var transformInfoRoam = transformInfo.roam; // No animation when first draw or in action
  104. var isFirstDraw = !regionsGroup.childAt(0) || payload;
  105. if (isFirstDraw) {
  106. group.x = transformInfoRoam.x;
  107. group.y = transformInfoRoam.y;
  108. group.scaleX = transformInfoRoam.scaleX;
  109. group.scaleY = transformInfoRoam.scaleY;
  110. group.dirty();
  111. } else {
  112. graphic.updateProps(group, transformInfoRoam, mapOrGeoModel);
  113. }
  114. var isVisualEncodedByVisualMap = data && data.getVisual('visualMeta') && data.getVisual('visualMeta').length > 0;
  115. var viewBuildCtx = {
  116. api: api,
  117. geo: geo,
  118. mapOrGeoModel: mapOrGeoModel,
  119. data: data,
  120. isVisualEncodedByVisualMap: isVisualEncodedByVisualMap,
  121. isGeo: isGeo,
  122. transformInfoRaw: transformInfoRaw
  123. };
  124. if (geo.resourceType === 'geoJSON') {
  125. this._buildGeoJSON(viewBuildCtx);
  126. } else if (geo.resourceType === 'geoSVG') {
  127. this._buildSVG(viewBuildCtx);
  128. }
  129. this._updateController(mapOrGeoModel, ecModel, api);
  130. this._updateMapSelectHandler(mapOrGeoModel, regionsGroup, api, fromView);
  131. };
  132. MapDraw.prototype._buildGeoJSON = function (viewBuildCtx) {
  133. var regionsGroupByName = this._regionsGroupByName = zrUtil.createHashMap();
  134. var regionsInfoByName = zrUtil.createHashMap();
  135. var regionsGroup = this._regionsGroup;
  136. var transformInfoRaw = viewBuildCtx.transformInfoRaw;
  137. var mapOrGeoModel = viewBuildCtx.mapOrGeoModel;
  138. var data = viewBuildCtx.data;
  139. var transformPoint = function (point) {
  140. return [point[0] * transformInfoRaw.scaleX + transformInfoRaw.x, point[1] * transformInfoRaw.scaleY + transformInfoRaw.y];
  141. };
  142. regionsGroup.removeAll(); // Only when the resource is GeoJSON, there is `geo.regions`.
  143. zrUtil.each(viewBuildCtx.geo.regions, function (region) {
  144. var regionName = region.name; // Consider in GeoJson properties.name may be duplicated, for example,
  145. // there is multiple region named "United Kindom" or "France" (so many
  146. // colonies). And it is not appropriate to merge them in geo, which
  147. // will make them share the same label and bring trouble in label
  148. // location calculation.
  149. var regionGroup = regionsGroupByName.get(regionName);
  150. var _a = regionsInfoByName.get(regionName) || {},
  151. dataIdx = _a.dataIdx,
  152. regionModel = _a.regionModel;
  153. if (!regionGroup) {
  154. regionGroup = regionsGroupByName.set(regionName, new graphic.Group());
  155. regionsGroup.add(regionGroup);
  156. dataIdx = data ? data.indexOfName(regionName) : null;
  157. regionModel = viewBuildCtx.isGeo ? mapOrGeoModel.getRegionModel(regionName) : data ? data.getItemModel(dataIdx) : null;
  158. regionsInfoByName.set(regionName, {
  159. dataIdx: dataIdx,
  160. regionModel: regionModel
  161. });
  162. }
  163. var compoundPath = new graphic.CompoundPath({
  164. segmentIgnoreThreshold: 1,
  165. shape: {
  166. paths: []
  167. }
  168. });
  169. regionGroup.add(compoundPath);
  170. zrUtil.each(region.geometries, function (geometry) {
  171. if (geometry.type !== 'polygon') {
  172. return;
  173. }
  174. var points = [];
  175. for (var i = 0; i < geometry.exterior.length; ++i) {
  176. points.push(transformPoint(geometry.exterior[i]));
  177. }
  178. compoundPath.shape.paths.push(new graphic.Polygon({
  179. segmentIgnoreThreshold: 1,
  180. shape: {
  181. points: points
  182. }
  183. }));
  184. for (var i = 0; i < (geometry.interiors ? geometry.interiors.length : 0); ++i) {
  185. var interior = geometry.interiors[i];
  186. var points_1 = [];
  187. for (var j = 0; j < interior.length; ++j) {
  188. points_1.push(transformPoint(interior[j]));
  189. }
  190. compoundPath.shape.paths.push(new graphic.Polygon({
  191. segmentIgnoreThreshold: 1,
  192. shape: {
  193. points: points_1
  194. }
  195. }));
  196. }
  197. });
  198. applyOptionStyleForRegion(viewBuildCtx, compoundPath, dataIdx, regionModel);
  199. if (compoundPath instanceof Displayable) {
  200. compoundPath.culling = true;
  201. }
  202. var centerPt = transformPoint(region.getCenter());
  203. resetLabelForRegion(viewBuildCtx, compoundPath, regionName, regionModel, mapOrGeoModel, dataIdx, centerPt);
  204. }); // Ensure children have been added to `regionGroup` before calling them.
  205. regionsGroupByName.each(function (regionGroup, regionName) {
  206. var _a = regionsInfoByName.get(regionName),
  207. dataIdx = _a.dataIdx,
  208. regionModel = _a.regionModel;
  209. resetEventTriggerForRegion(viewBuildCtx, regionGroup, regionName, regionModel, mapOrGeoModel, dataIdx);
  210. resetTooltipForRegion(viewBuildCtx, regionGroup, regionName, regionModel, mapOrGeoModel);
  211. resetStateTriggerForRegion(viewBuildCtx, regionGroup, regionName, regionModel, mapOrGeoModel);
  212. }, this);
  213. };
  214. MapDraw.prototype._buildSVG = function (viewBuildCtx) {
  215. var mapName = viewBuildCtx.geo.map;
  216. var transformInfoRaw = viewBuildCtx.transformInfoRaw;
  217. this._svgGroup.x = transformInfoRaw.x;
  218. this._svgGroup.y = transformInfoRaw.y;
  219. this._svgGroup.scaleX = transformInfoRaw.scaleX;
  220. this._svgGroup.scaleY = transformInfoRaw.scaleY;
  221. if (this._svgResourceChanged(mapName)) {
  222. this._freeSVG();
  223. this._useSVG(mapName);
  224. }
  225. var svgDispatcherMap = this._svgDispatcherMap = zrUtil.createHashMap();
  226. var focusSelf = false;
  227. zrUtil.each(this._svgGraphicRecord.named, function (namedItem) {
  228. // Note that we also allow different elements have the same name.
  229. // For example, a glyph of a city and the label of the city have
  230. // the same name and their tooltip info can be defined in a single
  231. // region option.
  232. var regionName = namedItem.name;
  233. var mapOrGeoModel = viewBuildCtx.mapOrGeoModel;
  234. var data = viewBuildCtx.data;
  235. var svgNodeTagLower = namedItem.svgNodeTagLower;
  236. var el = namedItem.el;
  237. var dataIdx = data ? data.indexOfName(regionName) : null;
  238. var regionModel = mapOrGeoModel.getRegionModel(regionName);
  239. if (OPTION_STYLE_ENABLED_TAG_MAP.get(svgNodeTagLower) != null && el instanceof Displayable) {
  240. applyOptionStyleForRegion(viewBuildCtx, el, dataIdx, regionModel);
  241. }
  242. if (el instanceof Displayable) {
  243. el.culling = true;
  244. } // We do not know how the SVG like so we'd better not to change z2.
  245. // Otherwise it might bring some unexpected result. For example,
  246. // an area hovered that make some inner city can not be clicked.
  247. el.z2EmphasisLift = 0; // If self named:
  248. if (!namedItem.namedFrom) {
  249. // label should batter to be displayed based on the center of <g>
  250. // if it is named rather than displayed on each child.
  251. if (LABEL_HOST_MAP.get(svgNodeTagLower) != null) {
  252. resetLabelForRegion(viewBuildCtx, el, regionName, regionModel, mapOrGeoModel, dataIdx, null);
  253. }
  254. resetEventTriggerForRegion(viewBuildCtx, el, regionName, regionModel, mapOrGeoModel, dataIdx);
  255. resetTooltipForRegion(viewBuildCtx, el, regionName, regionModel, mapOrGeoModel);
  256. if (STATE_TRIGGER_TAG_MAP.get(svgNodeTagLower) != null) {
  257. var focus_1 = resetStateTriggerForRegion(viewBuildCtx, el, regionName, regionModel, mapOrGeoModel);
  258. if (focus_1 === 'self') {
  259. focusSelf = true;
  260. }
  261. var els = svgDispatcherMap.get(regionName) || svgDispatcherMap.set(regionName, []);
  262. els.push(el);
  263. }
  264. }
  265. }, this);
  266. this._enableBlurEntireSVG(focusSelf, viewBuildCtx);
  267. };
  268. MapDraw.prototype._enableBlurEntireSVG = function (focusSelf, viewBuildCtx) {
  269. // It's a little complicated to support blurring the entire geoSVG in series-map.
  270. // So do not suport it until some requirements come.
  271. // At present, in series-map, only regions can be blurred.
  272. if (focusSelf && viewBuildCtx.isGeo) {
  273. var blurStyle = viewBuildCtx.mapOrGeoModel.getModel(['blur', 'itemStyle']).getItemStyle(); // Only suport `opacity` here. Because not sure that other props are suitable for
  274. // all of the elements generated by SVG (especially for Text/TSpan/Image/... ).
  275. var opacity_1 = blurStyle.opacity;
  276. this._svgGraphicRecord.root.traverse(function (el) {
  277. if (!el.isGroup) {
  278. // PENDING: clear those settings to SVG elements when `_freeSVG`.
  279. // (Currently it happen not to be needed.)
  280. setDefaultStateProxy(el);
  281. var style = el.ensureState('blur').style || {}; // Do not overwrite the region style that already set from region option.
  282. if (style.opacity == null && opacity_1 != null) {
  283. style.opacity = opacity_1;
  284. } // If `ensureState('blur').style = {}`, there will be default opacity.
  285. // Enable `stateTransition` (animation).
  286. el.ensureState('emphasis');
  287. }
  288. });
  289. }
  290. };
  291. MapDraw.prototype.remove = function () {
  292. this._regionsGroup.removeAll();
  293. this._regionsGroupByName = null;
  294. this._svgGroup.removeAll();
  295. this._freeSVG();
  296. this._controller.dispose();
  297. this._controllerHost = null;
  298. };
  299. MapDraw.prototype.findHighDownDispatchers = function (name, geoModel) {
  300. if (name == null) {
  301. return [];
  302. }
  303. var geo = geoModel.coordinateSystem;
  304. if (geo.resourceType === 'geoJSON') {
  305. var regionsGroupByName = this._regionsGroupByName;
  306. if (regionsGroupByName) {
  307. var regionGroup = regionsGroupByName.get(name);
  308. return regionGroup ? [regionGroup] : [];
  309. }
  310. } else if (geo.resourceType === 'geoSVG') {
  311. return this._svgDispatcherMap && this._svgDispatcherMap.get(name) || [];
  312. }
  313. };
  314. MapDraw.prototype._svgResourceChanged = function (mapName) {
  315. return this._svgMapName !== mapName;
  316. };
  317. MapDraw.prototype._useSVG = function (mapName) {
  318. var resource = geoSourceManager.getGeoResource(mapName);
  319. if (resource && resource.type === 'geoSVG') {
  320. var svgGraphic = resource.useGraphic(this.uid);
  321. this._svgGroup.add(svgGraphic.root);
  322. this._svgGraphicRecord = svgGraphic;
  323. this._svgMapName = mapName;
  324. }
  325. };
  326. MapDraw.prototype._freeSVG = function () {
  327. var mapName = this._svgMapName;
  328. if (mapName == null) {
  329. return;
  330. }
  331. var resource = geoSourceManager.getGeoResource(mapName);
  332. if (resource && resource.type === 'geoSVG') {
  333. resource.freeGraphic(this.uid);
  334. }
  335. this._svgGraphicRecord = null;
  336. this._svgDispatcherMap = null;
  337. this._svgGroup.removeAll();
  338. this._svgMapName = null;
  339. };
  340. MapDraw.prototype._updateController = function (mapOrGeoModel, ecModel, api) {
  341. var geo = mapOrGeoModel.coordinateSystem;
  342. var controller = this._controller;
  343. var controllerHost = this._controllerHost; // @ts-ignore FIXME:TS
  344. controllerHost.zoomLimit = mapOrGeoModel.get('scaleLimit');
  345. controllerHost.zoom = geo.getZoom(); // roamType is will be set default true if it is null
  346. // @ts-ignore FIXME:TS
  347. controller.enable(mapOrGeoModel.get('roam') || false);
  348. var mainType = mapOrGeoModel.mainType;
  349. function makeActionBase() {
  350. var action = {
  351. type: 'geoRoam',
  352. componentType: mainType
  353. };
  354. action[mainType + 'Id'] = mapOrGeoModel.id;
  355. return action;
  356. }
  357. controller.off('pan').on('pan', function (e) {
  358. this._mouseDownFlag = false;
  359. roamHelper.updateViewOnPan(controllerHost, e.dx, e.dy);
  360. api.dispatchAction(zrUtil.extend(makeActionBase(), {
  361. dx: e.dx,
  362. dy: e.dy
  363. }));
  364. }, this);
  365. controller.off('zoom').on('zoom', function (e) {
  366. this._mouseDownFlag = false;
  367. roamHelper.updateViewOnZoom(controllerHost, e.scale, e.originX, e.originY);
  368. api.dispatchAction(zrUtil.extend(makeActionBase(), {
  369. zoom: e.scale,
  370. originX: e.originX,
  371. originY: e.originY
  372. }));
  373. }, this);
  374. controller.setPointerChecker(function (e, x, y) {
  375. return geo.containPoint([x, y]) && !onIrrelevantElement(e, api, mapOrGeoModel);
  376. });
  377. };
  378. /**
  379. * FIXME: this is a temporarily workaround.
  380. * When `geoRoam` the elements need to be reset in `MapView['render']`, because the props like
  381. * `ignore` might have been modified by `LabelManager`, and `LabelManager#addLabelsOfSeries`
  382. * will subsequently cache `defaultAttr` like `ignore`. If do not do this reset, the modified
  383. * props will have no chance to be restored.
  384. * Note: this reset should be after `clearStates` in `renderSeries` becuase `useStates` in
  385. * `renderSeries` will cache the modified `ignore` to `el._normalState`.
  386. * TODO:
  387. * Use clone/immutable in `LabelManager`?
  388. */
  389. MapDraw.prototype.resetForLabelLayout = function () {
  390. this.group.traverse(function (el) {
  391. var label = el.getTextContent();
  392. if (label) {
  393. label.ignore = mapLabelRaw(label).ignore;
  394. }
  395. });
  396. };
  397. MapDraw.prototype._updateMapSelectHandler = function (mapOrGeoModel, regionsGroup, api, fromView) {
  398. var mapDraw = this;
  399. regionsGroup.off('mousedown');
  400. regionsGroup.off('click'); // @ts-ignore FIXME:TS resolve type conflict
  401. if (mapOrGeoModel.get('selectedMode')) {
  402. regionsGroup.on('mousedown', function () {
  403. mapDraw._mouseDownFlag = true;
  404. });
  405. regionsGroup.on('click', function (e) {
  406. if (!mapDraw._mouseDownFlag) {
  407. return;
  408. }
  409. mapDraw._mouseDownFlag = false;
  410. });
  411. }
  412. };
  413. return MapDraw;
  414. }();
  415. ;
  416. function applyOptionStyleForRegion(viewBuildCtx, el, dataIndex, regionModel) {
  417. // All of the path are using `itemStyle`, becuase
  418. // (1) Some SVG also use fill on polyline (The different between
  419. // polyline and polygon is "open" or "close" but not fill or not).
  420. // (2) For the common props like opacity, if some use itemStyle
  421. // and some use `lineStyle`, it might confuse users.
  422. // (3) Most SVG use <path>, where can not detect wether draw a "line"
  423. // or a filled shape, so use `itemStyle` for <path>.
  424. var normalStyleModel = regionModel.getModel('itemStyle');
  425. var emphasisStyleModel = regionModel.getModel(['emphasis', 'itemStyle']);
  426. var blurStyleModel = regionModel.getModel(['blur', 'itemStyle']);
  427. var selectStyleModel = regionModel.getModel(['select', 'itemStyle']); // NOTE: DONT use 'style' in visual when drawing map.
  428. // This component is used for drawing underlying map for both geo component and map series.
  429. var normalStyle = getFixedItemStyle(normalStyleModel);
  430. var emphasisStyle = getFixedItemStyle(emphasisStyleModel);
  431. var selectStyle = getFixedItemStyle(selectStyleModel);
  432. var blurStyle = getFixedItemStyle(blurStyleModel); // Update the itemStyle if has data visual
  433. var data = viewBuildCtx.data;
  434. if (data) {
  435. // Only visual color of each item will be used. It can be encoded by visualMap
  436. // But visual color of series is used in symbol drawing
  437. // Visual color for each series is for the symbol draw
  438. var style = data.getItemVisual(dataIndex, 'style');
  439. var decal = data.getItemVisual(dataIndex, 'decal');
  440. if (viewBuildCtx.isVisualEncodedByVisualMap && style.fill) {
  441. normalStyle.fill = style.fill;
  442. }
  443. if (decal) {
  444. normalStyle.decal = createOrUpdatePatternFromDecal(decal, viewBuildCtx.api);
  445. }
  446. } // SVG text, tspan and image can be named but not supporeted
  447. // to be styled by region option yet.
  448. el.setStyle(normalStyle);
  449. el.style.strokeNoScale = true;
  450. el.ensureState('emphasis').style = emphasisStyle;
  451. el.ensureState('select').style = selectStyle;
  452. el.ensureState('blur').style = blurStyle; // Enable blur
  453. setDefaultStateProxy(el);
  454. }
  455. function resetLabelForRegion(viewBuildCtx, el, regionName, regionModel, mapOrGeoModel, // Exist only if `viewBuildCtx.data` exists.
  456. dataIdx, // If labelXY not provided, use `textConfig.position: 'inside'`
  457. labelXY) {
  458. var data = viewBuildCtx.data;
  459. var isGeo = viewBuildCtx.isGeo;
  460. var isDataNaN = data && isNaN(data.get(data.mapDimension('value'), dataIdx));
  461. var itemLayout = data && data.getItemLayout(dataIdx); // In the following cases label will be drawn
  462. // 1. In map series and data value is NaN
  463. // 2. In geo component
  464. // 3. Region has no series legendIcon, which will be add a showLabel flag in mapSymbolLayout
  465. if (isGeo || isDataNaN || itemLayout && itemLayout.showLabel) {
  466. var query = !isGeo ? dataIdx : regionName;
  467. var labelFetcher = void 0; // Consider dataIdx not found.
  468. if (!data || dataIdx >= 0) {
  469. labelFetcher = mapOrGeoModel;
  470. }
  471. var specifiedTextOpt = labelXY ? {
  472. normal: {
  473. align: 'center',
  474. verticalAlign: 'middle'
  475. }
  476. } : null; // Caveat: must be called after `setDefaultStateProxy(el);` called.
  477. // because textContent will be assign with `el.stateProxy` inside.
  478. setLabelStyle(el, getLabelStatesModels(regionModel), {
  479. labelFetcher: labelFetcher,
  480. labelDataIndex: query,
  481. defaultText: regionName
  482. }, specifiedTextOpt);
  483. var textEl = el.getTextContent();
  484. if (textEl) {
  485. mapLabelRaw(textEl).ignore = textEl.ignore;
  486. if (el.textConfig && labelXY) {
  487. // Compute a relative offset based on the el bounding rect.
  488. var rect = el.getBoundingRect().clone(); // Need to make sure the percent position base on the same rect in normal and
  489. // emphasis state. Otherwise if using boundingRect of el, but the emphasis state
  490. // has borderWidth (even 0.5px), the text position will be changed obviously
  491. // if the position is very big like ['1234%', '1345%'].
  492. el.textConfig.layoutRect = rect;
  493. el.textConfig.position = [(labelXY[0] - rect.x) / rect.width * 100 + '%', (labelXY[1] - rect.y) / rect.height * 100 + '%'];
  494. }
  495. } // PENDING:
  496. // If labelLayout is enabled (test/label-layout.html), el.dataIndex should be specified.
  497. // But el.dataIndex is also used to determine whether user event should be triggered,
  498. // where el.seriesIndex or el.dataModel must be specified. At present for a single el
  499. // there is not case that "only label layout enabled but user event disabled", so here
  500. // we depends `resetEventTriggerForRegion` to do the job of setting `el.dataIndex`.
  501. el.disableLabelAnimation = true;
  502. } else {
  503. el.removeTextContent();
  504. el.removeTextConfig();
  505. el.disableLabelAnimation = null;
  506. }
  507. }
  508. function resetEventTriggerForRegion(viewBuildCtx, eventTrigger, regionName, regionModel, mapOrGeoModel, // Exist only if `viewBuildCtx.data` exists.
  509. dataIdx) {
  510. // setItemGraphicEl, setHoverStyle after all polygons and labels
  511. // are added to the rigionGroup
  512. if (viewBuildCtx.data) {
  513. // FIXME: when series-map use a SVG map, and there are duplicated name specified
  514. // on different SVG elements, after `data.setItemGraphicEl(...)`:
  515. // (1) all of them will be mounted with `dataIndex`, `seriesIndex`, so that tooltip
  516. // can be triggered only mouse hover. That's correct.
  517. // (2) only the last element will be kept in `data`, so that if trigger tooltip
  518. // by `dispatchAction`, only the last one can be found and triggered. That might be
  519. // not correct. We will fix it in future if anyone demanding that.
  520. viewBuildCtx.data.setItemGraphicEl(dataIdx, eventTrigger);
  521. } // series-map will not trigger "geoselectchange" no matter it is
  522. // based on a declared geo component. Becuause series-map will
  523. // trigger "selectchange". If it trigger both the two events,
  524. // If users call `chart.dispatchAction({type: 'toggleSelect'})`,
  525. // it not easy to also fire event "geoselectchanged".
  526. else {
  527. // Package custom mouse event for geo component
  528. getECData(eventTrigger).eventData = {
  529. componentType: 'geo',
  530. componentIndex: mapOrGeoModel.componentIndex,
  531. geoIndex: mapOrGeoModel.componentIndex,
  532. name: regionName,
  533. region: regionModel && regionModel.option || {}
  534. };
  535. }
  536. }
  537. function resetTooltipForRegion(viewBuildCtx, el, regionName, regionModel, mapOrGeoModel) {
  538. if (!viewBuildCtx.data) {
  539. graphic.setTooltipConfig({
  540. el: el,
  541. componentModel: mapOrGeoModel,
  542. itemName: regionName,
  543. // @ts-ignore FIXME:TS fix the "compatible with each other"?
  544. itemTooltipOption: regionModel.get('tooltip')
  545. });
  546. }
  547. }
  548. function resetStateTriggerForRegion(viewBuildCtx, el, regionName, regionModel, mapOrGeoModel) {
  549. // @ts-ignore FIXME:TS fix the "compatible with each other"?
  550. el.highDownSilentOnTouch = !!mapOrGeoModel.get('selectedMode'); // @ts-ignore FIXME:TS fix the "compatible with each other"?
  551. var emphasisModel = regionModel.getModel('emphasis');
  552. var focus = emphasisModel.get('focus');
  553. enableHoverEmphasis(el, focus, emphasisModel.get('blurScope'));
  554. if (viewBuildCtx.isGeo) {
  555. enableComponentHighDownFeatures(el, mapOrGeoModel, regionName);
  556. }
  557. return focus;
  558. }
  559. export default MapDraw; // @ts-ignore FIXME:TS fix the "compatible with each other"?