polarCreator.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 _config = require("../../config");
  20. var __DEV__ = _config.__DEV__;
  21. var zrUtil = require("zrender/lib/core/util");
  22. var Polar = require("./Polar");
  23. var _number = require("../../util/number");
  24. var parsePercent = _number.parsePercent;
  25. var _axisHelper = require("../../coord/axisHelper");
  26. var createScaleByModel = _axisHelper.createScaleByModel;
  27. var niceScaleExtent = _axisHelper.niceScaleExtent;
  28. var CoordinateSystem = require("../../CoordinateSystem");
  29. var _dataStackHelper = require("../../data/helper/dataStackHelper");
  30. var getStackedDimension = _dataStackHelper.getStackedDimension;
  31. require("./PolarModel");
  32. /*
  33. * Licensed to the Apache Software Foundation (ASF) under one
  34. * or more contributor license agreements. See the NOTICE file
  35. * distributed with this work for additional information
  36. * regarding copyright ownership. The ASF licenses this file
  37. * to you under the Apache License, Version 2.0 (the
  38. * "License"); you may not use this file except in compliance
  39. * with the License. You may obtain a copy of the License at
  40. *
  41. * http://www.apache.org/licenses/LICENSE-2.0
  42. *
  43. * Unless required by applicable law or agreed to in writing,
  44. * software distributed under the License is distributed on an
  45. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  46. * KIND, either express or implied. See the License for the
  47. * specific language governing permissions and limitations
  48. * under the License.
  49. */
  50. // TODO Axis scale
  51. /**
  52. * Resize method bound to the polar
  53. * @param {module:echarts/coord/polar/PolarModel} polarModel
  54. * @param {module:echarts/ExtensionAPI} api
  55. */
  56. function resizePolar(polar, polarModel, api) {
  57. var center = polarModel.get('center');
  58. var width = api.getWidth();
  59. var height = api.getHeight();
  60. polar.cx = parsePercent(center[0], width);
  61. polar.cy = parsePercent(center[1], height);
  62. var radiusAxis = polar.getRadiusAxis();
  63. var size = Math.min(width, height) / 2;
  64. var radius = polarModel.get('radius');
  65. if (radius == null) {
  66. radius = [0, '100%'];
  67. } else if (!zrUtil.isArray(radius)) {
  68. // r0 = 0
  69. radius = [0, radius];
  70. }
  71. radius = [parsePercent(radius[0], size), parsePercent(radius[1], size)];
  72. radiusAxis.inverse ? radiusAxis.setExtent(radius[1], radius[0]) : radiusAxis.setExtent(radius[0], radius[1]);
  73. }
  74. /**
  75. * Update polar
  76. */
  77. function updatePolarScale(ecModel, api) {
  78. var polar = this;
  79. var angleAxis = polar.getAngleAxis();
  80. var radiusAxis = polar.getRadiusAxis(); // Reset scale
  81. angleAxis.scale.setExtent(Infinity, -Infinity);
  82. radiusAxis.scale.setExtent(Infinity, -Infinity);
  83. ecModel.eachSeries(function (seriesModel) {
  84. if (seriesModel.coordinateSystem === polar) {
  85. var data = seriesModel.getData();
  86. zrUtil.each(data.mapDimension('radius', true), function (dim) {
  87. radiusAxis.scale.unionExtentFromData(data, getStackedDimension(data, dim));
  88. });
  89. zrUtil.each(data.mapDimension('angle', true), function (dim) {
  90. angleAxis.scale.unionExtentFromData(data, getStackedDimension(data, dim));
  91. });
  92. }
  93. });
  94. niceScaleExtent(angleAxis.scale, angleAxis.model);
  95. niceScaleExtent(radiusAxis.scale, radiusAxis.model); // Fix extent of category angle axis
  96. if (angleAxis.type === 'category' && !angleAxis.onBand) {
  97. var extent = angleAxis.getExtent();
  98. var diff = 360 / angleAxis.scale.count();
  99. angleAxis.inverse ? extent[1] += diff : extent[1] -= diff;
  100. angleAxis.setExtent(extent[0], extent[1]);
  101. }
  102. }
  103. /**
  104. * Set common axis properties
  105. * @param {module:echarts/coord/polar/AngleAxis|module:echarts/coord/polar/RadiusAxis}
  106. * @param {module:echarts/coord/polar/AxisModel}
  107. * @inner
  108. */
  109. function setAxis(axis, axisModel) {
  110. axis.type = axisModel.get('type');
  111. axis.scale = createScaleByModel(axisModel);
  112. axis.onBand = axisModel.get('boundaryGap') && axis.type === 'category';
  113. axis.inverse = axisModel.get('inverse');
  114. if (axisModel.mainType === 'angleAxis') {
  115. axis.inverse ^= axisModel.get('clockwise');
  116. var startAngle = axisModel.get('startAngle');
  117. axis.setExtent(startAngle, startAngle + (axis.inverse ? -360 : 360));
  118. } // Inject axis instance
  119. axisModel.axis = axis;
  120. axis.model = axisModel;
  121. }
  122. var polarCreator = {
  123. dimensions: Polar.prototype.dimensions,
  124. create: function (ecModel, api) {
  125. var polarList = [];
  126. ecModel.eachComponent('polar', function (polarModel, idx) {
  127. var polar = new Polar(idx); // Inject resize and update method
  128. polar.update = updatePolarScale;
  129. var radiusAxis = polar.getRadiusAxis();
  130. var angleAxis = polar.getAngleAxis();
  131. var radiusAxisModel = polarModel.findAxisModel('radiusAxis');
  132. var angleAxisModel = polarModel.findAxisModel('angleAxis');
  133. setAxis(radiusAxis, radiusAxisModel);
  134. setAxis(angleAxis, angleAxisModel);
  135. resizePolar(polar, polarModel, api);
  136. polarList.push(polar);
  137. polarModel.coordinateSystem = polar;
  138. polar.model = polarModel;
  139. }); // Inject coordinateSystem to series
  140. ecModel.eachSeries(function (seriesModel) {
  141. if (seriesModel.get('coordinateSystem') === 'polar') {
  142. var polarModel = ecModel.queryComponents({
  143. mainType: 'polar',
  144. index: seriesModel.get('polarIndex'),
  145. id: seriesModel.get('polarId')
  146. })[0];
  147. seriesModel.coordinateSystem = polarModel.coordinateSystem;
  148. }
  149. });
  150. return polarList;
  151. }
  152. };
  153. CoordinateSystem.register('polar', polarCreator);