cartesianAxisHelper.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import * as zrUtil from 'zrender/lib/core/util.js';
  41. import { SINGLE_REFERRING } from '../../util/model.js';
  42. /**
  43. * Can only be called after coordinate system creation stage.
  44. * (Can be called before coordinate system update stage).
  45. */
  46. export function layout(gridModel, axisModel, opt) {
  47. opt = opt || {};
  48. var grid = gridModel.coordinateSystem;
  49. var axis = axisModel.axis;
  50. var layout = {};
  51. var otherAxisOnZeroOf = axis.getAxesOnZeroOf()[0];
  52. var rawAxisPosition = axis.position;
  53. var axisPosition = otherAxisOnZeroOf ? 'onZero' : rawAxisPosition;
  54. var axisDim = axis.dim;
  55. var rect = grid.getRect();
  56. var rectBound = [rect.x, rect.x + rect.width, rect.y, rect.y + rect.height];
  57. var idx = {
  58. left: 0,
  59. right: 1,
  60. top: 0,
  61. bottom: 1,
  62. onZero: 2
  63. };
  64. var axisOffset = axisModel.get('offset') || 0;
  65. var posBound = axisDim === 'x' ? [rectBound[2] - axisOffset, rectBound[3] + axisOffset] : [rectBound[0] - axisOffset, rectBound[1] + axisOffset];
  66. if (otherAxisOnZeroOf) {
  67. var onZeroCoord = otherAxisOnZeroOf.toGlobalCoord(otherAxisOnZeroOf.dataToCoord(0));
  68. posBound[idx.onZero] = Math.max(Math.min(onZeroCoord, posBound[1]), posBound[0]);
  69. }
  70. // Axis position
  71. layout.position = [axisDim === 'y' ? posBound[idx[axisPosition]] : rectBound[0], axisDim === 'x' ? posBound[idx[axisPosition]] : rectBound[3]];
  72. // Axis rotation
  73. layout.rotation = Math.PI / 2 * (axisDim === 'x' ? 0 : 1);
  74. // Tick and label direction, x y is axisDim
  75. var dirMap = {
  76. top: -1,
  77. bottom: 1,
  78. left: -1,
  79. right: 1
  80. };
  81. layout.labelDirection = layout.tickDirection = layout.nameDirection = dirMap[rawAxisPosition];
  82. layout.labelOffset = otherAxisOnZeroOf ? posBound[idx[rawAxisPosition]] - posBound[idx.onZero] : 0;
  83. if (axisModel.get(['axisTick', 'inside'])) {
  84. layout.tickDirection = -layout.tickDirection;
  85. }
  86. if (zrUtil.retrieve(opt.labelInside, axisModel.get(['axisLabel', 'inside']))) {
  87. layout.labelDirection = -layout.labelDirection;
  88. }
  89. // Special label rotation
  90. var labelRotate = axisModel.get(['axisLabel', 'rotate']);
  91. layout.labelRotate = axisPosition === 'top' ? -labelRotate : labelRotate;
  92. // Over splitLine and splitArea
  93. layout.z2 = 1;
  94. return layout;
  95. }
  96. export function isCartesian2DSeries(seriesModel) {
  97. return seriesModel.get('coordinateSystem') === 'cartesian2d';
  98. }
  99. export function findAxisModels(seriesModel) {
  100. var axisModelMap = {
  101. xAxisModel: null,
  102. yAxisModel: null
  103. };
  104. zrUtil.each(axisModelMap, function (v, key) {
  105. var axisType = key.replace(/Model$/, '');
  106. var axisModel = seriesModel.getReferringComponents(axisType, SINGLE_REFERRING).models[0];
  107. if (process.env.NODE_ENV !== 'production') {
  108. if (!axisModel) {
  109. throw new Error(axisType + ' "' + zrUtil.retrieve3(seriesModel.get(axisType + 'Index'), seriesModel.get(axisType + 'Id'), 0) + '" not found');
  110. }
  111. }
  112. axisModelMap[key] = axisModel;
  113. });
  114. return axisModelMap;
  115. }