Radar.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. var zrUtil = require("zrender/lib/core/util");
  2. var IndicatorAxis = require("./IndicatorAxis");
  3. var IntervalScale = require("../../scale/Interval");
  4. var numberUtil = require("../../util/number");
  5. var _axisHelper = require("../axisHelper");
  6. var getScaleExtent = _axisHelper.getScaleExtent;
  7. var niceScaleExtent = _axisHelper.niceScaleExtent;
  8. var CoordinateSystem = require("../../CoordinateSystem");
  9. // TODO clockwise
  10. function Radar(radarModel, ecModel, api) {
  11. this._model = radarModel;
  12. /**
  13. * Radar dimensions
  14. * @type {Array.<string>}
  15. */
  16. this.dimensions = [];
  17. this._indicatorAxes = zrUtil.map(radarModel.getIndicatorModels(), function (indicatorModel, idx) {
  18. var dim = 'indicator_' + idx;
  19. var indicatorAxis = new IndicatorAxis(dim, new IntervalScale());
  20. indicatorAxis.name = indicatorModel.get('name'); // Inject model and axis
  21. indicatorAxis.model = indicatorModel;
  22. indicatorModel.axis = indicatorAxis;
  23. this.dimensions.push(dim);
  24. return indicatorAxis;
  25. }, this);
  26. this.resize(radarModel, api);
  27. /**
  28. * @type {number}
  29. * @readOnly
  30. */
  31. this.cx;
  32. /**
  33. * @type {number}
  34. * @readOnly
  35. */
  36. this.cy;
  37. /**
  38. * @type {number}
  39. * @readOnly
  40. */
  41. this.r;
  42. /**
  43. * @type {number}
  44. * @readOnly
  45. */
  46. this.startAngle;
  47. }
  48. Radar.prototype.getIndicatorAxes = function () {
  49. return this._indicatorAxes;
  50. };
  51. Radar.prototype.dataToPoint = function (value, indicatorIndex) {
  52. var indicatorAxis = this._indicatorAxes[indicatorIndex];
  53. return this.coordToPoint(indicatorAxis.dataToCoord(value), indicatorIndex);
  54. };
  55. Radar.prototype.coordToPoint = function (coord, indicatorIndex) {
  56. var indicatorAxis = this._indicatorAxes[indicatorIndex];
  57. var angle = indicatorAxis.angle;
  58. var x = this.cx + coord * Math.cos(angle);
  59. var y = this.cy - coord * Math.sin(angle);
  60. return [x, y];
  61. };
  62. Radar.prototype.pointToData = function (pt) {
  63. var dx = pt[0] - this.cx;
  64. var dy = pt[1] - this.cy;
  65. var radius = Math.sqrt(dx * dx + dy * dy);
  66. dx /= radius;
  67. dy /= radius;
  68. var radian = Math.atan2(-dy, dx); // Find the closest angle
  69. // FIXME index can calculated directly
  70. var minRadianDiff = Infinity;
  71. var closestAxis;
  72. var closestAxisIdx = -1;
  73. for (var i = 0; i < this._indicatorAxes.length; i++) {
  74. var indicatorAxis = this._indicatorAxes[i];
  75. var diff = Math.abs(radian - indicatorAxis.angle);
  76. if (diff < minRadianDiff) {
  77. closestAxis = indicatorAxis;
  78. closestAxisIdx = i;
  79. minRadianDiff = diff;
  80. }
  81. }
  82. return [closestAxisIdx, +(closestAxis && closestAxis.coodToData(radius))];
  83. };
  84. Radar.prototype.resize = function (radarModel, api) {
  85. var center = radarModel.get('center');
  86. var viewWidth = api.getWidth();
  87. var viewHeight = api.getHeight();
  88. var viewSize = Math.min(viewWidth, viewHeight) / 2;
  89. this.cx = numberUtil.parsePercent(center[0], viewWidth);
  90. this.cy = numberUtil.parsePercent(center[1], viewHeight);
  91. this.startAngle = radarModel.get('startAngle') * Math.PI / 180;
  92. this.r = numberUtil.parsePercent(radarModel.get('radius'), viewSize);
  93. zrUtil.each(this._indicatorAxes, function (indicatorAxis, idx) {
  94. indicatorAxis.setExtent(0, this.r);
  95. var angle = this.startAngle + idx * Math.PI * 2 / this._indicatorAxes.length; // Normalize to [-PI, PI]
  96. angle = Math.atan2(Math.sin(angle), Math.cos(angle));
  97. indicatorAxis.angle = angle;
  98. }, this);
  99. };
  100. Radar.prototype.update = function (ecModel, api) {
  101. var indicatorAxes = this._indicatorAxes;
  102. var radarModel = this._model;
  103. zrUtil.each(indicatorAxes, function (indicatorAxis) {
  104. indicatorAxis.scale.setExtent(Infinity, -Infinity);
  105. });
  106. ecModel.eachSeriesByType('radar', function (radarSeries, idx) {
  107. if (radarSeries.get('coordinateSystem') !== 'radar' || ecModel.getComponent('radar', radarSeries.get('radarIndex')) !== radarModel) {
  108. return;
  109. }
  110. var data = radarSeries.getData();
  111. zrUtil.each(indicatorAxes, function (indicatorAxis) {
  112. indicatorAxis.scale.unionExtentFromData(data, indicatorAxis.dim);
  113. });
  114. }, this);
  115. var splitNumber = radarModel.get('splitNumber');
  116. function increaseInterval(interval) {
  117. var exp10 = Math.pow(10, Math.floor(Math.log(interval) / Math.LN10)); // Increase interval
  118. var f = interval / exp10;
  119. if (f === 2) {
  120. f = 5;
  121. } else {
  122. // f is 2 or 5
  123. f *= 2;
  124. }
  125. return f * exp10;
  126. } // Force all the axis fixing the maxSplitNumber.
  127. zrUtil.each(indicatorAxes, function (indicatorAxis, idx) {
  128. var rawExtent = getScaleExtent(indicatorAxis.scale, indicatorAxis.model);
  129. niceScaleExtent(indicatorAxis.scale, indicatorAxis.model);
  130. var axisModel = indicatorAxis.model;
  131. var scale = indicatorAxis.scale;
  132. var fixedMin = axisModel.getMin();
  133. var fixedMax = axisModel.getMax();
  134. var interval = scale.getInterval();
  135. if (fixedMin != null && fixedMax != null) {
  136. // User set min, max, divide to get new interval
  137. scale.setExtent(+fixedMin, +fixedMax);
  138. scale.setInterval((fixedMax - fixedMin) / splitNumber);
  139. } else if (fixedMin != null) {
  140. var max; // User set min, expand extent on the other side
  141. do {
  142. max = fixedMin + interval * splitNumber;
  143. scale.setExtent(+fixedMin, max); // Interval must been set after extent
  144. // FIXME
  145. scale.setInterval(interval);
  146. interval = increaseInterval(interval);
  147. } while (max < rawExtent[1] && isFinite(max) && isFinite(rawExtent[1]));
  148. } else if (fixedMax != null) {
  149. var min; // User set min, expand extent on the other side
  150. do {
  151. min = fixedMax - interval * splitNumber;
  152. scale.setExtent(min, +fixedMax);
  153. scale.setInterval(interval);
  154. interval = increaseInterval(interval);
  155. } while (min > rawExtent[0] && isFinite(min) && isFinite(rawExtent[0]));
  156. } else {
  157. var nicedSplitNumber = scale.getTicks().length - 1;
  158. if (nicedSplitNumber > splitNumber) {
  159. interval = increaseInterval(interval);
  160. } // PENDING
  161. var center = Math.round((rawExtent[0] + rawExtent[1]) / 2 / interval) * interval;
  162. var halfSplitNumber = Math.round(splitNumber / 2);
  163. scale.setExtent(numberUtil.round(center - halfSplitNumber * interval), numberUtil.round(center + (splitNumber - halfSplitNumber) * interval));
  164. scale.setInterval(interval);
  165. }
  166. });
  167. };
  168. /**
  169. * Radar dimensions is based on the data
  170. * @type {Array}
  171. */
  172. Radar.dimensions = [];
  173. Radar.create = function (ecModel, api) {
  174. var radarList = [];
  175. ecModel.eachComponent('radar', function (radarModel) {
  176. var radar = new Radar(radarModel, ecModel, api);
  177. radarList.push(radar);
  178. radarModel.coordinateSystem = radar;
  179. });
  180. ecModel.eachSeriesByType('radar', function (radarSeries) {
  181. if (radarSeries.get('coordinateSystem') === 'radar') {
  182. // Inject coordinate system
  183. radarSeries.coordinateSystem = radarList[radarSeries.get('radarIndex') || 0];
  184. }
  185. });
  186. return radarList;
  187. };
  188. CoordinateSystem.register('radar', Radar);
  189. var _default = Radar;
  190. module.exports = _default;