geoCreator.js 7.4 KB

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