Polar.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var RadiusAxis = require("./RadiusAxis");
  2. var AngleAxis = require("./AngleAxis");
  3. /**
  4. * @module echarts/coord/polar/Polar
  5. */
  6. /**
  7. * @alias {module:echarts/coord/polar/Polar}
  8. * @constructor
  9. * @param {string} name
  10. */
  11. var Polar = function (name) {
  12. /**
  13. * @type {string}
  14. */
  15. this.name = name || '';
  16. /**
  17. * x of polar center
  18. * @type {number}
  19. */
  20. this.cx = 0;
  21. /**
  22. * y of polar center
  23. * @type {number}
  24. */
  25. this.cy = 0;
  26. /**
  27. * @type {module:echarts/coord/polar/RadiusAxis}
  28. * @private
  29. */
  30. this._radiusAxis = new RadiusAxis();
  31. /**
  32. * @type {module:echarts/coord/polar/AngleAxis}
  33. * @private
  34. */
  35. this._angleAxis = new AngleAxis();
  36. this._radiusAxis.polar = this._angleAxis.polar = this;
  37. };
  38. Polar.prototype = {
  39. type: 'polar',
  40. axisPointerEnabled: true,
  41. constructor: Polar,
  42. /**
  43. * @param {Array.<string>}
  44. * @readOnly
  45. */
  46. dimensions: ['radius', 'angle'],
  47. /**
  48. * @type {module:echarts/coord/PolarModel}
  49. */
  50. model: null,
  51. /**
  52. * If contain coord
  53. * @param {Array.<number>} point
  54. * @return {boolean}
  55. */
  56. containPoint: function (point) {
  57. var coord = this.pointToCoord(point);
  58. return this._radiusAxis.contain(coord[0]) && this._angleAxis.contain(coord[1]);
  59. },
  60. /**
  61. * If contain data
  62. * @param {Array.<number>} data
  63. * @return {boolean}
  64. */
  65. containData: function (data) {
  66. return this._radiusAxis.containData(data[0]) && this._angleAxis.containData(data[1]);
  67. },
  68. /**
  69. * @param {string} dim
  70. * @return {module:echarts/coord/polar/AngleAxis|module:echarts/coord/polar/RadiusAxis}
  71. */
  72. getAxis: function (dim) {
  73. return this['_' + dim + 'Axis'];
  74. },
  75. /**
  76. * @return {Array.<module:echarts/coord/Axis>}
  77. */
  78. getAxes: function () {
  79. return [this._radiusAxis, this._angleAxis];
  80. },
  81. /**
  82. * Get axes by type of scale
  83. * @param {string} scaleType
  84. * @return {module:echarts/coord/polar/AngleAxis|module:echarts/coord/polar/RadiusAxis}
  85. */
  86. 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. /**
  95. * @return {module:echarts/coord/polar/AngleAxis}
  96. */
  97. getAngleAxis: function () {
  98. return this._angleAxis;
  99. },
  100. /**
  101. * @return {module:echarts/coord/polar/RadiusAxis}
  102. */
  103. getRadiusAxis: function () {
  104. return this._radiusAxis;
  105. },
  106. /**
  107. * @param {module:echarts/coord/polar/Axis}
  108. * @return {module:echarts/coord/polar/Axis}
  109. */
  110. getOtherAxis: function (axis) {
  111. var angleAxis = this._angleAxis;
  112. return axis === angleAxis ? this._radiusAxis : angleAxis;
  113. },
  114. /**
  115. * Base axis will be used on stacking.
  116. *
  117. * @return {module:echarts/coord/polar/Axis}
  118. */
  119. getBaseAxis: function () {
  120. return this.getAxesByScale('ordinal')[0] || this.getAxesByScale('time')[0] || this.getAngleAxis();
  121. },
  122. /**
  123. * @param {string} [dim] 'radius' or 'angle' or 'auto' or null/undefined
  124. * @return {Object} {baseAxes: [], otherAxes: []}
  125. */
  126. getTooltipAxes: function (dim) {
  127. var baseAxis = dim != null && dim !== 'auto' ? this.getAxis(dim) : this.getBaseAxis();
  128. return {
  129. baseAxes: [baseAxis],
  130. otherAxes: [this.getOtherAxis(baseAxis)]
  131. };
  132. },
  133. /**
  134. * Convert a single data item to (x, y) point.
  135. * Parameter data is an array which the first element is radius and the second is angle
  136. * @param {Array.<number>} data
  137. * @param {boolean} [clamp=false]
  138. * @return {Array.<number>}
  139. */
  140. dataToPoint: function (data, clamp) {
  141. return this.coordToPoint([this._radiusAxis.dataToRadius(data[0], clamp), this._angleAxis.dataToAngle(data[1], clamp)]);
  142. },
  143. /**
  144. * Convert a (x, y) point to data
  145. * @param {Array.<number>} point
  146. * @param {boolean} [clamp=false]
  147. * @return {Array.<number>}
  148. */
  149. pointToData: function (point, clamp) {
  150. var coord = this.pointToCoord(point);
  151. return [this._radiusAxis.radiusToData(coord[0], clamp), this._angleAxis.angleToData(coord[1], clamp)];
  152. },
  153. /**
  154. * Convert a (x, y) point to (radius, angle) coord
  155. * @param {Array.<number>} point
  156. * @return {Array.<number>}
  157. */
  158. pointToCoord: function (point) {
  159. var dx = point[0] - this.cx;
  160. var dy = point[1] - this.cy;
  161. var angleAxis = this.getAngleAxis();
  162. var extent = angleAxis.getExtent();
  163. var minAngle = Math.min(extent[0], extent[1]);
  164. var maxAngle = Math.max(extent[0], extent[1]); // Fix fixed extent in polarCreator
  165. // FIXME
  166. angleAxis.inverse ? minAngle = maxAngle - 360 : maxAngle = minAngle + 360;
  167. var radius = Math.sqrt(dx * dx + dy * dy);
  168. dx /= radius;
  169. dy /= radius;
  170. var radian = Math.atan2(-dy, dx) / Math.PI * 180; // move to angleExtent
  171. var dir = radian < minAngle ? 1 : -1;
  172. while (radian < minAngle || radian > maxAngle) {
  173. radian += dir * 360;
  174. }
  175. return [radius, radian];
  176. },
  177. /**
  178. * Convert a (radius, angle) coord to (x, y) point
  179. * @param {Array.<number>} coord
  180. * @return {Array.<number>}
  181. */
  182. coordToPoint: function (coord) {
  183. var radius = coord[0];
  184. var radian = coord[1] / 180 * Math.PI;
  185. var x = Math.cos(radian) * radius + this.cx; // Inverse the y
  186. var y = -Math.sin(radian) * radius + this.cy;
  187. return [x, y];
  188. }
  189. };
  190. var _default = Polar;
  191. module.exports = _default;