legacyContainLabel.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 BoundingRect from 'zrender/lib/core/BoundingRect.js';
  41. import { each } from 'zrender/lib/core/util.js';
  42. import { registerLegacyGridContainLabelImpl } from './Grid.js';
  43. import OrdinalScale from '../../scale/Ordinal.js';
  44. import { makeLabelFormatter } from '../axisHelper.js';
  45. /**
  46. * [CAUTION] Never export methods other than `installLegacyGridContainLabel`.
  47. */
  48. export function installLegacyGridContainLabel() {
  49. registerLegacyGridContainLabelImpl(legacyLayOutGridByContained);
  50. }
  51. /**
  52. * The input gridRect and axes will be modified.
  53. */
  54. function legacyLayOutGridByContained(axesList, gridRect) {
  55. each(axesList, function (axis) {
  56. if (!axis.model.get(['axisLabel', 'inside'])) {
  57. var labelUnionRect = estimateLabelUnionRect(axis);
  58. if (labelUnionRect) {
  59. var dim = axis.isHorizontal() ? 'height' : 'width';
  60. var margin = axis.model.get(['axisLabel', 'margin']);
  61. gridRect[dim] -= labelUnionRect[dim] + margin;
  62. if (axis.position === 'top') {
  63. gridRect.y += labelUnionRect.height + margin;
  64. } else if (axis.position === 'left') {
  65. gridRect.x += labelUnionRect.width + margin;
  66. }
  67. }
  68. }
  69. });
  70. }
  71. /**
  72. * @return Be null/undefined if no labels.
  73. */
  74. function estimateLabelUnionRect(axis) {
  75. var axisModel = axis.model;
  76. var scale = axis.scale;
  77. if (!axisModel.get(['axisLabel', 'show']) || scale.isBlank()) {
  78. return;
  79. }
  80. var realNumberScaleTicks;
  81. var tickCount;
  82. var categoryScaleExtent = scale.getExtent();
  83. // Optimize for large category data, avoid call `getTicks()`.
  84. if (scale instanceof OrdinalScale) {
  85. tickCount = scale.count();
  86. } else {
  87. realNumberScaleTicks = scale.getTicks();
  88. tickCount = realNumberScaleTicks.length;
  89. }
  90. var axisLabelModel = axis.getLabelModel();
  91. var labelFormatter = makeLabelFormatter(axis);
  92. var rect;
  93. var step = 1;
  94. // Simple optimization for large amount of category labels
  95. if (tickCount > 40) {
  96. step = Math.ceil(tickCount / 40);
  97. }
  98. for (var i = 0; i < tickCount; i += step) {
  99. var tick = realNumberScaleTicks ? realNumberScaleTicks[i] : {
  100. value: categoryScaleExtent[0] + i
  101. };
  102. var label = labelFormatter(tick, i);
  103. var unrotatedSingleRect = axisLabelModel.getTextRect(label);
  104. var singleRect = rotateTextRect(unrotatedSingleRect, axisLabelModel.get('rotate') || 0);
  105. rect ? rect.union(singleRect) : rect = singleRect;
  106. }
  107. return rect;
  108. function rotateTextRect(textRect, rotate) {
  109. var rotateRadians = rotate * Math.PI / 180;
  110. var beforeWidth = textRect.width;
  111. var beforeHeight = textRect.height;
  112. var afterWidth = beforeWidth * Math.abs(Math.cos(rotateRadians)) + Math.abs(beforeHeight * Math.sin(rotateRadians));
  113. var afterHeight = beforeWidth * Math.abs(Math.sin(rotateRadians)) + Math.abs(beforeHeight * Math.cos(rotateRadians));
  114. var rotatedRect = new BoundingRect(textRect.x, textRect.y, afterWidth, afterHeight);
  115. return rotatedRect;
  116. }
  117. }