Radar.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.js';
  42. import IntervalScale from '../../scale/Interval.js';
  43. import * as numberUtil from '../../util/number.js';
  44. import { map, each, isString, isNumber } from 'zrender/lib/core/util.js';
  45. import { alignScaleTicks } from '../axisAlignTicks.js';
  46. var Radar = /** @class */function () {
  47. function Radar(radarModel, ecModel, api) {
  48. /**
  49. *
  50. * Radar dimensions
  51. */
  52. this.dimensions = [];
  53. this._model = radarModel;
  54. this._indicatorAxes = map(radarModel.getIndicatorModels(), function (indicatorModel, idx) {
  55. var dim = 'indicator_' + idx;
  56. var indicatorAxis = new IndicatorAxis(dim, new IntervalScale()
  57. // (indicatorModel.get('axisType') === 'log') ? new LogScale() : new IntervalScale()
  58. );
  59. indicatorAxis.name = indicatorModel.get('name');
  60. // Inject model and axis
  61. indicatorAxis.model = indicatorModel;
  62. indicatorModel.axis = indicatorAxis;
  63. this.dimensions.push(dim);
  64. return indicatorAxis;
  65. }, this);
  66. this.resize(radarModel, api);
  67. }
  68. Radar.prototype.getIndicatorAxes = function () {
  69. return this._indicatorAxes;
  70. };
  71. Radar.prototype.dataToPoint = function (value, indicatorIndex) {
  72. var indicatorAxis = this._indicatorAxes[indicatorIndex];
  73. return this.coordToPoint(indicatorAxis.dataToCoord(value), indicatorIndex);
  74. };
  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);
  90. // Find the closest angle
  91. // FIXME index can calculated directly
  92. var minRadianDiff = Infinity;
  93. var closestAxis;
  94. var closestAxisIdx = -1;
  95. for (var i = 0; i < this._indicatorAxes.length; i++) {
  96. var indicatorAxis = this._indicatorAxes[i];
  97. var diff = Math.abs(radian - indicatorAxis.angle);
  98. if (diff < minRadianDiff) {
  99. closestAxis = indicatorAxis;
  100. closestAxisIdx = i;
  101. minRadianDiff = diff;
  102. }
  103. }
  104. return [closestAxisIdx, +(closestAxis && closestAxis.coordToData(radius))];
  105. };
  106. Radar.prototype.resize = function (radarModel, api) {
  107. var center = radarModel.get('center');
  108. var viewWidth = api.getWidth();
  109. var viewHeight = api.getHeight();
  110. var viewSize = Math.min(viewWidth, viewHeight) / 2;
  111. this.cx = numberUtil.parsePercent(center[0], viewWidth);
  112. this.cy = numberUtil.parsePercent(center[1], viewHeight);
  113. this.startAngle = radarModel.get('startAngle') * Math.PI / 180;
  114. // radius may be single value like `20`, `'80%'`, or array like `[10, '80%']`
  115. var radius = radarModel.get('radius');
  116. if (isString(radius) || isNumber(radius)) {
  117. radius = [0, radius];
  118. }
  119. this.r0 = numberUtil.parsePercent(radius[0], viewSize);
  120. this.r = numberUtil.parsePercent(radius[1], viewSize);
  121. each(this._indicatorAxes, function (indicatorAxis, idx) {
  122. indicatorAxis.setExtent(this.r0, this.r);
  123. var angle = this.startAngle + idx * Math.PI * 2 / this._indicatorAxes.length;
  124. // Normalize to [-PI, PI]
  125. angle = Math.atan2(Math.sin(angle), Math.cos(angle));
  126. indicatorAxis.angle = angle;
  127. }, this);
  128. };
  129. Radar.prototype.update = function (ecModel, api) {
  130. var indicatorAxes = this._indicatorAxes;
  131. var radarModel = this._model;
  132. each(indicatorAxes, function (indicatorAxis) {
  133. indicatorAxis.scale.setExtent(Infinity, -Infinity);
  134. });
  135. ecModel.eachSeriesByType('radar', function (radarSeries, idx) {
  136. if (radarSeries.get('coordinateSystem') !== 'radar'
  137. // @ts-ignore
  138. || ecModel.getComponent('radar', radarSeries.get('radarIndex')) !== radarModel) {
  139. return;
  140. }
  141. var data = radarSeries.getData();
  142. each(indicatorAxes, function (indicatorAxis) {
  143. indicatorAxis.scale.unionExtentFromData(data, data.mapDimension(indicatorAxis.dim));
  144. });
  145. }, this);
  146. var splitNumber = radarModel.get('splitNumber');
  147. var dummyScale = new IntervalScale();
  148. dummyScale.setExtent(0, splitNumber);
  149. dummyScale.setInterval(1);
  150. // Force all the axis fixing the maxSplitNumber.
  151. each(indicatorAxes, function (indicatorAxis, idx) {
  152. alignScaleTicks(indicatorAxis.scale, indicatorAxis.model, dummyScale);
  153. });
  154. };
  155. Radar.prototype.convertToPixel = function (ecModel, finder, value) {
  156. console.warn('Not implemented.');
  157. return null;
  158. };
  159. Radar.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  160. console.warn('Not implemented.');
  161. return null;
  162. };
  163. Radar.prototype.containPoint = function (point) {
  164. console.warn('Not implemented.');
  165. return false;
  166. };
  167. Radar.create = function (ecModel, api) {
  168. var radarList = [];
  169. ecModel.eachComponent('radar', function (radarModel) {
  170. var radar = new Radar(radarModel, ecModel, api);
  171. radarList.push(radar);
  172. radarModel.coordinateSystem = radar;
  173. });
  174. ecModel.eachSeriesByType('radar', function (radarSeries) {
  175. if (radarSeries.get('coordinateSystem') === 'radar') {
  176. // Inject coordinate system
  177. // @ts-ignore
  178. radarSeries.coordinateSystem = radarList[radarSeries.get('radarIndex') || 0];
  179. }
  180. });
  181. return radarList;
  182. };
  183. /**
  184. * Radar dimensions is based on the data
  185. */
  186. Radar.dimensions = [];
  187. return Radar;
  188. }();
  189. export default Radar;