SingleAxisView.js 4.3 KB

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