Polar.js 7.2 KB

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