cartesianAxisHelper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. /**
  39. * Can only be called after coordinate system creation stage.
  40. * (Can be called before coordinate system update stage).
  41. *
  42. * @param {Object} opt {labelInside}
  43. * @return {Object} {
  44. * position, rotation, labelDirection, labelOffset,
  45. * tickDirection, labelRotate, z2
  46. * }
  47. */
  48. function layout(gridModel, axisModel, opt) {
  49. opt = opt || {};
  50. var grid = gridModel.coordinateSystem;
  51. var axis = axisModel.axis;
  52. var layout = {};
  53. var otherAxisOnZeroOf = axis.getAxesOnZeroOf()[0];
  54. var rawAxisPosition = axis.position;
  55. var axisPosition = otherAxisOnZeroOf ? 'onZero' : rawAxisPosition;
  56. var axisDim = axis.dim;
  57. var rect = grid.getRect();
  58. var rectBound = [rect.x, rect.x + rect.width, rect.y, rect.y + rect.height];
  59. var idx = {
  60. left: 0,
  61. right: 1,
  62. top: 0,
  63. bottom: 1,
  64. onZero: 2
  65. };
  66. var axisOffset = axisModel.get('offset') || 0;
  67. var posBound = axisDim === 'x' ? [rectBound[2] - axisOffset, rectBound[3] + axisOffset] : [rectBound[0] - axisOffset, rectBound[1] + axisOffset];
  68. if (otherAxisOnZeroOf) {
  69. var onZeroCoord = otherAxisOnZeroOf.toGlobalCoord(otherAxisOnZeroOf.dataToCoord(0));
  70. posBound[idx.onZero] = Math.max(Math.min(onZeroCoord, posBound[1]), posBound[0]);
  71. } // Axis position
  72. layout.position = [axisDim === 'y' ? posBound[idx[axisPosition]] : rectBound[0], axisDim === 'x' ? posBound[idx[axisPosition]] : rectBound[3]]; // Axis rotation
  73. layout.rotation = Math.PI / 2 * (axisDim === 'x' ? 0 : 1); // Tick and label direction, x y is axisDim
  74. var dirMap = {
  75. top: -1,
  76. bottom: 1,
  77. left: -1,
  78. right: 1
  79. };
  80. layout.labelDirection = layout.tickDirection = layout.nameDirection = dirMap[rawAxisPosition];
  81. layout.labelOffset = otherAxisOnZeroOf ? posBound[idx[rawAxisPosition]] - posBound[idx.onZero] : 0;
  82. if (axisModel.get('axisTick.inside')) {
  83. layout.tickDirection = -layout.tickDirection;
  84. }
  85. if (zrUtil.retrieve(opt.labelInside, axisModel.get('axisLabel.inside'))) {
  86. layout.labelDirection = -layout.labelDirection;
  87. } // Special label rotation
  88. var labelRotate = axisModel.get('axisLabel.rotate');
  89. layout.labelRotate = axisPosition === 'top' ? -labelRotate : labelRotate; // Over splitLine and splitArea
  90. layout.z2 = 1;
  91. return layout;
  92. }
  93. exports.layout = layout;