Polar.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 RadiusAxis from './RadiusAxis.js';
  41. import AngleAxis from './AngleAxis.js';
  42. export var polarDimensions = ['radius', 'angle'];
  43. var Polar = /** @class */function () {
  44. function Polar(name) {
  45. this.dimensions = polarDimensions;
  46. this.type = 'polar';
  47. /**
  48. * x of polar center
  49. */
  50. this.cx = 0;
  51. /**
  52. * y of polar center
  53. */
  54. this.cy = 0;
  55. this._radiusAxis = new RadiusAxis();
  56. this._angleAxis = new AngleAxis();
  57. this.axisPointerEnabled = true;
  58. this.name = name || '';
  59. this._radiusAxis.polar = this._angleAxis.polar = this;
  60. }
  61. /**
  62. * If contain coord
  63. */
  64. Polar.prototype.containPoint = function (point) {
  65. var coord = this.pointToCoord(point);
  66. return this._radiusAxis.contain(coord[0]) && this._angleAxis.contain(coord[1]);
  67. };
  68. /**
  69. * If contain data
  70. */
  71. Polar.prototype.containData = function (data) {
  72. return this._radiusAxis.containData(data[0]) && this._angleAxis.containData(data[1]);
  73. };
  74. Polar.prototype.getAxis = function (dim) {
  75. var key = '_' + dim + 'Axis';
  76. return this[key];
  77. };
  78. Polar.prototype.getAxes = function () {
  79. return [this._radiusAxis, this._angleAxis];
  80. };
  81. /**
  82. * Get axes by type of scale
  83. */
  84. Polar.prototype.getAxesByScale = function (scaleType) {
  85. var axes = [];
  86. var angleAxis = this._angleAxis;
  87. var radiusAxis = this._radiusAxis;
  88. angleAxis.scale.type === scaleType && axes.push(angleAxis);
  89. radiusAxis.scale.type === scaleType && axes.push(radiusAxis);
  90. return axes;
  91. };
  92. Polar.prototype.getAngleAxis = function () {
  93. return this._angleAxis;
  94. };
  95. Polar.prototype.getRadiusAxis = function () {
  96. return this._radiusAxis;
  97. };
  98. Polar.prototype.getOtherAxis = function (axis) {
  99. var angleAxis = this._angleAxis;
  100. return axis === angleAxis ? this._radiusAxis : angleAxis;
  101. };
  102. /**
  103. * Base axis will be used on stacking.
  104. *
  105. */
  106. Polar.prototype.getBaseAxis = function () {
  107. return this.getAxesByScale('ordinal')[0] || this.getAxesByScale('time')[0] || this.getAngleAxis();
  108. };
  109. Polar.prototype.getTooltipAxes = function (dim) {
  110. var baseAxis = dim != null && dim !== 'auto' ? this.getAxis(dim) : this.getBaseAxis();
  111. return {
  112. baseAxes: [baseAxis],
  113. otherAxes: [this.getOtherAxis(baseAxis)]
  114. };
  115. };
  116. /**
  117. * Convert a single data item to (x, y) point.
  118. * Parameter data is an array which the first element is radius and the second is angle
  119. */
  120. Polar.prototype.dataToPoint = function (data, clamp) {
  121. return this.coordToPoint([this._radiusAxis.dataToRadius(data[0], clamp), this._angleAxis.dataToAngle(data[1], clamp)]);
  122. };
  123. /**
  124. * Convert a (x, y) point to data
  125. */
  126. Polar.prototype.pointToData = function (point, clamp) {
  127. var coord = this.pointToCoord(point);
  128. return [this._radiusAxis.radiusToData(coord[0], clamp), this._angleAxis.angleToData(coord[1], clamp)];
  129. };
  130. /**
  131. * Convert a (x, y) point to (radius, angle) coord
  132. */
  133. Polar.prototype.pointToCoord = function (point) {
  134. var dx = point[0] - this.cx;
  135. var dy = point[1] - this.cy;
  136. var angleAxis = this.getAngleAxis();
  137. var extent = angleAxis.getExtent();
  138. var minAngle = Math.min(extent[0], extent[1]);
  139. var maxAngle = Math.max(extent[0], extent[1]);
  140. // Fix fixed extent in polarCreator
  141. // FIXME
  142. angleAxis.inverse ? minAngle = maxAngle - 360 : maxAngle = minAngle + 360;
  143. var radius = Math.sqrt(dx * dx + dy * dy);
  144. dx /= radius;
  145. dy /= radius;
  146. var radian = Math.atan2(-dy, dx) / Math.PI * 180;
  147. // move to angleExtent
  148. var dir = radian < minAngle ? 1 : -1;
  149. while (radian < minAngle || radian > maxAngle) {
  150. radian += dir * 360;
  151. }
  152. return [radius, radian];
  153. };
  154. /**
  155. * Convert a (radius, angle) coord to (x, y) point
  156. */
  157. Polar.prototype.coordToPoint = function (coord) {
  158. var radius = coord[0];
  159. var radian = coord[1] / 180 * Math.PI;
  160. var x = Math.cos(radian) * radius + this.cx;
  161. // Inverse the y
  162. var y = -Math.sin(radian) * radius + this.cy;
  163. return [x, y];
  164. };
  165. /**
  166. * Get ring area of cartesian.
  167. * Area will have a contain function to determine if a point is in the coordinate system.
  168. */
  169. Polar.prototype.getArea = function () {
  170. var angleAxis = this.getAngleAxis();
  171. var radiusAxis = this.getRadiusAxis();
  172. var radiusExtent = radiusAxis.getExtent().slice();
  173. radiusExtent[0] > radiusExtent[1] && radiusExtent.reverse();
  174. var angleExtent = angleAxis.getExtent();
  175. var RADIAN = Math.PI / 180;
  176. return {
  177. cx: this.cx,
  178. cy: this.cy,
  179. r0: radiusExtent[0],
  180. r: radiusExtent[1],
  181. startAngle: -angleExtent[0] * RADIAN,
  182. endAngle: -angleExtent[1] * RADIAN,
  183. clockwise: angleAxis.inverse,
  184. contain: function (x, y) {
  185. // It's a ring shape.
  186. // Start angle and end angle don't matter
  187. var dx = x - this.cx;
  188. var dy = y - this.cy;
  189. // minus a tiny value 1e-4 to avoid being clipped unexpectedly
  190. var d2 = dx * dx + dy * dy - 1e-4;
  191. var r = this.r;
  192. var r0 = this.r0;
  193. return d2 <= r * r && d2 >= r0 * r0;
  194. }
  195. };
  196. };
  197. Polar.prototype.convertToPixel = function (ecModel, finder, value) {
  198. var coordSys = getCoordSys(finder);
  199. return coordSys === this ? this.dataToPoint(value) : null;
  200. };
  201. Polar.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  202. var coordSys = getCoordSys(finder);
  203. return coordSys === this ? this.pointToData(pixel) : null;
  204. };
  205. return Polar;
  206. }();
  207. function getCoordSys(finder) {
  208. var seriesModel = finder.seriesModel;
  209. var polarModel = finder.polarModel;
  210. return polarModel && polarModel.coordinateSystem || seriesModel && seriesModel.coordinateSystem;
  211. }
  212. export default Polar;