geoCreator.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. var _config = require("../../config");
  20. var __DEV__ = _config.__DEV__;
  21. var echarts = require("../../echarts");
  22. var zrUtil = require("zrender/lib/core/util");
  23. var Geo = require("./Geo");
  24. var layout = require("../../util/layout");
  25. var numberUtil = require("../../util/number");
  26. var geoSourceManager = require("./geoSourceManager");
  27. var mapDataStorage = require("./mapDataStorage");
  28. /*
  29. * Licensed to the Apache Software Foundation (ASF) under one
  30. * or more contributor license agreements. See the NOTICE file
  31. * distributed with this work for additional information
  32. * regarding copyright ownership. The ASF licenses this file
  33. * to you under the Apache License, Version 2.0 (the
  34. * "License"); you may not use this file except in compliance
  35. * with the License. You may obtain a copy of the License at
  36. *
  37. * http://www.apache.org/licenses/LICENSE-2.0
  38. *
  39. * Unless required by applicable law or agreed to in writing,
  40. * software distributed under the License is distributed on an
  41. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  42. * KIND, either express or implied. See the License for the
  43. * specific language governing permissions and limitations
  44. * under the License.
  45. */
  46. /**
  47. * Resize method bound to the geo
  48. * @param {module:echarts/coord/geo/GeoModel|module:echarts/chart/map/MapModel} geoModel
  49. * @param {module:echarts/ExtensionAPI} api
  50. */
  51. function resizeGeo(geoModel, api) {
  52. var boundingCoords = geoModel.get('boundingCoords');
  53. if (boundingCoords != null) {
  54. var leftTop = boundingCoords[0];
  55. var rightBottom = boundingCoords[1];
  56. if (isNaN(leftTop[0]) || isNaN(leftTop[1]) || isNaN(rightBottom[0]) || isNaN(rightBottom[1])) {} else {
  57. this.setBoundingRect(leftTop[0], leftTop[1], rightBottom[0] - leftTop[0], rightBottom[1] - leftTop[1]);
  58. }
  59. }
  60. var rect = this.getBoundingRect();
  61. var boxLayoutOption;
  62. var center = geoModel.get('layoutCenter');
  63. var size = 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. if (center && size) {
  69. center = [numberUtil.parsePercent(center[0], viewWidth), numberUtil.parsePercent(center[1], viewHeight)];
  70. size = numberUtil.parsePercent(size, Math.min(viewWidth, viewHeight));
  71. if (!isNaN(center[0]) && !isNaN(center[1]) && !isNaN(size)) {
  72. useCenterAndSize = true;
  73. } else {}
  74. }
  75. var viewRect;
  76. if (useCenterAndSize) {
  77. var viewRect = {};
  78. if (aspect > 1) {
  79. // Width is same with size
  80. viewRect.width = size;
  81. viewRect.height = size / aspect;
  82. } else {
  83. viewRect.height = size;
  84. viewRect.width = size * aspect;
  85. }
  86. viewRect.y = center[1] - viewRect.height / 2;
  87. viewRect.x = center[0] - viewRect.width / 2;
  88. } else {
  89. // Use left/top/width/height
  90. boxLayoutOption = geoModel.getBoxLayoutParams(); // 0.75 rate
  91. boxLayoutOption.aspect = aspect;
  92. viewRect = layout.getLayoutRect(boxLayoutOption, {
  93. width: viewWidth,
  94. height: viewHeight
  95. });
  96. }
  97. this.setViewRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
  98. this.setCenter(geoModel.get('center'));
  99. this.setZoom(geoModel.get('zoom'));
  100. }
  101. /**
  102. * @param {module:echarts/coord/Geo} geo
  103. * @param {module:echarts/model/Model} model
  104. * @inner
  105. */
  106. function setGeoCoords(geo, model) {
  107. zrUtil.each(model.get('geoCoord'), function (geoCoord, name) {
  108. geo.addGeoCoord(name, geoCoord);
  109. });
  110. }
  111. var geoCreator = {
  112. // For deciding which dimensions to use when creating list data
  113. dimensions: Geo.prototype.dimensions,
  114. create: function (ecModel, api) {
  115. var geoList = []; // FIXME Create each time may be slow
  116. ecModel.eachComponent('geo', function (geoModel, idx) {
  117. var name = geoModel.get('map');
  118. var aspectScale = geoModel.get('aspectScale');
  119. var invertLongitute = true;
  120. var mapRecords = mapDataStorage.retrieveMap(name);
  121. if (mapRecords && mapRecords[0] && mapRecords[0].type === 'svg') {
  122. aspectScale == null && (aspectScale = 1);
  123. invertLongitute = false;
  124. } else {
  125. aspectScale == null && (aspectScale = 0.75);
  126. }
  127. var geo = new Geo(name + idx, name, geoModel.get('nameMap'), invertLongitute);
  128. geo.aspectScale = aspectScale;
  129. geo.zoomLimit = geoModel.get('scaleLimit');
  130. geoList.push(geo);
  131. setGeoCoords(geo, geoModel);
  132. geoModel.coordinateSystem = geo;
  133. geo.model = geoModel; // Inject resize method
  134. geo.resize = resizeGeo;
  135. geo.resize(geoModel, api);
  136. });
  137. ecModel.eachSeries(function (seriesModel) {
  138. var coordSys = seriesModel.get('coordinateSystem');
  139. if (coordSys === 'geo') {
  140. var geoIndex = seriesModel.get('geoIndex') || 0;
  141. seriesModel.coordinateSystem = geoList[geoIndex];
  142. }
  143. }); // If has map series
  144. var mapModelGroupBySeries = {};
  145. ecModel.eachSeriesByType('map', function (seriesModel) {
  146. if (!seriesModel.getHostGeoModel()) {
  147. var mapType = seriesModel.getMapType();
  148. mapModelGroupBySeries[mapType] = mapModelGroupBySeries[mapType] || [];
  149. mapModelGroupBySeries[mapType].push(seriesModel);
  150. }
  151. });
  152. zrUtil.each(mapModelGroupBySeries, function (mapSeries, mapType) {
  153. var nameMapList = zrUtil.map(mapSeries, function (singleMapSeries) {
  154. return singleMapSeries.get('nameMap');
  155. });
  156. var geo = new Geo(mapType, mapType, zrUtil.mergeAll(nameMapList));
  157. geo.zoomLimit = zrUtil.retrieve.apply(null, zrUtil.map(mapSeries, function (singleMapSeries) {
  158. return singleMapSeries.get('scaleLimit');
  159. }));
  160. geoList.push(geo); // Inject resize method
  161. geo.resize = resizeGeo;
  162. geo.aspectScale = mapSeries[0].get('aspectScale');
  163. geo.resize(mapSeries[0], api);
  164. zrUtil.each(mapSeries, function (singleMapSeries) {
  165. singleMapSeries.coordinateSystem = geo;
  166. setGeoCoords(geo, singleMapSeries);
  167. });
  168. });
  169. return geoList;
  170. },
  171. /**
  172. * Fill given regions array
  173. * @param {Array.<Object>} originRegionArr
  174. * @param {string} mapName
  175. * @param {Object} [nameMap]
  176. * @return {Array}
  177. */
  178. getFilledRegions: function (originRegionArr, mapName, nameMap) {
  179. // Not use the original
  180. var regionsArr = (originRegionArr || []).slice();
  181. var dataNameMap = zrUtil.createHashMap();
  182. for (var i = 0; i < regionsArr.length; i++) {
  183. dataNameMap.set(regionsArr[i].name, regionsArr[i]);
  184. }
  185. var source = geoSourceManager.load(mapName, nameMap);
  186. zrUtil.each(source.regions, function (region) {
  187. var name = region.name;
  188. !dataNameMap.get(name) && regionsArr.push({
  189. name: name
  190. });
  191. });
  192. return regionsArr;
  193. }
  194. };
  195. echarts.registerCoordinateSystem('geo', geoCreator);
  196. var _default = geoCreator;
  197. module.exports = _default;