geoCreator.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 Geo, { geo2DDimensions } from './Geo.js';
  42. import * as layout from '../../util/layout.js';
  43. import * as numberUtil from '../../util/number.js';
  44. import geoSourceManager from './geoSourceManager.js';
  45. import * as vector from 'zrender/lib/core/vector.js';
  46. /**
  47. * Resize method bound to the geo
  48. */
  49. function resizeGeo(geoModel, api) {
  50. var boundingCoords = geoModel.get('boundingCoords');
  51. if (boundingCoords != null) {
  52. var leftTop_1 = boundingCoords[0];
  53. var rightBottom_1 = boundingCoords[1];
  54. if (!(isFinite(leftTop_1[0]) && isFinite(leftTop_1[1]) && isFinite(rightBottom_1[0]) && isFinite(rightBottom_1[1]))) {
  55. if (process.env.NODE_ENV !== 'production') {
  56. console.error('Invalid boundingCoords');
  57. }
  58. } else {
  59. // Sample around the lng/lat rect and use projection to calculate actual bounding rect.
  60. var projection_1 = this.projection;
  61. if (projection_1) {
  62. var xMin = leftTop_1[0];
  63. var yMin = leftTop_1[1];
  64. var xMax = rightBottom_1[0];
  65. var yMax = rightBottom_1[1];
  66. leftTop_1 = [Infinity, Infinity];
  67. rightBottom_1 = [-Infinity, -Infinity]; // TODO better way?
  68. var sampleLine = function (x0, y0, x1, y1) {
  69. var dx = x1 - x0;
  70. var dy = y1 - y0;
  71. for (var i = 0; i <= 100; i++) {
  72. var p = i / 100;
  73. var pt = projection_1.project([x0 + dx * p, y0 + dy * p]);
  74. vector.min(leftTop_1, leftTop_1, pt);
  75. vector.max(rightBottom_1, rightBottom_1, pt);
  76. }
  77. }; // Top
  78. sampleLine(xMin, yMin, xMax, yMin); // Right
  79. sampleLine(xMax, yMin, xMax, yMax); // Bottom
  80. sampleLine(xMax, yMax, xMin, yMax); // Left
  81. sampleLine(xMin, yMax, xMax, yMin);
  82. }
  83. this.setBoundingRect(leftTop_1[0], leftTop_1[1], rightBottom_1[0] - leftTop_1[0], rightBottom_1[1] - leftTop_1[1]);
  84. }
  85. }
  86. var rect = this.getBoundingRect();
  87. var centerOption = geoModel.get('layoutCenter');
  88. var sizeOption = geoModel.get('layoutSize');
  89. var viewWidth = api.getWidth();
  90. var viewHeight = api.getHeight();
  91. var aspect = rect.width / rect.height * this.aspectScale;
  92. var useCenterAndSize = false;
  93. var center;
  94. var size;
  95. if (centerOption && sizeOption) {
  96. center = [numberUtil.parsePercent(centerOption[0], viewWidth), numberUtil.parsePercent(centerOption[1], viewHeight)];
  97. size = numberUtil.parsePercent(sizeOption, Math.min(viewWidth, viewHeight));
  98. if (!isNaN(center[0]) && !isNaN(center[1]) && !isNaN(size)) {
  99. useCenterAndSize = true;
  100. } else {
  101. if (process.env.NODE_ENV !== 'production') {
  102. console.warn('Given layoutCenter or layoutSize data are invalid. Use left/top/width/height instead.');
  103. }
  104. }
  105. }
  106. var viewRect;
  107. if (useCenterAndSize) {
  108. viewRect = {};
  109. if (aspect > 1) {
  110. // Width is same with size
  111. viewRect.width = size;
  112. viewRect.height = size / aspect;
  113. } else {
  114. viewRect.height = size;
  115. viewRect.width = size * aspect;
  116. }
  117. viewRect.y = center[1] - viewRect.height / 2;
  118. viewRect.x = center[0] - viewRect.width / 2;
  119. } else {
  120. // Use left/top/width/height
  121. var boxLayoutOption = geoModel.getBoxLayoutParams();
  122. boxLayoutOption.aspect = aspect;
  123. viewRect = layout.getLayoutRect(boxLayoutOption, {
  124. width: viewWidth,
  125. height: viewHeight
  126. });
  127. }
  128. this.setViewRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
  129. this.setCenter(geoModel.get('center'), api);
  130. this.setZoom(geoModel.get('zoom'));
  131. } // Back compat for ECharts2, where the coord map is set on map series:
  132. // {type: 'map', geoCoord: {'cityA': [116.46,39.92], 'cityA': [119.12,24.61]}},
  133. function setGeoCoords(geo, model) {
  134. zrUtil.each(model.get('geoCoord'), function (geoCoord, name) {
  135. geo.addGeoCoord(name, geoCoord);
  136. });
  137. }
  138. var GeoCreator =
  139. /** @class */
  140. function () {
  141. function GeoCreator() {
  142. // For deciding which dimensions to use when creating list data
  143. this.dimensions = geo2DDimensions;
  144. }
  145. GeoCreator.prototype.create = function (ecModel, api) {
  146. var geoList = [];
  147. function getCommonGeoProperties(model) {
  148. return {
  149. nameProperty: model.get('nameProperty'),
  150. aspectScale: model.get('aspectScale'),
  151. projection: model.get('projection')
  152. };
  153. } // FIXME Create each time may be slow
  154. ecModel.eachComponent('geo', function (geoModel, idx) {
  155. var mapName = geoModel.get('map');
  156. var geo = new Geo(mapName + idx, mapName, zrUtil.extend({
  157. nameMap: geoModel.get('nameMap')
  158. }, getCommonGeoProperties(geoModel)));
  159. geo.zoomLimit = geoModel.get('scaleLimit');
  160. geoList.push(geo); // setGeoCoords(geo, geoModel);
  161. geoModel.coordinateSystem = geo;
  162. geo.model = geoModel; // Inject resize method
  163. geo.resize = resizeGeo;
  164. geo.resize(geoModel, api);
  165. });
  166. ecModel.eachSeries(function (seriesModel) {
  167. var coordSys = seriesModel.get('coordinateSystem');
  168. if (coordSys === 'geo') {
  169. var geoIndex = seriesModel.get('geoIndex') || 0;
  170. seriesModel.coordinateSystem = geoList[geoIndex];
  171. }
  172. }); // If has map series
  173. var mapModelGroupBySeries = {};
  174. ecModel.eachSeriesByType('map', function (seriesModel) {
  175. if (!seriesModel.getHostGeoModel()) {
  176. var mapType = seriesModel.getMapType();
  177. mapModelGroupBySeries[mapType] = mapModelGroupBySeries[mapType] || [];
  178. mapModelGroupBySeries[mapType].push(seriesModel);
  179. }
  180. });
  181. zrUtil.each(mapModelGroupBySeries, function (mapSeries, mapType) {
  182. var nameMapList = zrUtil.map(mapSeries, function (singleMapSeries) {
  183. return singleMapSeries.get('nameMap');
  184. });
  185. var geo = new Geo(mapType, mapType, zrUtil.extend({
  186. nameMap: zrUtil.mergeAll(nameMapList)
  187. }, getCommonGeoProperties(mapSeries[0])));
  188. geo.zoomLimit = zrUtil.retrieve.apply(null, zrUtil.map(mapSeries, function (singleMapSeries) {
  189. return singleMapSeries.get('scaleLimit');
  190. }));
  191. geoList.push(geo); // Inject resize method
  192. geo.resize = resizeGeo;
  193. geo.resize(mapSeries[0], api);
  194. zrUtil.each(mapSeries, function (singleMapSeries) {
  195. singleMapSeries.coordinateSystem = geo;
  196. setGeoCoords(geo, singleMapSeries);
  197. });
  198. });
  199. return geoList;
  200. };
  201. /**
  202. * Fill given regions array
  203. */
  204. GeoCreator.prototype.getFilledRegions = function (originRegionArr, mapName, nameMap, nameProperty) {
  205. // Not use the original
  206. var regionsArr = (originRegionArr || []).slice();
  207. var dataNameMap = zrUtil.createHashMap();
  208. for (var i = 0; i < regionsArr.length; i++) {
  209. dataNameMap.set(regionsArr[i].name, regionsArr[i]);
  210. }
  211. var source = geoSourceManager.load(mapName, nameMap, nameProperty);
  212. zrUtil.each(source.regions, function (region) {
  213. var name = region.name;
  214. !dataNameMap.get(name) && regionsArr.push({
  215. name: name
  216. });
  217. });
  218. return regionsArr;
  219. };
  220. return GeoCreator;
  221. }();
  222. var geoCreator = new GeoCreator();
  223. export default geoCreator;