SingleAxisView.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 AxisBuilder = require("./AxisBuilder");
  21. var graphic = require("../../util/graphic");
  22. var singleAxisHelper = require("../../coord/single/singleAxisHelper");
  23. var AxisView = require("./AxisView");
  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'];
  47. var SingleAxisView = AxisView.extend({
  48. type: 'singleAxis',
  49. axisPointerClass: 'SingleAxisPointer',
  50. render: function (axisModel, ecModel, api, payload) {
  51. var group = this.group;
  52. group.removeAll();
  53. var oldAxisGroup = this._axisGroup;
  54. this._axisGroup = new graphic.Group();
  55. var layout = singleAxisHelper.layout(axisModel);
  56. var axisBuilder = new AxisBuilder(axisModel, layout);
  57. zrUtil.each(axisBuilderAttrs, axisBuilder.add, axisBuilder);
  58. group.add(this._axisGroup);
  59. group.add(axisBuilder.getGroup());
  60. zrUtil.each(selfBuilderAttrs, function (name) {
  61. if (axisModel.get(name + '.show')) {
  62. this['_' + name](axisModel);
  63. }
  64. }, this);
  65. graphic.groupTransition(oldAxisGroup, this._axisGroup, axisModel);
  66. SingleAxisView.superCall(this, 'render', axisModel, ecModel, api, payload);
  67. },
  68. remove: function () {
  69. rectCoordAxisHandleRemove(this);
  70. },
  71. _splitLine: function (axisModel) {
  72. var axis = axisModel.axis;
  73. if (axis.scale.isBlank()) {
  74. return;
  75. }
  76. var splitLineModel = axisModel.getModel('splitLine');
  77. var lineStyleModel = splitLineModel.getModel('lineStyle');
  78. var lineWidth = lineStyleModel.get('width');
  79. var lineColors = lineStyleModel.get('color');
  80. lineColors = lineColors instanceof Array ? lineColors : [lineColors];
  81. var gridRect = axisModel.coordinateSystem.getRect();
  82. var isHorizontal = axis.isHorizontal();
  83. var splitLines = [];
  84. var lineCount = 0;
  85. var ticksCoords = axis.getTicksCoords({
  86. tickModel: splitLineModel
  87. });
  88. var p1 = [];
  89. var p2 = [];
  90. for (var i = 0; i < ticksCoords.length; ++i) {
  91. var tickCoord = axis.toGlobalCoord(ticksCoords[i].coord);
  92. if (isHorizontal) {
  93. p1[0] = tickCoord;
  94. p1[1] = gridRect.y;
  95. p2[0] = tickCoord;
  96. p2[1] = gridRect.y + gridRect.height;
  97. } else {
  98. p1[0] = gridRect.x;
  99. p1[1] = tickCoord;
  100. p2[0] = gridRect.x + gridRect.width;
  101. p2[1] = tickCoord;
  102. }
  103. var colorIndex = lineCount++ % lineColors.length;
  104. splitLines[colorIndex] = splitLines[colorIndex] || [];
  105. splitLines[colorIndex].push(new graphic.Line({
  106. subPixelOptimize: true,
  107. shape: {
  108. x1: p1[0],
  109. y1: p1[1],
  110. x2: p2[0],
  111. y2: p2[1]
  112. },
  113. style: {
  114. lineWidth: lineWidth
  115. },
  116. silent: true
  117. }));
  118. }
  119. for (var i = 0; i < splitLines.length; ++i) {
  120. this.group.add(graphic.mergePath(splitLines[i], {
  121. style: {
  122. stroke: lineColors[i % lineColors.length],
  123. lineDash: lineStyleModel.getLineDash(lineWidth),
  124. lineWidth: lineWidth
  125. },
  126. silent: true
  127. }));
  128. }
  129. },
  130. _splitArea: function (axisModel) {
  131. rectCoordAxisBuildSplitArea(this, this._axisGroup, axisModel, axisModel);
  132. }
  133. });
  134. var _default = SingleAxisView;
  135. module.exports = _default;