RadiusAxisView.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 zrUtil = require("zrender/lib/core/util");
  20. var graphic = require("../../util/graphic");
  21. var AxisBuilder = require("./AxisBuilder");
  22. var AxisView = require("./AxisView");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. var axisBuilderAttrs = ['axisLine', 'axisTickLabel', 'axisName'];
  42. var selfBuilderAttrs = ['splitLine', 'splitArea', 'minorSplitLine'];
  43. var _default = AxisView.extend({
  44. type: 'radiusAxis',
  45. axisPointerClass: 'PolarAxisPointer',
  46. render: function (radiusAxisModel, ecModel) {
  47. this.group.removeAll();
  48. if (!radiusAxisModel.get('show')) {
  49. return;
  50. }
  51. var radiusAxis = radiusAxisModel.axis;
  52. var polar = radiusAxis.polar;
  53. var angleAxis = polar.getAngleAxis();
  54. var ticksCoords = radiusAxis.getTicksCoords();
  55. var minorTicksCoords = radiusAxis.getMinorTicksCoords();
  56. var axisAngle = angleAxis.getExtent()[0];
  57. var radiusExtent = radiusAxis.getExtent();
  58. var layout = layoutAxis(polar, radiusAxisModel, axisAngle);
  59. var axisBuilder = new AxisBuilder(radiusAxisModel, layout);
  60. zrUtil.each(axisBuilderAttrs, axisBuilder.add, axisBuilder);
  61. this.group.add(axisBuilder.getGroup());
  62. zrUtil.each(selfBuilderAttrs, function (name) {
  63. if (radiusAxisModel.get(name + '.show') && !radiusAxis.scale.isBlank()) {
  64. this['_' + name](radiusAxisModel, polar, axisAngle, radiusExtent, ticksCoords, minorTicksCoords);
  65. }
  66. }, this);
  67. },
  68. /**
  69. * @private
  70. */
  71. _splitLine: function (radiusAxisModel, polar, axisAngle, radiusExtent, ticksCoords) {
  72. var splitLineModel = radiusAxisModel.getModel('splitLine');
  73. var lineStyleModel = splitLineModel.getModel('lineStyle');
  74. var lineColors = lineStyleModel.get('color');
  75. var lineCount = 0;
  76. lineColors = lineColors instanceof Array ? lineColors : [lineColors];
  77. var splitLines = [];
  78. for (var i = 0; i < ticksCoords.length; i++) {
  79. var colorIndex = lineCount++ % lineColors.length;
  80. splitLines[colorIndex] = splitLines[colorIndex] || [];
  81. splitLines[colorIndex].push(new graphic.Circle({
  82. shape: {
  83. cx: polar.cx,
  84. cy: polar.cy,
  85. r: ticksCoords[i].coord
  86. }
  87. }));
  88. } // Simple optimization
  89. // Batching the lines if color are the same
  90. for (var i = 0; i < splitLines.length; i++) {
  91. this.group.add(graphic.mergePath(splitLines[i], {
  92. style: zrUtil.defaults({
  93. stroke: lineColors[i % lineColors.length],
  94. fill: null
  95. }, lineStyleModel.getLineStyle()),
  96. silent: true
  97. }));
  98. }
  99. },
  100. /**
  101. * @private
  102. */
  103. _minorSplitLine: function (radiusAxisModel, polar, axisAngle, radiusExtent, ticksCoords, minorTicksCoords) {
  104. if (!minorTicksCoords.length) {
  105. return;
  106. }
  107. var minorSplitLineModel = radiusAxisModel.getModel('minorSplitLine');
  108. var lineStyleModel = minorSplitLineModel.getModel('lineStyle');
  109. var lines = [];
  110. for (var i = 0; i < minorTicksCoords.length; i++) {
  111. for (var k = 0; k < minorTicksCoords[i].length; k++) {
  112. lines.push(new graphic.Circle({
  113. shape: {
  114. cx: polar.cx,
  115. cy: polar.cy,
  116. r: minorTicksCoords[i][k].coord
  117. }
  118. }));
  119. }
  120. }
  121. this.group.add(graphic.mergePath(lines, {
  122. style: zrUtil.defaults({
  123. fill: null
  124. }, lineStyleModel.getLineStyle()),
  125. silent: true
  126. }));
  127. },
  128. /**
  129. * @private
  130. */
  131. _splitArea: function (radiusAxisModel, polar, axisAngle, radiusExtent, ticksCoords) {
  132. if (!ticksCoords.length) {
  133. return;
  134. }
  135. var splitAreaModel = radiusAxisModel.getModel('splitArea');
  136. var areaStyleModel = splitAreaModel.getModel('areaStyle');
  137. var areaColors = areaStyleModel.get('color');
  138. var lineCount = 0;
  139. areaColors = areaColors instanceof Array ? areaColors : [areaColors];
  140. var splitAreas = [];
  141. var prevRadius = ticksCoords[0].coord;
  142. for (var i = 1; i < ticksCoords.length; i++) {
  143. var colorIndex = lineCount++ % areaColors.length;
  144. splitAreas[colorIndex] = splitAreas[colorIndex] || [];
  145. splitAreas[colorIndex].push(new graphic.Sector({
  146. shape: {
  147. cx: polar.cx,
  148. cy: polar.cy,
  149. r0: prevRadius,
  150. r: ticksCoords[i].coord,
  151. startAngle: 0,
  152. endAngle: Math.PI * 2
  153. },
  154. silent: true
  155. }));
  156. prevRadius = ticksCoords[i].coord;
  157. } // Simple optimization
  158. // Batching the lines if color are the same
  159. for (var i = 0; i < splitAreas.length; i++) {
  160. this.group.add(graphic.mergePath(splitAreas[i], {
  161. style: zrUtil.defaults({
  162. fill: areaColors[i % areaColors.length]
  163. }, areaStyleModel.getAreaStyle()),
  164. silent: true
  165. }));
  166. }
  167. }
  168. });
  169. /**
  170. * @inner
  171. */
  172. function layoutAxis(polar, radiusAxisModel, axisAngle) {
  173. return {
  174. position: [polar.cx, polar.cy],
  175. rotation: axisAngle / 180 * Math.PI,
  176. labelDirection: -1,
  177. tickDirection: -1,
  178. nameDirection: 1,
  179. labelRotate: radiusAxisModel.getModel('axisLabel').get('rotate'),
  180. // Over splitLine and splitArea
  181. z2: 1
  182. };
  183. }
  184. module.exports = _default;