Radar.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. // TODO clockwise
  41. import IndicatorAxis from './IndicatorAxis';
  42. import IntervalScale from '../../scale/Interval';
  43. import * as numberUtil from '../../util/number';
  44. import { getScaleExtent, niceScaleExtent } from '../axisHelper';
  45. import { parseAxisModelMinMax } from '../scaleRawExtentInfo';
  46. import { map, each } from 'zrender/lib/core/util';
  47. var Radar =
  48. /** @class */
  49. function () {
  50. function Radar(radarModel, ecModel, api) {
  51. /**
  52. *
  53. * Radar dimensions
  54. */
  55. this.dimensions = [];
  56. this._model = radarModel;
  57. this._indicatorAxes = map(radarModel.getIndicatorModels(), function (indicatorModel, idx) {
  58. var dim = 'indicator_' + idx;
  59. var indicatorAxis = new IndicatorAxis(dim, new IntervalScale() // (indicatorModel.get('axisType') === 'log') ? new LogScale() : new IntervalScale()
  60. );
  61. indicatorAxis.name = indicatorModel.get('name'); // Inject model and axis
  62. indicatorAxis.model = indicatorModel;
  63. indicatorModel.axis = indicatorAxis;
  64. this.dimensions.push(dim);
  65. return indicatorAxis;
  66. }, this);
  67. this.resize(radarModel, api);
  68. }
  69. Radar.prototype.getIndicatorAxes = function () {
  70. return this._indicatorAxes;
  71. };
  72. Radar.prototype.dataToPoint = function (value, indicatorIndex) {
  73. var indicatorAxis = this._indicatorAxes[indicatorIndex];
  74. return this.coordToPoint(indicatorAxis.dataToCoord(value), indicatorIndex);
  75. }; // TODO: API should be coordToPoint([coord, indicatorIndex])
  76. Radar.prototype.coordToPoint = function (coord, indicatorIndex) {
  77. var indicatorAxis = this._indicatorAxes[indicatorIndex];
  78. var angle = indicatorAxis.angle;
  79. var x = this.cx + coord * Math.cos(angle);
  80. var y = this.cy - coord * Math.sin(angle);
  81. return [x, y];
  82. };
  83. Radar.prototype.pointToData = function (pt) {
  84. var dx = pt[0] - this.cx;
  85. var dy = pt[1] - this.cy;
  86. var radius = Math.sqrt(dx * dx + dy * dy);
  87. dx /= radius;
  88. dy /= radius;
  89. var radian = Math.atan2(-dy, dx); // Find the closest angle
  90. // FIXME index can calculated directly
  91. var minRadianDiff = Infinity;
  92. var closestAxis;
  93. var closestAxisIdx = -1;
  94. for (var i = 0; i < this._indicatorAxes.length; i++) {
  95. var indicatorAxis = this._indicatorAxes[i];
  96. var diff = Math.abs(radian - indicatorAxis.angle);
  97. if (diff < minRadianDiff) {
  98. closestAxis = indicatorAxis;
  99. closestAxisIdx = i;
  100. minRadianDiff = diff;
  101. }
  102. }
  103. return [closestAxisIdx, +(closestAxis && closestAxis.coordToData(radius))];
  104. };
  105. Radar.prototype.resize = function (radarModel, api) {
  106. var center = radarModel.get('center');
  107. var viewWidth = api.getWidth();
  108. var viewHeight = api.getHeight();
  109. var viewSize = Math.min(viewWidth, viewHeight) / 2;
  110. this.cx = numberUtil.parsePercent(center[0], viewWidth);
  111. this.cy = numberUtil.parsePercent(center[1], viewHeight);
  112. this.startAngle = radarModel.get('startAngle') * Math.PI / 180; // radius may be single value like `20`, `'80%'`, or array like `[10, '80%']`
  113. var radius = radarModel.get('radius');
  114. if (typeof radius === 'string' || typeof radius === 'number') {
  115. radius = [0, radius];
  116. }
  117. this.r0 = numberUtil.parsePercent(radius[0], viewSize);
  118. this.r = numberUtil.parsePercent(radius[1], viewSize);
  119. each(this._indicatorAxes, function (indicatorAxis, idx) {
  120. indicatorAxis.setExtent(this.r0, this.r);
  121. var angle = this.startAngle + idx * Math.PI * 2 / this._indicatorAxes.length; // Normalize to [-PI, PI]
  122. angle = Math.atan2(Math.sin(angle), Math.cos(angle));
  123. indicatorAxis.angle = angle;
  124. }, this);
  125. };
  126. Radar.prototype.update = function (ecModel, api) {
  127. var indicatorAxes = this._indicatorAxes;
  128. var radarModel = this._model;
  129. each(indicatorAxes, function (indicatorAxis) {
  130. indicatorAxis.scale.setExtent(Infinity, -Infinity);
  131. });
  132. ecModel.eachSeriesByType('radar', function (radarSeries, idx) {
  133. if (radarSeries.get('coordinateSystem') !== 'radar' // @ts-ignore
  134. || ecModel.getComponent('radar', radarSeries.get('radarIndex')) !== radarModel) {
  135. return;
  136. }
  137. var data = radarSeries.getData();
  138. each(indicatorAxes, function (indicatorAxis) {
  139. indicatorAxis.scale.unionExtentFromData(data, data.mapDimension(indicatorAxis.dim));
  140. });
  141. }, this);
  142. var splitNumber = radarModel.get('splitNumber');
  143. function increaseInterval(interval) {
  144. var exp10 = Math.pow(10, Math.floor(Math.log(interval) / Math.LN10)); // Increase interval
  145. var f = interval / exp10;
  146. if (f === 2) {
  147. f = 5;
  148. } else {
  149. // f is 2 or 5
  150. f *= 2;
  151. }
  152. return f * exp10;
  153. } // Force all the axis fixing the maxSplitNumber.
  154. each(indicatorAxes, function (indicatorAxis, idx) {
  155. var rawExtent = getScaleExtent(indicatorAxis.scale, indicatorAxis.model).extent;
  156. niceScaleExtent(indicatorAxis.scale, indicatorAxis.model);
  157. var axisModel = indicatorAxis.model;
  158. var scale = indicatorAxis.scale;
  159. var fixedMin = parseAxisModelMinMax(scale, axisModel.get('min', true));
  160. var fixedMax = parseAxisModelMinMax(scale, axisModel.get('max', true));
  161. var interval = scale.getInterval();
  162. if (fixedMin != null && fixedMax != null) {
  163. // User set min, max, divide to get new interval
  164. scale.setExtent(+fixedMin, +fixedMax);
  165. scale.setInterval((fixedMax - fixedMin) / splitNumber);
  166. } else if (fixedMin != null) {
  167. var max = void 0; // User set min, expand extent on the other side
  168. do {
  169. max = fixedMin + interval * splitNumber;
  170. scale.setExtent(+fixedMin, max); // Interval must been set after extent
  171. // FIXME
  172. scale.setInterval(interval);
  173. interval = increaseInterval(interval);
  174. } while (max < rawExtent[1] && isFinite(max) && isFinite(rawExtent[1]));
  175. } else if (fixedMax != null) {
  176. var min = void 0; // User set min, expand extent on the other side
  177. do {
  178. min = fixedMax - interval * splitNumber;
  179. scale.setExtent(min, +fixedMax);
  180. scale.setInterval(interval);
  181. interval = increaseInterval(interval);
  182. } while (min > rawExtent[0] && isFinite(min) && isFinite(rawExtent[0]));
  183. } else {
  184. var nicedSplitNumber = scale.getTicks().length - 1;
  185. if (nicedSplitNumber > splitNumber) {
  186. interval = increaseInterval(interval);
  187. } // TODO
  188. var max = Math.ceil(rawExtent[1] / interval) * interval;
  189. var min = numberUtil.round(max - interval * splitNumber);
  190. scale.setExtent(min, max);
  191. scale.setInterval(interval);
  192. }
  193. });
  194. };
  195. Radar.prototype.convertToPixel = function (ecModel, finder, value) {
  196. console.warn('Not implemented.');
  197. return null;
  198. };
  199. Radar.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  200. console.warn('Not implemented.');
  201. return null;
  202. };
  203. Radar.prototype.containPoint = function (point) {
  204. console.warn('Not implemented.');
  205. return false;
  206. };
  207. Radar.create = function (ecModel, api) {
  208. var radarList = [];
  209. ecModel.eachComponent('radar', function (radarModel) {
  210. var radar = new Radar(radarModel, ecModel, api);
  211. radarList.push(radar);
  212. radarModel.coordinateSystem = radar;
  213. });
  214. ecModel.eachSeriesByType('radar', function (radarSeries) {
  215. if (radarSeries.get('coordinateSystem') === 'radar') {
  216. // Inject coordinate system
  217. // @ts-ignore
  218. radarSeries.coordinateSystem = radarList[radarSeries.get('radarIndex') || 0];
  219. }
  220. });
  221. return radarList;
  222. };
  223. /**
  224. * Radar dimensions is based on the data
  225. */
  226. Radar.dimensions = [];
  227. return Radar;
  228. }();
  229. export default Radar;