cartesianAxisHelper.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. import AxisBuilder from '../../component/axis/AxisBuilder.js';
  43. import { isIntervalOrLogScale } from '../../scale/helper.js';
  44. /**
  45. * [__CAUTION__]
  46. * MUST guarantee: if only the input `rect` and `axis.extent` changed,
  47. * only `layout.position` changes.
  48. * This character is replied on `grid.contain` calculation in `AxisBuilder`.
  49. * @see updateCartesianAxisViewCommonPartBuilder
  50. *
  51. * Can only be called after coordinate system creation stage.
  52. * (Can be called before coordinate system update stage).
  53. */
  54. export function layout(rect, axisModel, opt) {
  55. opt = opt || {};
  56. var axis = axisModel.axis;
  57. var layout = {};
  58. var otherAxisOnZeroOf = axis.getAxesOnZeroOf()[0];
  59. var rawAxisPosition = axis.position;
  60. var axisPosition = otherAxisOnZeroOf ? 'onZero' : rawAxisPosition;
  61. var axisDim = axis.dim;
  62. var rectBound = [rect.x, rect.x + rect.width, rect.y, rect.y + rect.height];
  63. var idx = {
  64. left: 0,
  65. right: 1,
  66. top: 0,
  67. bottom: 1,
  68. onZero: 2
  69. };
  70. var axisOffset = axisModel.get('offset') || 0;
  71. var posBound = axisDim === 'x' ? [rectBound[2] - axisOffset, rectBound[3] + axisOffset] : [rectBound[0] - axisOffset, rectBound[1] + axisOffset];
  72. if (otherAxisOnZeroOf) {
  73. var onZeroCoord = otherAxisOnZeroOf.toGlobalCoord(otherAxisOnZeroOf.dataToCoord(0));
  74. posBound[idx.onZero] = Math.max(Math.min(onZeroCoord, posBound[1]), posBound[0]);
  75. }
  76. // Axis position
  77. layout.position = [axisDim === 'y' ? posBound[idx[axisPosition]] : rectBound[0], axisDim === 'x' ? posBound[idx[axisPosition]] : rectBound[3]];
  78. // Axis rotation
  79. layout.rotation = Math.PI / 2 * (axisDim === 'x' ? 0 : 1);
  80. // Tick and label direction, x y is axisDim
  81. var dirMap = {
  82. top: -1,
  83. bottom: 1,
  84. left: -1,
  85. right: 1
  86. };
  87. layout.labelDirection = layout.tickDirection = layout.nameDirection = dirMap[rawAxisPosition];
  88. layout.labelOffset = otherAxisOnZeroOf ? posBound[idx[rawAxisPosition]] - posBound[idx.onZero] : 0;
  89. if (axisModel.get(['axisTick', 'inside'])) {
  90. layout.tickDirection = -layout.tickDirection;
  91. }
  92. if (zrUtil.retrieve(opt.labelInside, axisModel.get(['axisLabel', 'inside']))) {
  93. layout.labelDirection = -layout.labelDirection;
  94. }
  95. // Special label rotation
  96. var labelRotate = axisModel.get(['axisLabel', 'rotate']);
  97. layout.labelRotate = axisPosition === 'top' ? -labelRotate : labelRotate;
  98. // Over splitLine and splitArea
  99. layout.z2 = 1;
  100. return layout;
  101. }
  102. export function isCartesian2DDeclaredSeries(seriesModel) {
  103. return seriesModel.get('coordinateSystem') === 'cartesian2d';
  104. }
  105. /**
  106. * Note: If pie (or other similar series) use cartesian2d, here
  107. * option `seriesModel.get('coordinateSystem') === 'cartesian2d'`
  108. * and `seriesModel.coordinateSystem !== cartesian2dCoordSysInstance`
  109. * and `seriesModel.boxCoordinateSystem === cartesian2dCoordSysInstance`,
  110. * the logic below is probably wrong, therefore skip it temporarily.
  111. */
  112. export function isCartesian2DInjectedAsDataCoordSys(seriesModel) {
  113. return seriesModel.coordinateSystem && seriesModel.coordinateSystem.type === 'cartesian2d';
  114. }
  115. export function findAxisModels(seriesModel) {
  116. var axisModelMap = {
  117. xAxisModel: null,
  118. yAxisModel: null
  119. };
  120. zrUtil.each(axisModelMap, function (v, key) {
  121. var axisType = key.replace(/Model$/, '');
  122. var axisModel = seriesModel.getReferringComponents(axisType, SINGLE_REFERRING).models[0];
  123. if (process.env.NODE_ENV !== 'production') {
  124. if (!axisModel) {
  125. throw new Error(axisType + ' "' + zrUtil.retrieve3(seriesModel.get(axisType + 'Index'), seriesModel.get(axisType + 'Id'), 0) + '" not found');
  126. }
  127. }
  128. axisModelMap[key] = axisModel;
  129. });
  130. return axisModelMap;
  131. }
  132. export function createCartesianAxisViewCommonPartBuilder(gridRect, cartesians, axisModel, api, ctx, defaultNameMoveOverlap) {
  133. var layoutResult = layout(gridRect, axisModel);
  134. var axisLineAutoShow = false;
  135. var axisTickAutoShow = false;
  136. // Not show axisTick or axisLine if other axis is category / time
  137. for (var i = 0; i < cartesians.length; i++) {
  138. if (isIntervalOrLogScale(cartesians[i].getOtherAxis(axisModel.axis).scale)) {
  139. // Still show axis tick or axisLine if other axis is value / log
  140. axisLineAutoShow = axisTickAutoShow = true;
  141. if (axisModel.axis.type === 'category' && axisModel.axis.onBand) {
  142. axisTickAutoShow = false;
  143. }
  144. }
  145. }
  146. layoutResult.axisLineAutoShow = axisLineAutoShow;
  147. layoutResult.axisTickAutoShow = axisTickAutoShow;
  148. layoutResult.defaultNameMoveOverlap = defaultNameMoveOverlap;
  149. return new AxisBuilder(axisModel, api, layoutResult, ctx);
  150. }
  151. export function updateCartesianAxisViewCommonPartBuilder(axisBuilder, gridRect, axisModel) {
  152. var newRaw = layout(gridRect, axisModel);
  153. if (process.env.NODE_ENV !== 'production') {
  154. var oldRaw_1 = axisBuilder.__getRawCfg();
  155. zrUtil.each(zrUtil.keys(newRaw), function (prop) {
  156. if (prop !== 'position' && prop !== 'labelOffset') {
  157. zrUtil.assert(newRaw[prop] === oldRaw_1[prop]);
  158. }
  159. });
  160. }
  161. axisBuilder.updateCfg(newRaw);
  162. }