Radar.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 zrUtil = require("zrender/lib/core/util");
  20. var IndicatorAxis = require("./IndicatorAxis");
  21. var IntervalScale = require("../../scale/Interval");
  22. var numberUtil = require("../../util/number");
  23. var _axisHelper = require("../axisHelper");
  24. var getScaleExtent = _axisHelper.getScaleExtent;
  25. var niceScaleExtent = _axisHelper.niceScaleExtent;
  26. var CoordinateSystem = require("../../CoordinateSystem");
  27. var LogScale = require("../../scale/Log");
  28. /*
  29. * Licensed to the Apache Software Foundation (ASF) under one
  30. * or more contributor license agreements. See the NOTICE file
  31. * distributed with this work for additional information
  32. * regarding copyright ownership. The ASF licenses this file
  33. * to you under the Apache License, Version 2.0 (the
  34. * "License"); you may not use this file except in compliance
  35. * with the License. You may obtain a copy of the License at
  36. *
  37. * http://www.apache.org/licenses/LICENSE-2.0
  38. *
  39. * Unless required by applicable law or agreed to in writing,
  40. * software distributed under the License is distributed on an
  41. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  42. * KIND, either express or implied. See the License for the
  43. * specific language governing permissions and limitations
  44. * under the License.
  45. */
  46. // TODO clockwise
  47. function Radar(radarModel, ecModel, api) {
  48. this._model = radarModel;
  49. /**
  50. * Radar dimensions
  51. * @type {Array.<string>}
  52. */
  53. this.dimensions = [];
  54. this._indicatorAxes = zrUtil.map(radarModel.getIndicatorModels(), function (indicatorModel, idx) {
  55. var dim = 'indicator_' + idx;
  56. var indicatorAxis = new IndicatorAxis(dim, indicatorModel.get('axisType') === 'log' ? new LogScale() : new IntervalScale());
  57. indicatorAxis.name = indicatorModel.get('name'); // Inject model and axis
  58. indicatorAxis.model = indicatorModel;
  59. indicatorModel.axis = indicatorAxis;
  60. this.dimensions.push(dim);
  61. return indicatorAxis;
  62. }, this);
  63. this.resize(radarModel, api);
  64. /**
  65. * @type {number}
  66. * @readOnly
  67. */
  68. this.cx;
  69. /**
  70. * @type {number}
  71. * @readOnly
  72. */
  73. this.cy;
  74. /**
  75. * @type {number}
  76. * @readOnly
  77. */
  78. this.r;
  79. /**
  80. * @type {number}
  81. * @readOnly
  82. */
  83. this.r0;
  84. /**
  85. * @type {number}
  86. * @readOnly
  87. */
  88. this.startAngle;
  89. }
  90. Radar.prototype.getIndicatorAxes = function () {
  91. return this._indicatorAxes;
  92. };
  93. Radar.prototype.dataToPoint = function (value, indicatorIndex) {
  94. var indicatorAxis = this._indicatorAxes[indicatorIndex];
  95. return this.coordToPoint(indicatorAxis.dataToCoord(value), indicatorIndex);
  96. };
  97. Radar.prototype.coordToPoint = function (coord, indicatorIndex) {
  98. var indicatorAxis = this._indicatorAxes[indicatorIndex];
  99. var angle = indicatorAxis.angle;
  100. var x = this.cx + coord * Math.cos(angle);
  101. var y = this.cy - coord * Math.sin(angle);
  102. return [x, y];
  103. };
  104. Radar.prototype.pointToData = function (pt) {
  105. var dx = pt[0] - this.cx;
  106. var dy = pt[1] - this.cy;
  107. var radius = Math.sqrt(dx * dx + dy * dy);
  108. dx /= radius;
  109. dy /= radius;
  110. var radian = Math.atan2(-dy, dx); // Find the closest angle
  111. // FIXME index can calculated directly
  112. var minRadianDiff = Infinity;
  113. var closestAxis;
  114. var closestAxisIdx = -1;
  115. for (var i = 0; i < this._indicatorAxes.length; i++) {
  116. var indicatorAxis = this._indicatorAxes[i];
  117. var diff = Math.abs(radian - indicatorAxis.angle);
  118. if (diff < minRadianDiff) {
  119. closestAxis = indicatorAxis;
  120. closestAxisIdx = i;
  121. minRadianDiff = diff;
  122. }
  123. }
  124. return [closestAxisIdx, +(closestAxis && closestAxis.coordToData(radius))];
  125. };
  126. Radar.prototype.resize = function (radarModel, api) {
  127. var center = radarModel.get('center');
  128. var viewWidth = api.getWidth();
  129. var viewHeight = api.getHeight();
  130. var viewSize = Math.min(viewWidth, viewHeight) / 2;
  131. this.cx = numberUtil.parsePercent(center[0], viewWidth);
  132. this.cy = numberUtil.parsePercent(center[1], viewHeight);
  133. this.startAngle = radarModel.get('startAngle') * Math.PI / 180; // radius may be single value like `20`, `'80%'`, or array like `[10, '80%']`
  134. var radius = radarModel.get('radius');
  135. if (typeof radius === 'string' || typeof radius === 'number') {
  136. radius = [0, radius];
  137. }
  138. this.r0 = numberUtil.parsePercent(radius[0], viewSize);
  139. this.r = numberUtil.parsePercent(radius[1], viewSize);
  140. zrUtil.each(this._indicatorAxes, function (indicatorAxis, idx) {
  141. indicatorAxis.setExtent(this.r0, this.r);
  142. var angle = this.startAngle + idx * Math.PI * 2 / this._indicatorAxes.length; // Normalize to [-PI, PI]
  143. angle = Math.atan2(Math.sin(angle), Math.cos(angle));
  144. indicatorAxis.angle = angle;
  145. }, this);
  146. };
  147. Radar.prototype.update = function (ecModel, api) {
  148. var indicatorAxes = this._indicatorAxes;
  149. var radarModel = this._model;
  150. zrUtil.each(indicatorAxes, function (indicatorAxis) {
  151. indicatorAxis.scale.setExtent(Infinity, -Infinity);
  152. });
  153. ecModel.eachSeriesByType('radar', function (radarSeries, idx) {
  154. if (radarSeries.get('coordinateSystem') !== 'radar' || ecModel.getComponent('radar', radarSeries.get('radarIndex')) !== radarModel) {
  155. return;
  156. }
  157. var data = radarSeries.getData();
  158. zrUtil.each(indicatorAxes, function (indicatorAxis) {
  159. indicatorAxis.scale.unionExtentFromData(data, data.mapDimension(indicatorAxis.dim));
  160. });
  161. }, this);
  162. var splitNumber = radarModel.get('splitNumber');
  163. function increaseInterval(interval) {
  164. var exp10 = Math.pow(10, Math.floor(Math.log(interval) / Math.LN10)); // Increase interval
  165. var f = interval / exp10;
  166. if (f === 2) {
  167. f = 5;
  168. } else {
  169. // f is 2 or 5
  170. f *= 2;
  171. }
  172. return f * exp10;
  173. } // Force all the axis fixing the maxSplitNumber.
  174. zrUtil.each(indicatorAxes, function (indicatorAxis, idx) {
  175. var rawExtent = getScaleExtent(indicatorAxis.scale, indicatorAxis.model).extent;
  176. niceScaleExtent(indicatorAxis.scale, indicatorAxis.model);
  177. var axisModel = indicatorAxis.model;
  178. var scale = indicatorAxis.scale;
  179. var fixedMin = axisModel.getMin();
  180. var fixedMax = axisModel.getMax();
  181. var interval = scale.getInterval();
  182. if (fixedMin != null && fixedMax != null) {
  183. // User set min, max, divide to get new interval
  184. scale.setExtent(+fixedMin, +fixedMax);
  185. scale.setInterval((fixedMax - fixedMin) / splitNumber);
  186. } else if (fixedMin != null) {
  187. var max; // User set min, expand extent on the other side
  188. do {
  189. max = fixedMin + interval * splitNumber;
  190. scale.setExtent(+fixedMin, max); // Interval must been set after extent
  191. // FIXME
  192. scale.setInterval(interval);
  193. interval = increaseInterval(interval);
  194. } while (max < rawExtent[1] && isFinite(max) && isFinite(rawExtent[1]));
  195. } else if (fixedMax != null) {
  196. var min; // User set min, expand extent on the other side
  197. do {
  198. min = fixedMax - interval * splitNumber;
  199. scale.setExtent(min, +fixedMax);
  200. scale.setInterval(interval);
  201. interval = increaseInterval(interval);
  202. } while (min > rawExtent[0] && isFinite(min) && isFinite(rawExtent[0]));
  203. } else {
  204. var nicedSplitNumber = scale.getTicks().length - 1;
  205. if (nicedSplitNumber > splitNumber) {
  206. interval = increaseInterval(interval);
  207. } // TODO
  208. var max = Math.ceil(rawExtent[1] / interval) * interval;
  209. var min = numberUtil.round(max - interval * splitNumber);
  210. scale.setExtent(min, max);
  211. scale.setInterval(interval);
  212. }
  213. });
  214. };
  215. /**
  216. * Radar dimensions is based on the data
  217. * @type {Array}
  218. */
  219. Radar.dimensions = [];
  220. Radar.create = function (ecModel, api) {
  221. var radarList = [];
  222. ecModel.eachComponent('radar', function (radarModel) {
  223. var radar = new Radar(radarModel, ecModel, api);
  224. radarList.push(radar);
  225. radarModel.coordinateSystem = radar;
  226. });
  227. ecModel.eachSeriesByType('radar', function (radarSeries) {
  228. if (radarSeries.get('coordinateSystem') === 'radar') {
  229. // Inject coordinate system
  230. radarSeries.coordinateSystem = radarList[radarSeries.get('radarIndex') || 0];
  231. }
  232. });
  233. return radarList;
  234. };
  235. CoordinateSystem.register('radar', Radar);
  236. var _default = Radar;
  237. module.exports = _default;