axisHelper.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 _config = require("../config");
  20. var __DEV__ = _config.__DEV__;
  21. var zrUtil = require("zrender/lib/core/util");
  22. var OrdinalScale = require("../scale/Ordinal");
  23. var IntervalScale = require("../scale/Interval");
  24. var Scale = require("../scale/Scale");
  25. var numberUtil = require("../util/number");
  26. var _barGrid = require("../layout/barGrid");
  27. var prepareLayoutBarSeries = _barGrid.prepareLayoutBarSeries;
  28. var makeColumnLayout = _barGrid.makeColumnLayout;
  29. var retrieveColumnLayout = _barGrid.retrieveColumnLayout;
  30. var BoundingRect = require("zrender/lib/core/BoundingRect");
  31. require("../scale/Time");
  32. require("../scale/Log");
  33. /*
  34. * Licensed to the Apache Software Foundation (ASF) under one
  35. * or more contributor license agreements. See the NOTICE file
  36. * distributed with this work for additional information
  37. * regarding copyright ownership. The ASF licenses this file
  38. * to you under the Apache License, Version 2.0 (the
  39. * "License"); you may not use this file except in compliance
  40. * with the License. You may obtain a copy of the License at
  41. *
  42. * http://www.apache.org/licenses/LICENSE-2.0
  43. *
  44. * Unless required by applicable law or agreed to in writing,
  45. * software distributed under the License is distributed on an
  46. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  47. * KIND, either express or implied. See the License for the
  48. * specific language governing permissions and limitations
  49. * under the License.
  50. */
  51. /**
  52. * Get axis scale extent before niced.
  53. * Item of returned array can only be number (including Infinity and NaN).
  54. */
  55. function getScaleExtent(scale, model) {
  56. var scaleType = scale.type;
  57. var min = model.getMin();
  58. var max = model.getMax();
  59. var originalExtent = scale.getExtent();
  60. var axisDataLen;
  61. var boundaryGap;
  62. var span;
  63. if (scaleType === 'ordinal') {
  64. axisDataLen = model.getCategories().length;
  65. } else {
  66. boundaryGap = model.get('boundaryGap');
  67. if (!zrUtil.isArray(boundaryGap)) {
  68. boundaryGap = [boundaryGap || 0, boundaryGap || 0];
  69. }
  70. if (typeof boundaryGap[0] === 'boolean') {
  71. boundaryGap = [0, 0];
  72. }
  73. boundaryGap[0] = numberUtil.parsePercent(boundaryGap[0], 1);
  74. boundaryGap[1] = numberUtil.parsePercent(boundaryGap[1], 1);
  75. span = originalExtent[1] - originalExtent[0] || Math.abs(originalExtent[0]);
  76. } // Notice: When min/max is not set (that is, when there are null/undefined,
  77. // which is the most common case), these cases should be ensured:
  78. // (1) For 'ordinal', show all axis.data.
  79. // (2) For others:
  80. // + `boundaryGap` is applied (if min/max set, boundaryGap is
  81. // disabled).
  82. // + If `needCrossZero`, min/max should be zero, otherwise, min/max should
  83. // be the result that originalExtent enlarged by boundaryGap.
  84. // (3) If no data, it should be ensured that `scale.setBlank` is set.
  85. // FIXME
  86. // (1) When min/max is 'dataMin' or 'dataMax', should boundaryGap be able to used?
  87. // (2) When `needCrossZero` and all data is positive/negative, should it be ensured
  88. // that the results processed by boundaryGap are positive/negative?
  89. if (min === 'dataMin') {
  90. min = originalExtent[0];
  91. } else if (typeof min === 'function') {
  92. min = min({
  93. min: originalExtent[0],
  94. max: originalExtent[1]
  95. });
  96. }
  97. if (max === 'dataMax') {
  98. max = originalExtent[1];
  99. } else if (typeof max === 'function') {
  100. max = max({
  101. min: originalExtent[0],
  102. max: originalExtent[1]
  103. });
  104. }
  105. var fixMin = min != null;
  106. var fixMax = max != null;
  107. if (min == null) {
  108. min = scaleType === 'ordinal' ? axisDataLen ? 0 : NaN : originalExtent[0] - boundaryGap[0] * span;
  109. }
  110. if (max == null) {
  111. max = scaleType === 'ordinal' ? axisDataLen ? axisDataLen - 1 : NaN : originalExtent[1] + boundaryGap[1] * span;
  112. }
  113. (min == null || !isFinite(min)) && (min = NaN);
  114. (max == null || !isFinite(max)) && (max = NaN);
  115. scale.setBlank(zrUtil.eqNaN(min) || zrUtil.eqNaN(max) || scaleType === 'ordinal' && !scale.getOrdinalMeta().categories.length); // Evaluate if axis needs cross zero
  116. if (model.getNeedCrossZero()) {
  117. // Axis is over zero and min is not set
  118. if (min > 0 && max > 0 && !fixMin) {
  119. min = 0;
  120. } // Axis is under zero and max is not set
  121. if (min < 0 && max < 0 && !fixMax) {
  122. max = 0;
  123. }
  124. } // If bars are placed on a base axis of type time or interval account for axis boundary overflow and current axis
  125. // is base axis
  126. // FIXME
  127. // (1) Consider support value axis, where below zero and axis `onZero` should be handled properly.
  128. // (2) Refactor the logic with `barGrid`. Is it not need to `makeBarWidthAndOffsetInfo` twice with different extent?
  129. // Should not depend on series type `bar`?
  130. // (3) Fix that might overlap when using dataZoom.
  131. // (4) Consider other chart types using `barGrid`?
  132. // See #6728, #4862, `test/bar-overflow-time-plot.html`
  133. var ecModel = model.ecModel;
  134. if (ecModel && scaleType === 'time'
  135. /*|| scaleType === 'interval' */
  136. ) {
  137. var barSeriesModels = prepareLayoutBarSeries('bar', ecModel);
  138. var isBaseAxisAndHasBarSeries;
  139. zrUtil.each(barSeriesModels, function (seriesModel) {
  140. isBaseAxisAndHasBarSeries |= seriesModel.getBaseAxis() === model.axis;
  141. });
  142. if (isBaseAxisAndHasBarSeries) {
  143. // Calculate placement of bars on axis
  144. var barWidthAndOffset = makeColumnLayout(barSeriesModels); // Adjust axis min and max to account for overflow
  145. var adjustedScale = adjustScaleForOverflow(min, max, model, barWidthAndOffset);
  146. min = adjustedScale.min;
  147. max = adjustedScale.max;
  148. }
  149. }
  150. return {
  151. extent: [min, max],
  152. // "fix" means "fixed", the value should not be
  153. // changed in the subsequent steps.
  154. fixMin: fixMin,
  155. fixMax: fixMax
  156. };
  157. }
  158. function adjustScaleForOverflow(min, max, model, barWidthAndOffset) {
  159. // Get Axis Length
  160. var axisExtent = model.axis.getExtent();
  161. var axisLength = axisExtent[1] - axisExtent[0]; // Get bars on current base axis and calculate min and max overflow
  162. var barsOnCurrentAxis = retrieveColumnLayout(barWidthAndOffset, model.axis);
  163. if (barsOnCurrentAxis === undefined) {
  164. return {
  165. min: min,
  166. max: max
  167. };
  168. }
  169. var minOverflow = Infinity;
  170. zrUtil.each(barsOnCurrentAxis, function (item) {
  171. minOverflow = Math.min(item.offset, minOverflow);
  172. });
  173. var maxOverflow = -Infinity;
  174. zrUtil.each(barsOnCurrentAxis, function (item) {
  175. maxOverflow = Math.max(item.offset + item.width, maxOverflow);
  176. });
  177. minOverflow = Math.abs(minOverflow);
  178. maxOverflow = Math.abs(maxOverflow);
  179. var totalOverFlow = minOverflow + maxOverflow; // Calulate required buffer based on old range and overflow
  180. var oldRange = max - min;
  181. var oldRangePercentOfNew = 1 - (minOverflow + maxOverflow) / axisLength;
  182. var overflowBuffer = oldRange / oldRangePercentOfNew - oldRange;
  183. max += overflowBuffer * (maxOverflow / totalOverFlow);
  184. min -= overflowBuffer * (minOverflow / totalOverFlow);
  185. return {
  186. min: min,
  187. max: max
  188. };
  189. }
  190. function niceScaleExtent(scale, model) {
  191. var extentInfo = getScaleExtent(scale, model);
  192. var extent = extentInfo.extent;
  193. var splitNumber = model.get('splitNumber');
  194. if (scale.type === 'log') {
  195. scale.base = model.get('logBase');
  196. }
  197. var scaleType = scale.type;
  198. scale.setExtent(extent[0], extent[1]);
  199. scale.niceExtent({
  200. splitNumber: splitNumber,
  201. fixMin: extentInfo.fixMin,
  202. fixMax: extentInfo.fixMax,
  203. minInterval: scaleType === 'interval' || scaleType === 'time' ? model.get('minInterval') : null,
  204. maxInterval: scaleType === 'interval' || scaleType === 'time' ? model.get('maxInterval') : null
  205. }); // If some one specified the min, max. And the default calculated interval
  206. // is not good enough. He can specify the interval. It is often appeared
  207. // in angle axis with angle 0 - 360. Interval calculated in interval scale is hard
  208. // to be 60.
  209. // FIXME
  210. var interval = model.get('interval');
  211. if (interval != null) {
  212. scale.setInterval && scale.setInterval(interval);
  213. }
  214. }
  215. /**
  216. * @param {module:echarts/model/Model} model
  217. * @param {string} [axisType] Default retrieve from model.type
  218. * @return {module:echarts/scale/*}
  219. */
  220. function createScaleByModel(model, axisType) {
  221. axisType = axisType || model.get('type');
  222. if (axisType) {
  223. switch (axisType) {
  224. // Buildin scale
  225. case 'category':
  226. return new OrdinalScale(model.getOrdinalMeta ? model.getOrdinalMeta() : model.getCategories(), [Infinity, -Infinity]);
  227. case 'value':
  228. return new IntervalScale();
  229. // Extended scale, like time and log
  230. default:
  231. return (Scale.getClass(axisType) || IntervalScale).create(model);
  232. }
  233. }
  234. }
  235. /**
  236. * Check if the axis corss 0
  237. */
  238. function ifAxisCrossZero(axis) {
  239. var dataExtent = axis.scale.getExtent();
  240. var min = dataExtent[0];
  241. var max = dataExtent[1];
  242. return !(min > 0 && max > 0 || min < 0 && max < 0);
  243. }
  244. /**
  245. * @param {module:echarts/coord/Axis} axis
  246. * @return {Function} Label formatter function.
  247. * param: {number} tickValue,
  248. * param: {number} idx, the index in all ticks.
  249. * If category axis, this param is not requied.
  250. * return: {string} label string.
  251. */
  252. function makeLabelFormatter(axis) {
  253. var labelFormatter = axis.getLabelModel().get('formatter');
  254. var categoryTickStart = axis.type === 'category' ? axis.scale.getExtent()[0] : null;
  255. if (typeof labelFormatter === 'string') {
  256. labelFormatter = function (tpl) {
  257. return function (val) {
  258. // For category axis, get raw value; for numeric axis,
  259. // get foramtted label like '1,333,444'.
  260. val = axis.scale.getLabel(val);
  261. return tpl.replace('{value}', val != null ? val : '');
  262. };
  263. }(labelFormatter); // Consider empty array
  264. return labelFormatter;
  265. } else if (typeof labelFormatter === 'function') {
  266. return function (tickValue, idx) {
  267. // The original intention of `idx` is "the index of the tick in all ticks".
  268. // But the previous implementation of category axis do not consider the
  269. // `axisLabel.interval`, which cause that, for example, the `interval` is
  270. // `1`, then the ticks "name5", "name7", "name9" are displayed, where the
  271. // corresponding `idx` are `0`, `2`, `4`, but not `0`, `1`, `2`. So we keep
  272. // the definition here for back compatibility.
  273. if (categoryTickStart != null) {
  274. idx = tickValue - categoryTickStart;
  275. }
  276. return labelFormatter(getAxisRawValue(axis, tickValue), idx);
  277. };
  278. } else {
  279. return function (tick) {
  280. return axis.scale.getLabel(tick);
  281. };
  282. }
  283. }
  284. function getAxisRawValue(axis, value) {
  285. // In category axis with data zoom, tick is not the original
  286. // index of axis.data. So tick should not be exposed to user
  287. // in category axis.
  288. return axis.type === 'category' ? axis.scale.getLabel(value) : value;
  289. }
  290. /**
  291. * @param {module:echarts/coord/Axis} axis
  292. * @return {module:zrender/core/BoundingRect} Be null/undefined if no labels.
  293. */
  294. function estimateLabelUnionRect(axis) {
  295. var axisModel = axis.model;
  296. var scale = axis.scale;
  297. if (!axisModel.get('axisLabel.show') || scale.isBlank()) {
  298. return;
  299. }
  300. var isCategory = axis.type === 'category';
  301. var realNumberScaleTicks;
  302. var tickCount;
  303. var categoryScaleExtent = scale.getExtent(); // Optimize for large category data, avoid call `getTicks()`.
  304. if (isCategory) {
  305. tickCount = scale.count();
  306. } else {
  307. realNumberScaleTicks = scale.getTicks();
  308. tickCount = realNumberScaleTicks.length;
  309. }
  310. var axisLabelModel = axis.getLabelModel();
  311. var labelFormatter = makeLabelFormatter(axis);
  312. var rect;
  313. var step = 1; // Simple optimization for large amount of labels
  314. if (tickCount > 40) {
  315. step = Math.ceil(tickCount / 40);
  316. }
  317. for (var i = 0; i < tickCount; i += step) {
  318. var tickValue = realNumberScaleTicks ? realNumberScaleTicks[i] : categoryScaleExtent[0] + i;
  319. var label = labelFormatter(tickValue);
  320. var unrotatedSingleRect = axisLabelModel.getTextRect(label);
  321. var singleRect = rotateTextRect(unrotatedSingleRect, axisLabelModel.get('rotate') || 0);
  322. rect ? rect.union(singleRect) : rect = singleRect;
  323. }
  324. return rect;
  325. }
  326. function rotateTextRect(textRect, rotate) {
  327. var rotateRadians = rotate * Math.PI / 180;
  328. var boundingBox = textRect.plain();
  329. var beforeWidth = boundingBox.width;
  330. var beforeHeight = boundingBox.height;
  331. var afterWidth = beforeWidth * Math.abs(Math.cos(rotateRadians)) + Math.abs(beforeHeight * Math.sin(rotateRadians));
  332. var afterHeight = beforeWidth * Math.abs(Math.sin(rotateRadians)) + Math.abs(beforeHeight * Math.cos(rotateRadians));
  333. var rotatedRect = new BoundingRect(boundingBox.x, boundingBox.y, afterWidth, afterHeight);
  334. return rotatedRect;
  335. }
  336. /**
  337. * @param {module:echarts/src/model/Model} model axisLabelModel or axisTickModel
  338. * @return {number|String} Can be null|'auto'|number|function
  339. */
  340. function getOptionCategoryInterval(model) {
  341. var interval = model.get('interval');
  342. return interval == null ? 'auto' : interval;
  343. }
  344. /**
  345. * Set `categoryInterval` as 0 implicitly indicates that
  346. * show all labels reguardless of overlap.
  347. * @param {Object} axis axisModel.axis
  348. * @return {boolean}
  349. */
  350. function shouldShowAllLabels(axis) {
  351. return axis.type === 'category' && getOptionCategoryInterval(axis.getLabelModel()) === 0;
  352. }
  353. exports.getScaleExtent = getScaleExtent;
  354. exports.niceScaleExtent = niceScaleExtent;
  355. exports.createScaleByModel = createScaleByModel;
  356. exports.ifAxisCrossZero = ifAxisCrossZero;
  357. exports.makeLabelFormatter = makeLabelFormatter;
  358. exports.getAxisRawValue = getAxisRawValue;
  359. exports.estimateLabelUnionRect = estimateLabelUnionRect;
  360. exports.getOptionCategoryInterval = getOptionCategoryInterval;
  361. exports.shouldShowAllLabels = shouldShowAllLabels;