Radar.js 7.2 KB

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