AngleAxis.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 textContain = require("zrender/lib/contain/text");
  21. var Axis = require("../Axis");
  22. var _model = require("../../util/model");
  23. var makeInner = _model.makeInner;
  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 inner = makeInner();
  43. function AngleAxis(scale, angleExtent) {
  44. angleExtent = angleExtent || [0, 360];
  45. Axis.call(this, 'angle', scale, angleExtent);
  46. /**
  47. * Axis type
  48. * - 'category'
  49. * - 'value'
  50. * - 'time'
  51. * - 'log'
  52. * @type {string}
  53. */
  54. this.type = 'category';
  55. }
  56. AngleAxis.prototype = {
  57. constructor: AngleAxis,
  58. /**
  59. * @override
  60. */
  61. pointToData: function (point, clamp) {
  62. return this.polar.pointToData(point, clamp)[this.dim === 'radius' ? 0 : 1];
  63. },
  64. dataToAngle: Axis.prototype.dataToCoord,
  65. angleToData: Axis.prototype.coordToData,
  66. /**
  67. * Only be called in category axis.
  68. * Angle axis uses text height to decide interval
  69. *
  70. * @override
  71. * @return {number} Auto interval for cateogry axis tick and label
  72. */
  73. calculateCategoryInterval: function () {
  74. var axis = this;
  75. var labelModel = axis.getLabelModel();
  76. var ordinalScale = axis.scale;
  77. var ordinalExtent = ordinalScale.getExtent(); // Providing this method is for optimization:
  78. // avoid generating a long array by `getTicks`
  79. // in large category data case.
  80. var tickCount = ordinalScale.count();
  81. if (ordinalExtent[1] - ordinalExtent[0] < 1) {
  82. return 0;
  83. }
  84. var tickValue = ordinalExtent[0];
  85. var unitSpan = axis.dataToCoord(tickValue + 1) - axis.dataToCoord(tickValue);
  86. var unitH = Math.abs(unitSpan); // Not precise, just use height as text width
  87. // and each distance from axis line yet.
  88. var rect = textContain.getBoundingRect(tickValue, labelModel.getFont(), 'center', 'top');
  89. var maxH = Math.max(rect.height, 7);
  90. var dh = maxH / unitH; // 0/0 is NaN, 1/0 is Infinity.
  91. isNaN(dh) && (dh = Infinity);
  92. var interval = Math.max(0, Math.floor(dh));
  93. var cache = inner(axis.model);
  94. var lastAutoInterval = cache.lastAutoInterval;
  95. var lastTickCount = cache.lastTickCount; // Use cache to keep interval stable while moving zoom window,
  96. // otherwise the calculated interval might jitter when the zoom
  97. // window size is close to the interval-changing size.
  98. if (lastAutoInterval != null && lastTickCount != null && Math.abs(lastAutoInterval - interval) <= 1 && Math.abs(lastTickCount - tickCount) <= 1 // Always choose the bigger one, otherwise the critical
  99. // point is not the same when zooming in or zooming out.
  100. && lastAutoInterval > interval) {
  101. interval = lastAutoInterval;
  102. } // Only update cache if cache not used, otherwise the
  103. // changing of interval is too insensitive.
  104. else {
  105. cache.lastTickCount = tickCount;
  106. cache.lastAutoInterval = interval;
  107. }
  108. return interval;
  109. }
  110. };
  111. zrUtil.inherits(AngleAxis, Axis);
  112. var _default = AngleAxis;
  113. module.exports = _default;