CartesianAxisView.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /*
  25. * Licensed to the Apache Software Foundation (ASF) under one
  26. * or more contributor license agreements. See the NOTICE file
  27. * distributed with this work for additional information
  28. * regarding copyright ownership. The ASF licenses this file
  29. * to you under the Apache License, Version 2.0 (the
  30. * "License"); you may not use this file except in compliance
  31. * with the License. You may obtain a copy of the License at
  32. *
  33. * http://www.apache.org/licenses/LICENSE-2.0
  34. *
  35. * Unless required by applicable law or agreed to in writing,
  36. * software distributed under the License is distributed on an
  37. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  38. * KIND, either express or implied. See the License for the
  39. * specific language governing permissions and limitations
  40. * under the License.
  41. */
  42. var axisBuilderAttrs = ['axisLine', 'axisTickLabel', 'axisName'];
  43. var selfBuilderAttrs = ['splitArea', 'splitLine', 'minorSplitLine'];
  44. var CartesianAxisView = AxisView.extend({
  45. type: 'cartesianAxis',
  46. axisPointerClass: 'CartesianAxisPointer',
  47. /**
  48. * @override
  49. */
  50. render: function (axisModel, ecModel, api, payload) {
  51. this.group.removeAll();
  52. var oldAxisGroup = this._axisGroup;
  53. this._axisGroup = new graphic.Group();
  54. this.group.add(this._axisGroup);
  55. if (!axisModel.get('show')) {
  56. return;
  57. }
  58. var gridModel = axisModel.getCoordSysModel();
  59. var layout = cartesianAxisHelper.layout(gridModel, axisModel);
  60. var axisBuilder = new AxisBuilder(axisModel, layout);
  61. zrUtil.each(axisBuilderAttrs, axisBuilder.add, axisBuilder);
  62. this._axisGroup.add(axisBuilder.getGroup());
  63. zrUtil.each(selfBuilderAttrs, function (name) {
  64. if (axisModel.get(name + '.show')) {
  65. this['_' + name](axisModel, gridModel);
  66. }
  67. }, this);
  68. graphic.groupTransition(oldAxisGroup, this._axisGroup, axisModel);
  69. CartesianAxisView.superCall(this, 'render', axisModel, ecModel, api, payload);
  70. },
  71. remove: function () {
  72. this._splitAreaColors = null;
  73. },
  74. /**
  75. * @param {module:echarts/coord/cartesian/AxisModel} axisModel
  76. * @param {module:echarts/coord/cartesian/GridModel} gridModel
  77. * @private
  78. */
  79. _splitLine: function (axisModel, gridModel) {
  80. var axis = axisModel.axis;
  81. if (axis.scale.isBlank()) {
  82. return;
  83. }
  84. var splitLineModel = axisModel.getModel('splitLine');
  85. var lineStyleModel = splitLineModel.getModel('lineStyle');
  86. var lineColors = lineStyleModel.get('color');
  87. lineColors = zrUtil.isArray(lineColors) ? lineColors : [lineColors];
  88. var gridRect = gridModel.coordinateSystem.getRect();
  89. var isHorizontal = axis.isHorizontal();
  90. var lineCount = 0;
  91. var ticksCoords = axis.getTicksCoords({
  92. tickModel: splitLineModel
  93. });
  94. var p1 = [];
  95. var p2 = [];
  96. var lineStyle = lineStyleModel.getLineStyle();
  97. for (var i = 0; i < ticksCoords.length; i++) {
  98. var tickCoord = axis.toGlobalCoord(ticksCoords[i].coord);
  99. if (isHorizontal) {
  100. p1[0] = tickCoord;
  101. p1[1] = gridRect.y;
  102. p2[0] = tickCoord;
  103. p2[1] = gridRect.y + gridRect.height;
  104. } else {
  105. p1[0] = gridRect.x;
  106. p1[1] = tickCoord;
  107. p2[0] = gridRect.x + gridRect.width;
  108. p2[1] = tickCoord;
  109. }
  110. var colorIndex = lineCount++ % lineColors.length;
  111. var tickValue = ticksCoords[i].tickValue;
  112. this._axisGroup.add(new graphic.Line({
  113. anid: tickValue != null ? 'line_' + ticksCoords[i].tickValue : null,
  114. subPixelOptimize: true,
  115. shape: {
  116. x1: p1[0],
  117. y1: p1[1],
  118. x2: p2[0],
  119. y2: p2[1]
  120. },
  121. style: zrUtil.defaults({
  122. stroke: lineColors[colorIndex]
  123. }, lineStyle),
  124. silent: true
  125. }));
  126. }
  127. },
  128. /**
  129. * @param {module:echarts/coord/cartesian/AxisModel} axisModel
  130. * @param {module:echarts/coord/cartesian/GridModel} gridModel
  131. * @private
  132. */
  133. _minorSplitLine: function (axisModel, gridModel) {
  134. var axis = axisModel.axis;
  135. var minorSplitLineModel = axisModel.getModel('minorSplitLine');
  136. var lineStyleModel = minorSplitLineModel.getModel('lineStyle');
  137. var gridRect = gridModel.coordinateSystem.getRect();
  138. var isHorizontal = axis.isHorizontal();
  139. var minorTicksCoords = axis.getMinorTicksCoords();
  140. if (!minorTicksCoords.length) {
  141. return;
  142. }
  143. var p1 = [];
  144. var p2 = [];
  145. var lineStyle = lineStyleModel.getLineStyle();
  146. for (var i = 0; i < minorTicksCoords.length; i++) {
  147. for (var k = 0; k < minorTicksCoords[i].length; k++) {
  148. var tickCoord = axis.toGlobalCoord(minorTicksCoords[i][k].coord);
  149. if (isHorizontal) {
  150. p1[0] = tickCoord;
  151. p1[1] = gridRect.y;
  152. p2[0] = tickCoord;
  153. p2[1] = gridRect.y + gridRect.height;
  154. } else {
  155. p1[0] = gridRect.x;
  156. p1[1] = tickCoord;
  157. p2[0] = gridRect.x + gridRect.width;
  158. p2[1] = tickCoord;
  159. }
  160. this._axisGroup.add(new graphic.Line({
  161. anid: 'minor_line_' + minorTicksCoords[i][k].tickValue,
  162. subPixelOptimize: true,
  163. shape: {
  164. x1: p1[0],
  165. y1: p1[1],
  166. x2: p2[0],
  167. y2: p2[1]
  168. },
  169. style: lineStyle,
  170. silent: true
  171. }));
  172. }
  173. }
  174. },
  175. /**
  176. * @param {module:echarts/coord/cartesian/AxisModel} axisModel
  177. * @param {module:echarts/coord/cartesian/GridModel} gridModel
  178. * @private
  179. */
  180. _splitArea: function (axisModel, gridModel) {
  181. var axis = axisModel.axis;
  182. if (axis.scale.isBlank()) {
  183. return;
  184. }
  185. var splitAreaModel = axisModel.getModel('splitArea');
  186. var areaStyleModel = splitAreaModel.getModel('areaStyle');
  187. var areaColors = areaStyleModel.get('color');
  188. var gridRect = gridModel.coordinateSystem.getRect();
  189. var ticksCoords = axis.getTicksCoords({
  190. tickModel: splitAreaModel,
  191. clamp: true
  192. });
  193. if (!ticksCoords.length) {
  194. return;
  195. } // For Making appropriate splitArea animation, the color and anid
  196. // should be corresponding to previous one if possible.
  197. var areaColorsLen = areaColors.length;
  198. var lastSplitAreaColors = this._splitAreaColors;
  199. var newSplitAreaColors = zrUtil.createHashMap();
  200. var colorIndex = 0;
  201. if (lastSplitAreaColors) {
  202. for (var i = 0; i < ticksCoords.length; i++) {
  203. var cIndex = lastSplitAreaColors.get(ticksCoords[i].tickValue);
  204. if (cIndex != null) {
  205. colorIndex = (cIndex + (areaColorsLen - 1) * i) % areaColorsLen;
  206. break;
  207. }
  208. }
  209. }
  210. var prev = axis.toGlobalCoord(ticksCoords[0].coord);
  211. var areaStyle = areaStyleModel.getAreaStyle();
  212. areaColors = zrUtil.isArray(areaColors) ? areaColors : [areaColors];
  213. for (var i = 1; i < ticksCoords.length; i++) {
  214. var tickCoord = axis.toGlobalCoord(ticksCoords[i].coord);
  215. var x;
  216. var y;
  217. var width;
  218. var height;
  219. if (axis.isHorizontal()) {
  220. x = prev;
  221. y = gridRect.y;
  222. width = tickCoord - x;
  223. height = gridRect.height;
  224. prev = x + width;
  225. } else {
  226. x = gridRect.x;
  227. y = prev;
  228. width = gridRect.width;
  229. height = tickCoord - y;
  230. prev = y + height;
  231. }
  232. var tickValue = ticksCoords[i - 1].tickValue;
  233. tickValue != null && newSplitAreaColors.set(tickValue, colorIndex);
  234. this._axisGroup.add(new graphic.Rect({
  235. anid: tickValue != null ? 'area_' + tickValue : null,
  236. shape: {
  237. x: x,
  238. y: y,
  239. width: width,
  240. height: height
  241. },
  242. style: zrUtil.defaults({
  243. fill: areaColors[colorIndex]
  244. }, areaStyle),
  245. silent: true
  246. }));
  247. colorIndex = (colorIndex + 1) % areaColorsLen;
  248. }
  249. this._splitAreaColors = newSplitAreaColors;
  250. }
  251. });
  252. CartesianAxisView.extend({
  253. type: 'xAxis'
  254. });
  255. CartesianAxisView.extend({
  256. type: 'yAxis'
  257. });