Polar.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 RadiusAxis = require("./RadiusAxis");
  20. var AngleAxis = require("./AngleAxis");
  21. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. /**
  40. * @module echarts/coord/polar/Polar
  41. */
  42. /**
  43. * @alias {module:echarts/coord/polar/Polar}
  44. * @constructor
  45. * @param {string} name
  46. */
  47. var Polar = function (name) {
  48. /**
  49. * @type {string}
  50. */
  51. this.name = name || '';
  52. /**
  53. * x of polar center
  54. * @type {number}
  55. */
  56. this.cx = 0;
  57. /**
  58. * y of polar center
  59. * @type {number}
  60. */
  61. this.cy = 0;
  62. /**
  63. * @type {module:echarts/coord/polar/RadiusAxis}
  64. * @private
  65. */
  66. this._radiusAxis = new RadiusAxis();
  67. /**
  68. * @type {module:echarts/coord/polar/AngleAxis}
  69. * @private
  70. */
  71. this._angleAxis = new AngleAxis();
  72. this._radiusAxis.polar = this._angleAxis.polar = this;
  73. };
  74. Polar.prototype = {
  75. type: 'polar',
  76. axisPointerEnabled: true,
  77. constructor: Polar,
  78. /**
  79. * @param {Array.<string>}
  80. * @readOnly
  81. */
  82. dimensions: ['radius', 'angle'],
  83. /**
  84. * @type {module:echarts/coord/PolarModel}
  85. */
  86. model: null,
  87. /**
  88. * If contain coord
  89. * @param {Array.<number>} point
  90. * @return {boolean}
  91. */
  92. containPoint: function (point) {
  93. var coord = this.pointToCoord(point);
  94. return this._radiusAxis.contain(coord[0]) && this._angleAxis.contain(coord[1]);
  95. },
  96. /**
  97. * If contain data
  98. * @param {Array.<number>} data
  99. * @return {boolean}
  100. */
  101. containData: function (data) {
  102. return this._radiusAxis.containData(data[0]) && this._angleAxis.containData(data[1]);
  103. },
  104. /**
  105. * @param {string} dim
  106. * @return {module:echarts/coord/polar/AngleAxis|module:echarts/coord/polar/RadiusAxis}
  107. */
  108. getAxis: function (dim) {
  109. return this['_' + dim + 'Axis'];
  110. },
  111. /**
  112. * @return {Array.<module:echarts/coord/Axis>}
  113. */
  114. getAxes: function () {
  115. return [this._radiusAxis, this._angleAxis];
  116. },
  117. /**
  118. * Get axes by type of scale
  119. * @param {string} scaleType
  120. * @return {module:echarts/coord/polar/AngleAxis|module:echarts/coord/polar/RadiusAxis}
  121. */
  122. getAxesByScale: function (scaleType) {
  123. var axes = [];
  124. var angleAxis = this._angleAxis;
  125. var radiusAxis = this._radiusAxis;
  126. angleAxis.scale.type === scaleType && axes.push(angleAxis);
  127. radiusAxis.scale.type === scaleType && axes.push(radiusAxis);
  128. return axes;
  129. },
  130. /**
  131. * @return {module:echarts/coord/polar/AngleAxis}
  132. */
  133. getAngleAxis: function () {
  134. return this._angleAxis;
  135. },
  136. /**
  137. * @return {module:echarts/coord/polar/RadiusAxis}
  138. */
  139. getRadiusAxis: function () {
  140. return this._radiusAxis;
  141. },
  142. /**
  143. * @param {module:echarts/coord/polar/Axis}
  144. * @return {module:echarts/coord/polar/Axis}
  145. */
  146. getOtherAxis: function (axis) {
  147. var angleAxis = this._angleAxis;
  148. return axis === angleAxis ? this._radiusAxis : angleAxis;
  149. },
  150. /**
  151. * Base axis will be used on stacking.
  152. *
  153. * @return {module:echarts/coord/polar/Axis}
  154. */
  155. getBaseAxis: function () {
  156. return this.getAxesByScale('ordinal')[0] || this.getAxesByScale('time')[0] || this.getAngleAxis();
  157. },
  158. /**
  159. * @param {string} [dim] 'radius' or 'angle' or 'auto' or null/undefined
  160. * @return {Object} {baseAxes: [], otherAxes: []}
  161. */
  162. getTooltipAxes: function (dim) {
  163. var baseAxis = dim != null && dim !== 'auto' ? this.getAxis(dim) : this.getBaseAxis();
  164. return {
  165. baseAxes: [baseAxis],
  166. otherAxes: [this.getOtherAxis(baseAxis)]
  167. };
  168. },
  169. /**
  170. * Convert a single data item to (x, y) point.
  171. * Parameter data is an array which the first element is radius and the second is angle
  172. * @param {Array.<number>} data
  173. * @param {boolean} [clamp=false]
  174. * @return {Array.<number>}
  175. */
  176. dataToPoint: function (data, clamp) {
  177. return this.coordToPoint([this._radiusAxis.dataToRadius(data[0], clamp), this._angleAxis.dataToAngle(data[1], clamp)]);
  178. },
  179. /**
  180. * Convert a (x, y) point to data
  181. * @param {Array.<number>} point
  182. * @param {boolean} [clamp=false]
  183. * @return {Array.<number>}
  184. */
  185. pointToData: function (point, clamp) {
  186. var coord = this.pointToCoord(point);
  187. return [this._radiusAxis.radiusToData(coord[0], clamp), this._angleAxis.angleToData(coord[1], clamp)];
  188. },
  189. /**
  190. * Convert a (x, y) point to (radius, angle) coord
  191. * @param {Array.<number>} point
  192. * @return {Array.<number>}
  193. */
  194. pointToCoord: function (point) {
  195. var dx = point[0] - this.cx;
  196. var dy = point[1] - this.cy;
  197. var angleAxis = this.getAngleAxis();
  198. var extent = angleAxis.getExtent();
  199. var minAngle = Math.min(extent[0], extent[1]);
  200. var maxAngle = Math.max(extent[0], extent[1]); // Fix fixed extent in polarCreator
  201. // FIXME
  202. angleAxis.inverse ? minAngle = maxAngle - 360 : maxAngle = minAngle + 360;
  203. var radius = Math.sqrt(dx * dx + dy * dy);
  204. dx /= radius;
  205. dy /= radius;
  206. var radian = Math.atan2(-dy, dx) / Math.PI * 180; // move to angleExtent
  207. var dir = radian < minAngle ? 1 : -1;
  208. while (radian < minAngle || radian > maxAngle) {
  209. radian += dir * 360;
  210. }
  211. return [radius, radian];
  212. },
  213. /**
  214. * Convert a (radius, angle) coord to (x, y) point
  215. * @param {Array.<number>} coord
  216. * @return {Array.<number>}
  217. */
  218. coordToPoint: function (coord) {
  219. var radius = coord[0];
  220. var radian = coord[1] / 180 * Math.PI;
  221. var x = Math.cos(radian) * radius + this.cx; // Inverse the y
  222. var y = -Math.sin(radian) * radius + this.cy;
  223. return [x, y];
  224. },
  225. /**
  226. * Get ring area of cartesian.
  227. * Area will have a contain function to determine if a point is in the coordinate system.
  228. * @return {Ring}
  229. */
  230. getArea: function () {
  231. var angleAxis = this.getAngleAxis();
  232. var radiusAxis = this.getRadiusAxis();
  233. var radiusExtent = radiusAxis.getExtent().slice();
  234. radiusExtent[0] > radiusExtent[1] && radiusExtent.reverse();
  235. var angleExtent = angleAxis.getExtent();
  236. var RADIAN = Math.PI / 180;
  237. return {
  238. cx: this.cx,
  239. cy: this.cy,
  240. r0: radiusExtent[0],
  241. r: radiusExtent[1],
  242. startAngle: -angleExtent[0] * RADIAN,
  243. endAngle: -angleExtent[1] * RADIAN,
  244. clockwise: angleAxis.inverse,
  245. contain: function (x, y) {
  246. // It's a ring shape.
  247. // Start angle and end angle don't matter
  248. var dx = x - this.cx;
  249. var dy = y - this.cy;
  250. var d2 = dx * dx + dy * dy;
  251. var r = this.r;
  252. var r0 = this.r0;
  253. return d2 <= r * r && d2 >= r0 * r0;
  254. }
  255. };
  256. }
  257. };
  258. var _default = Polar;
  259. module.exports = _default;