CartesianAxisView.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. var cartesianAxisHelper = require("../../coord/cartesian/cartesianAxisHelper");
  24. var _axisSplitHelper = require("./axisSplitHelper");
  25. var rectCoordAxisBuildSplitArea = _axisSplitHelper.rectCoordAxisBuildSplitArea;
  26. var rectCoordAxisHandleRemove = _axisSplitHelper.rectCoordAxisHandleRemove;
  27. /*
  28. * Licensed to the Apache Software Foundation (ASF) under one
  29. * or more contributor license agreements. See the NOTICE file
  30. * distributed with this work for additional information
  31. * regarding copyright ownership. The ASF licenses this file
  32. * to you under the Apache License, Version 2.0 (the
  33. * "License"); you may not use this file except in compliance
  34. * with the License. You may obtain a copy of the License at
  35. *
  36. * http://www.apache.org/licenses/LICENSE-2.0
  37. *
  38. * Unless required by applicable law or agreed to in writing,
  39. * software distributed under the License is distributed on an
  40. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  41. * KIND, either express or implied. See the License for the
  42. * specific language governing permissions and limitations
  43. * under the License.
  44. */
  45. var axisBuilderAttrs = ['axisLine', 'axisTickLabel', 'axisName'];
  46. var selfBuilderAttrs = ['splitArea', 'splitLine', 'minorSplitLine'];
  47. var CartesianAxisView = AxisView.extend({
  48. type: 'cartesianAxis',
  49. axisPointerClass: 'CartesianAxisPointer',
  50. /**
  51. * @override
  52. */
  53. render: function (axisModel, ecModel, api, payload) {
  54. this.group.removeAll();
  55. var oldAxisGroup = this._axisGroup;
  56. this._axisGroup = new graphic.Group();
  57. this.group.add(this._axisGroup);
  58. if (!axisModel.get('show')) {
  59. return;
  60. }
  61. var gridModel = axisModel.getCoordSysModel();
  62. var layout = cartesianAxisHelper.layout(gridModel, axisModel);
  63. var axisBuilder = new AxisBuilder(axisModel, layout);
  64. zrUtil.each(axisBuilderAttrs, axisBuilder.add, axisBuilder);
  65. this._axisGroup.add(axisBuilder.getGroup());
  66. zrUtil.each(selfBuilderAttrs, function (name) {
  67. if (axisModel.get(name + '.show')) {
  68. this['_' + name](axisModel, gridModel);
  69. }
  70. }, this);
  71. graphic.groupTransition(oldAxisGroup, this._axisGroup, axisModel);
  72. CartesianAxisView.superCall(this, 'render', axisModel, ecModel, api, payload);
  73. },
  74. remove: function () {
  75. rectCoordAxisHandleRemove(this);
  76. },
  77. /**
  78. * @param {module:echarts/coord/cartesian/AxisModel} axisModel
  79. * @param {module:echarts/coord/cartesian/GridModel} gridModel
  80. * @private
  81. */
  82. _splitLine: function (axisModel, gridModel) {
  83. var axis = axisModel.axis;
  84. if (axis.scale.isBlank()) {
  85. return;
  86. }
  87. var splitLineModel = axisModel.getModel('splitLine');
  88. var lineStyleModel = splitLineModel.getModel('lineStyle');
  89. var lineColors = lineStyleModel.get('color');
  90. lineColors = zrUtil.isArray(lineColors) ? lineColors : [lineColors];
  91. var gridRect = gridModel.coordinateSystem.getRect();
  92. var isHorizontal = axis.isHorizontal();
  93. var lineCount = 0;
  94. var ticksCoords = axis.getTicksCoords({
  95. tickModel: splitLineModel
  96. });
  97. var p1 = [];
  98. var p2 = [];
  99. var lineStyle = lineStyleModel.getLineStyle();
  100. for (var i = 0; i < ticksCoords.length; i++) {
  101. var tickCoord = axis.toGlobalCoord(ticksCoords[i].coord);
  102. if (isHorizontal) {
  103. p1[0] = tickCoord;
  104. p1[1] = gridRect.y;
  105. p2[0] = tickCoord;
  106. p2[1] = gridRect.y + gridRect.height;
  107. } else {
  108. p1[0] = gridRect.x;
  109. p1[1] = tickCoord;
  110. p2[0] = gridRect.x + gridRect.width;
  111. p2[1] = tickCoord;
  112. }
  113. var colorIndex = lineCount++ % lineColors.length;
  114. var tickValue = ticksCoords[i].tickValue;
  115. this._axisGroup.add(new graphic.Line({
  116. anid: tickValue != null ? 'line_' + ticksCoords[i].tickValue : null,
  117. subPixelOptimize: true,
  118. shape: {
  119. x1: p1[0],
  120. y1: p1[1],
  121. x2: p2[0],
  122. y2: p2[1]
  123. },
  124. style: zrUtil.defaults({
  125. stroke: lineColors[colorIndex]
  126. }, lineStyle),
  127. silent: true
  128. }));
  129. }
  130. },
  131. /**
  132. * @param {module:echarts/coord/cartesian/AxisModel} axisModel
  133. * @param {module:echarts/coord/cartesian/GridModel} gridModel
  134. * @private
  135. */
  136. _minorSplitLine: function (axisModel, gridModel) {
  137. var axis = axisModel.axis;
  138. var minorSplitLineModel = axisModel.getModel('minorSplitLine');
  139. var lineStyleModel = minorSplitLineModel.getModel('lineStyle');
  140. var gridRect = gridModel.coordinateSystem.getRect();
  141. var isHorizontal = axis.isHorizontal();
  142. var minorTicksCoords = axis.getMinorTicksCoords();
  143. if (!minorTicksCoords.length) {
  144. return;
  145. }
  146. var p1 = [];
  147. var p2 = [];
  148. var lineStyle = lineStyleModel.getLineStyle();
  149. for (var i = 0; i < minorTicksCoords.length; i++) {
  150. for (var k = 0; k < minorTicksCoords[i].length; k++) {
  151. var tickCoord = axis.toGlobalCoord(minorTicksCoords[i][k].coord);
  152. if (isHorizontal) {
  153. p1[0] = tickCoord;
  154. p1[1] = gridRect.y;
  155. p2[0] = tickCoord;
  156. p2[1] = gridRect.y + gridRect.height;
  157. } else {
  158. p1[0] = gridRect.x;
  159. p1[1] = tickCoord;
  160. p2[0] = gridRect.x + gridRect.width;
  161. p2[1] = tickCoord;
  162. }
  163. this._axisGroup.add(new graphic.Line({
  164. anid: 'minor_line_' + minorTicksCoords[i][k].tickValue,
  165. subPixelOptimize: true,
  166. shape: {
  167. x1: p1[0],
  168. y1: p1[1],
  169. x2: p2[0],
  170. y2: p2[1]
  171. },
  172. style: lineStyle,
  173. silent: true
  174. }));
  175. }
  176. }
  177. },
  178. /**
  179. * @param {module:echarts/coord/cartesian/AxisModel} axisModel
  180. * @param {module:echarts/coord/cartesian/GridModel} gridModel
  181. * @private
  182. */
  183. _splitArea: function (axisModel, gridModel) {
  184. rectCoordAxisBuildSplitArea(this, this._axisGroup, axisModel, gridModel);
  185. }
  186. });
  187. CartesianAxisView.extend({
  188. type: 'xAxis'
  189. });
  190. CartesianAxisView.extend({
  191. type: 'yAxis'
  192. });