Region.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 BoundingRect = require("zrender/lib/core/BoundingRect");
  20. var bbox = require("zrender/lib/core/bbox");
  21. var vec2 = require("zrender/lib/core/vector");
  22. var polygonContain = require("zrender/lib/contain/polygon");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. /**
  42. * @module echarts/coord/geo/Region
  43. */
  44. /**
  45. * @param {string|Region} name
  46. * @param {Array} geometries
  47. * @param {Array.<number>} cp
  48. */
  49. function Region(name, geometries, cp) {
  50. /**
  51. * @type {string}
  52. * @readOnly
  53. */
  54. this.name = name;
  55. /**
  56. * @type {Array.<Array>}
  57. * @readOnly
  58. */
  59. this.geometries = geometries;
  60. if (!cp) {
  61. var rect = this.getBoundingRect();
  62. cp = [rect.x + rect.width / 2, rect.y + rect.height / 2];
  63. } else {
  64. cp = [cp[0], cp[1]];
  65. }
  66. /**
  67. * @type {Array.<number>}
  68. */
  69. this.center = cp;
  70. }
  71. Region.prototype = {
  72. constructor: Region,
  73. properties: null,
  74. /**
  75. * @return {module:zrender/core/BoundingRect}
  76. */
  77. getBoundingRect: function () {
  78. var rect = this._rect;
  79. if (rect) {
  80. return rect;
  81. }
  82. var MAX_NUMBER = Number.MAX_VALUE;
  83. var min = [MAX_NUMBER, MAX_NUMBER];
  84. var max = [-MAX_NUMBER, -MAX_NUMBER];
  85. var min2 = [];
  86. var max2 = [];
  87. var geometries = this.geometries;
  88. for (var i = 0; i < geometries.length; i++) {
  89. // Only support polygon
  90. if (geometries[i].type !== 'polygon') {
  91. continue;
  92. } // Doesn't consider hole
  93. var exterior = geometries[i].exterior;
  94. bbox.fromPoints(exterior, min2, max2);
  95. vec2.min(min, min, min2);
  96. vec2.max(max, max, max2);
  97. } // No data
  98. if (i === 0) {
  99. min[0] = min[1] = max[0] = max[1] = 0;
  100. }
  101. return this._rect = new BoundingRect(min[0], min[1], max[0] - min[0], max[1] - min[1]);
  102. },
  103. /**
  104. * @param {<Array.<number>} coord
  105. * @return {boolean}
  106. */
  107. contain: function (coord) {
  108. var rect = this.getBoundingRect();
  109. var geometries = this.geometries;
  110. if (!rect.contain(coord[0], coord[1])) {
  111. return false;
  112. }
  113. loopGeo: for (var i = 0, len = geometries.length; i < len; i++) {
  114. // Only support polygon.
  115. if (geometries[i].type !== 'polygon') {
  116. continue;
  117. }
  118. var exterior = geometries[i].exterior;
  119. var interiors = geometries[i].interiors;
  120. if (polygonContain.contain(exterior, coord[0], coord[1])) {
  121. // Not in the region if point is in the hole.
  122. for (var k = 0; k < (interiors ? interiors.length : 0); k++) {
  123. if (polygonContain.contain(interiors[k])) {
  124. continue loopGeo;
  125. }
  126. }
  127. return true;
  128. }
  129. }
  130. return false;
  131. },
  132. transformTo: function (x, y, width, height) {
  133. var rect = this.getBoundingRect();
  134. var aspect = rect.width / rect.height;
  135. if (!width) {
  136. width = aspect * height;
  137. } else if (!height) {
  138. height = width / aspect;
  139. }
  140. var target = new BoundingRect(x, y, width, height);
  141. var transform = rect.calculateTransform(target);
  142. var geometries = this.geometries;
  143. for (var i = 0; i < geometries.length; i++) {
  144. // Only support polygon.
  145. if (geometries[i].type !== 'polygon') {
  146. continue;
  147. }
  148. var exterior = geometries[i].exterior;
  149. var interiors = geometries[i].interiors;
  150. for (var p = 0; p < exterior.length; p++) {
  151. vec2.applyTransform(exterior[p], exterior[p], transform);
  152. }
  153. for (var h = 0; h < (interiors ? interiors.length : 0); h++) {
  154. for (var p = 0; p < interiors[h].length; p++) {
  155. vec2.applyTransform(interiors[h][p], interiors[h][p], transform);
  156. }
  157. }
  158. }
  159. rect = this._rect;
  160. rect.copy(target); // Update center
  161. this.center = [rect.x + rect.width / 2, rect.y + rect.height / 2];
  162. },
  163. cloneShallow: function (name) {
  164. name == null && (name = this.name);
  165. var newRegion = new Region(name, this.geometries, this.center);
  166. newRegion._rect = this._rect;
  167. newRegion.transformTo = null; // Simply avoid to be called.
  168. return newRegion;
  169. }
  170. };
  171. var _default = Region;
  172. module.exports = _default;