axisHelper.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. var _config = require("../config");
  2. var __DEV__ = _config.__DEV__;
  3. var zrUtil = require("zrender/lib/core/util");
  4. var textContain = require("zrender/lib/contain/text");
  5. var OrdinalScale = require("../scale/Ordinal");
  6. var IntervalScale = require("../scale/Interval");
  7. var Scale = require("../scale/Scale");
  8. var numberUtil = require("../util/number");
  9. require("../scale/Time");
  10. require("../scale/Log");
  11. /**
  12. * Get axis scale extent before niced.
  13. * Item of returned array can only be number (including Infinity and NaN).
  14. */
  15. function getScaleExtent(scale, model) {
  16. var scaleType = scale.type;
  17. var min = model.getMin();
  18. var max = model.getMax();
  19. var fixMin = min != null;
  20. var fixMax = max != null;
  21. var originalExtent = scale.getExtent();
  22. var axisDataLen;
  23. var boundaryGap;
  24. var span;
  25. if (scaleType === 'ordinal') {
  26. axisDataLen = (model.get('data') || []).length;
  27. } else {
  28. boundaryGap = model.get('boundaryGap');
  29. if (!zrUtil.isArray(boundaryGap)) {
  30. boundaryGap = [boundaryGap || 0, boundaryGap || 0];
  31. }
  32. if (typeof boundaryGap[0] === 'boolean') {
  33. boundaryGap = [0, 0];
  34. }
  35. boundaryGap[0] = numberUtil.parsePercent(boundaryGap[0], 1);
  36. boundaryGap[1] = numberUtil.parsePercent(boundaryGap[1], 1);
  37. span = originalExtent[1] - originalExtent[0] || Math.abs(originalExtent[0]);
  38. } // Notice: When min/max is not set (that is, when there are null/undefined,
  39. // which is the most common case), these cases should be ensured:
  40. // (1) For 'ordinal', show all axis.data.
  41. // (2) For others:
  42. // + `boundaryGap` is applied (if min/max set, boundaryGap is
  43. // disabled).
  44. // + If `needCrossZero`, min/max should be zero, otherwise, min/max should
  45. // be the result that originalExtent enlarged by boundaryGap.
  46. // (3) If no data, it should be ensured that `scale.setBlank` is set.
  47. // FIXME
  48. // (1) When min/max is 'dataMin' or 'dataMax', should boundaryGap be able to used?
  49. // (2) When `needCrossZero` and all data is positive/negative, should it be ensured
  50. // that the results processed by boundaryGap are positive/negative?
  51. if (min == null) {
  52. min = scaleType === 'ordinal' ? axisDataLen ? 0 : NaN : originalExtent[0] - boundaryGap[0] * span;
  53. }
  54. if (max == null) {
  55. max = scaleType === 'ordinal' ? axisDataLen ? axisDataLen - 1 : NaN : originalExtent[1] + boundaryGap[1] * span;
  56. }
  57. if (min === 'dataMin') {
  58. min = originalExtent[0];
  59. } else if (typeof min === 'function') {
  60. min = min({
  61. min: originalExtent[0],
  62. max: originalExtent[1]
  63. });
  64. }
  65. if (max === 'dataMax') {
  66. max = originalExtent[1];
  67. } else if (typeof max === 'function') {
  68. max = max({
  69. min: originalExtent[0],
  70. max: originalExtent[1]
  71. });
  72. }
  73. (min == null || !isFinite(min)) && (min = NaN);
  74. (max == null || !isFinite(max)) && (max = NaN);
  75. scale.setBlank(zrUtil.eqNaN(min) || zrUtil.eqNaN(max)); // Evaluate if axis needs cross zero
  76. if (model.getNeedCrossZero()) {
  77. // Axis is over zero and min is not set
  78. if (min > 0 && max > 0 && !fixMin) {
  79. min = 0;
  80. } // Axis is under zero and max is not set
  81. if (min < 0 && max < 0 && !fixMax) {
  82. max = 0;
  83. }
  84. }
  85. return [min, max];
  86. }
  87. function niceScaleExtent(scale, model) {
  88. var extent = getScaleExtent(scale, model);
  89. var fixMin = model.getMin() != null;
  90. var fixMax = model.getMax() != null;
  91. var splitNumber = model.get('splitNumber');
  92. if (scale.type === 'log') {
  93. scale.base = model.get('logBase');
  94. }
  95. var scaleType = scale.type;
  96. scale.setExtent(extent[0], extent[1]);
  97. scale.niceExtent({
  98. splitNumber: splitNumber,
  99. fixMin: fixMin,
  100. fixMax: fixMax,
  101. minInterval: scaleType === 'interval' || scaleType === 'time' ? model.get('minInterval') : null,
  102. maxInterval: scaleType === 'interval' || scaleType === 'time' ? model.get('maxInterval') : null
  103. }); // If some one specified the min, max. And the default calculated interval
  104. // is not good enough. He can specify the interval. It is often appeared
  105. // in angle axis with angle 0 - 360. Interval calculated in interval scale is hard
  106. // to be 60.
  107. // FIXME
  108. var interval = model.get('interval');
  109. if (interval != null) {
  110. scale.setInterval && scale.setInterval(interval);
  111. }
  112. }
  113. /**
  114. * @param {module:echarts/model/Model} model
  115. * @param {string} [axisType] Default retrieve from model.type
  116. * @return {module:echarts/scale/*}
  117. */
  118. function createScaleByModel(model, axisType) {
  119. axisType = axisType || model.get('type');
  120. if (axisType) {
  121. switch (axisType) {
  122. // Buildin scale
  123. case 'category':
  124. return new OrdinalScale(model.getCategories(), [Infinity, -Infinity]);
  125. case 'value':
  126. return new IntervalScale();
  127. // Extended scale, like time and log
  128. default:
  129. return (Scale.getClass(axisType) || IntervalScale).create(model);
  130. }
  131. }
  132. }
  133. /**
  134. * Check if the axis corss 0
  135. */
  136. function ifAxisCrossZero(axis) {
  137. var dataExtent = axis.scale.getExtent();
  138. var min = dataExtent[0];
  139. var max = dataExtent[1];
  140. return !(min > 0 && max > 0 || min < 0 && max < 0);
  141. }
  142. /**
  143. * @param {Array.<number>} tickCoords In axis self coordinate.
  144. * @param {Array.<string>} labels
  145. * @param {string} font
  146. * @param {number} axisRotate 0: towards right horizontally, clock-wise is negative.
  147. * @param {number} [labelRotate=0] 0: towards right horizontally, clock-wise is negative.
  148. * @return {number}
  149. */
  150. function getAxisLabelInterval(tickCoords, labels, font, axisRotate, labelRotate) {
  151. var textSpaceTakenRect;
  152. var autoLabelInterval = 0;
  153. var accumulatedLabelInterval = 0;
  154. var rotation = (axisRotate - labelRotate) / 180 * Math.PI;
  155. var step = 1;
  156. if (labels.length > 40) {
  157. // Simple optimization for large amount of labels
  158. step = Math.floor(labels.length / 40);
  159. }
  160. for (var i = 0; i < tickCoords.length; i += step) {
  161. var tickCoord = tickCoords[i]; // Not precise, do not consider align and vertical align
  162. // and each distance from axis line yet.
  163. var rect = textContain.getBoundingRect(labels[i], font, 'center', 'top');
  164. rect.x += tickCoord * Math.cos(rotation);
  165. rect.y += tickCoord * Math.sin(rotation); // Magic number
  166. rect.width *= 1.3;
  167. rect.height *= 1.3;
  168. if (!textSpaceTakenRect) {
  169. textSpaceTakenRect = rect.clone();
  170. } // There is no space for current label;
  171. else if (textSpaceTakenRect.intersect(rect)) {
  172. accumulatedLabelInterval++;
  173. autoLabelInterval = Math.max(autoLabelInterval, accumulatedLabelInterval);
  174. } else {
  175. textSpaceTakenRect.union(rect); // Reset
  176. accumulatedLabelInterval = 0;
  177. }
  178. }
  179. if (autoLabelInterval === 0 && step > 1) {
  180. return step;
  181. }
  182. return (autoLabelInterval + 1) * step - 1;
  183. }
  184. /**
  185. * @param {Object} axis
  186. * @param {Function} labelFormatter
  187. * @return {Array.<string>}
  188. */
  189. function getFormattedLabels(axis, labelFormatter) {
  190. var scale = axis.scale;
  191. var labels = scale.getTicksLabels();
  192. var ticks = scale.getTicks();
  193. if (typeof labelFormatter === 'string') {
  194. labelFormatter = function (tpl) {
  195. return function (val) {
  196. return tpl.replace('{value}', val != null ? val : '');
  197. };
  198. }(labelFormatter); // Consider empty array
  199. return zrUtil.map(labels, labelFormatter);
  200. } else if (typeof labelFormatter === 'function') {
  201. return zrUtil.map(ticks, function (tick, idx) {
  202. return labelFormatter(getAxisRawValue(axis, tick), idx);
  203. }, this);
  204. } else {
  205. return labels;
  206. }
  207. }
  208. function getAxisRawValue(axis, value) {
  209. // In category axis with data zoom, tick is not the original
  210. // index of axis.data. So tick should not be exposed to user
  211. // in category axis.
  212. return axis.type === 'category' ? axis.scale.getLabel(value) : value;
  213. }
  214. exports.getScaleExtent = getScaleExtent;
  215. exports.niceScaleExtent = niceScaleExtent;
  216. exports.createScaleByModel = createScaleByModel;
  217. exports.ifAxisCrossZero = ifAxisCrossZero;
  218. exports.getAxisLabelInterval = getAxisLabelInterval;
  219. exports.getFormattedLabels = getFormattedLabels;
  220. exports.getAxisRawValue = getAxisRawValue;