axisHelper.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 OrdinalScale from '../scale/Ordinal.js';
  42. import IntervalScale from '../scale/Interval.js';
  43. import Scale from '../scale/Scale.js';
  44. import { prepareLayoutBarSeries, makeColumnLayout, retrieveColumnLayout } from '../layout/barGrid.js';
  45. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  46. import TimeScale from '../scale/Time.js';
  47. import LogScale from '../scale/Log.js';
  48. import { getStackedDimension } from '../data/helper/dataStackHelper.js';
  49. import { ensureScaleRawExtentInfo } from './scaleRawExtentInfo.js';
  50. /**
  51. * Get axis scale extent before niced.
  52. * Item of returned array can only be number (including Infinity and NaN).
  53. *
  54. * Caution:
  55. * Precondition of calling this method:
  56. * The scale extent has been initialized using series data extent via
  57. * `scale.setExtent` or `scale.unionExtentFromData`;
  58. */
  59. export function getScaleExtent(scale, model) {
  60. var scaleType = scale.type;
  61. var rawExtentResult = ensureScaleRawExtentInfo(scale, model, scale.getExtent()).calculate();
  62. scale.setBlank(rawExtentResult.isBlank);
  63. var min = rawExtentResult.min;
  64. var max = rawExtentResult.max; // If bars are placed on a base axis of type time or interval account for axis boundary overflow and current axis
  65. // is base axis
  66. // FIXME
  67. // (1) Consider support value axis, where below zero and axis `onZero` should be handled properly.
  68. // (2) Refactor the logic with `barGrid`. Is it not need to `makeBarWidthAndOffsetInfo` twice with different extent?
  69. // Should not depend on series type `bar`?
  70. // (3) Fix that might overlap when using dataZoom.
  71. // (4) Consider other chart types using `barGrid`?
  72. // See #6728, #4862, `test/bar-overflow-time-plot.html`
  73. var ecModel = model.ecModel;
  74. if (ecModel && scaleType === 'time'
  75. /* || scaleType === 'interval' */
  76. ) {
  77. var barSeriesModels = prepareLayoutBarSeries('bar', ecModel);
  78. var isBaseAxisAndHasBarSeries_1 = false;
  79. zrUtil.each(barSeriesModels, function (seriesModel) {
  80. isBaseAxisAndHasBarSeries_1 = isBaseAxisAndHasBarSeries_1 || seriesModel.getBaseAxis() === model.axis;
  81. });
  82. if (isBaseAxisAndHasBarSeries_1) {
  83. // Calculate placement of bars on axis. TODO should be decoupled
  84. // with barLayout
  85. var barWidthAndOffset = makeColumnLayout(barSeriesModels); // Adjust axis min and max to account for overflow
  86. var adjustedScale = adjustScaleForOverflow(min, max, model, barWidthAndOffset);
  87. min = adjustedScale.min;
  88. max = adjustedScale.max;
  89. }
  90. }
  91. return {
  92. extent: [min, max],
  93. // "fix" means "fixed", the value should not be
  94. // changed in the subsequent steps.
  95. fixMin: rawExtentResult.minFixed,
  96. fixMax: rawExtentResult.maxFixed
  97. };
  98. }
  99. function adjustScaleForOverflow(min, max, model, // Only support cartesian coord yet.
  100. barWidthAndOffset) {
  101. // Get Axis Length
  102. var axisExtent = model.axis.getExtent();
  103. var axisLength = axisExtent[1] - axisExtent[0]; // Get bars on current base axis and calculate min and max overflow
  104. var barsOnCurrentAxis = retrieveColumnLayout(barWidthAndOffset, model.axis);
  105. if (barsOnCurrentAxis === undefined) {
  106. return {
  107. min: min,
  108. max: max
  109. };
  110. }
  111. var minOverflow = Infinity;
  112. zrUtil.each(barsOnCurrentAxis, function (item) {
  113. minOverflow = Math.min(item.offset, minOverflow);
  114. });
  115. var maxOverflow = -Infinity;
  116. zrUtil.each(barsOnCurrentAxis, function (item) {
  117. maxOverflow = Math.max(item.offset + item.width, maxOverflow);
  118. });
  119. minOverflow = Math.abs(minOverflow);
  120. maxOverflow = Math.abs(maxOverflow);
  121. var totalOverFlow = minOverflow + maxOverflow; // Calculate required buffer based on old range and overflow
  122. var oldRange = max - min;
  123. var oldRangePercentOfNew = 1 - (minOverflow + maxOverflow) / axisLength;
  124. var overflowBuffer = oldRange / oldRangePercentOfNew - oldRange;
  125. max += overflowBuffer * (maxOverflow / totalOverFlow);
  126. min -= overflowBuffer * (minOverflow / totalOverFlow);
  127. return {
  128. min: min,
  129. max: max
  130. };
  131. } // Precondition of calling this method:
  132. // The scale extent has been initialized using series data extent via
  133. // `scale.setExtent` or `scale.unionExtentFromData`;
  134. export function niceScaleExtent(scale, inModel) {
  135. var model = inModel;
  136. var extentInfo = getScaleExtent(scale, model);
  137. var extent = extentInfo.extent;
  138. var splitNumber = model.get('splitNumber');
  139. if (scale instanceof LogScale) {
  140. scale.base = model.get('logBase');
  141. }
  142. var scaleType = scale.type;
  143. var interval = model.get('interval');
  144. var isIntervalOrTime = scaleType === 'interval' || scaleType === 'time';
  145. scale.setExtent(extent[0], extent[1]);
  146. scale.calcNiceExtent({
  147. splitNumber: splitNumber,
  148. fixMin: extentInfo.fixMin,
  149. fixMax: extentInfo.fixMax,
  150. minInterval: isIntervalOrTime ? model.get('minInterval') : null,
  151. maxInterval: isIntervalOrTime ? model.get('maxInterval') : null
  152. }); // If some one specified the min, max. And the default calculated interval
  153. // is not good enough. He can specify the interval. It is often appeared
  154. // in angle axis with angle 0 - 360. Interval calculated in interval scale is hard
  155. // to be 60.
  156. // FIXME
  157. if (interval != null) {
  158. scale.setInterval && scale.setInterval(interval);
  159. }
  160. }
  161. /**
  162. * @param axisType Default retrieve from model.type
  163. */
  164. export function createScaleByModel(model, axisType) {
  165. axisType = axisType || model.get('type');
  166. if (axisType) {
  167. switch (axisType) {
  168. // Buildin scale
  169. case 'category':
  170. return new OrdinalScale({
  171. ordinalMeta: model.getOrdinalMeta ? model.getOrdinalMeta() : model.getCategories(),
  172. extent: [Infinity, -Infinity]
  173. });
  174. case 'time':
  175. return new TimeScale({
  176. locale: model.ecModel.getLocaleModel(),
  177. useUTC: model.ecModel.get('useUTC')
  178. });
  179. default:
  180. // case 'value'/'interval', 'log', or others.
  181. return new (Scale.getClass(axisType) || IntervalScale)();
  182. }
  183. }
  184. }
  185. /**
  186. * Check if the axis cross 0
  187. */
  188. export function ifAxisCrossZero(axis) {
  189. var dataExtent = axis.scale.getExtent();
  190. var min = dataExtent[0];
  191. var max = dataExtent[1];
  192. return !(min > 0 && max > 0 || min < 0 && max < 0);
  193. }
  194. /**
  195. * @param axis
  196. * @return Label formatter function.
  197. * param: {number} tickValue,
  198. * param: {number} idx, the index in all ticks.
  199. * If category axis, this param is not required.
  200. * return: {string} label string.
  201. */
  202. export function makeLabelFormatter(axis) {
  203. var labelFormatter = axis.getLabelModel().get('formatter');
  204. var categoryTickStart = axis.type === 'category' ? axis.scale.getExtent()[0] : null;
  205. if (axis.scale.type === 'time') {
  206. return function (tpl) {
  207. return function (tick, idx) {
  208. return axis.scale.getFormattedLabel(tick, idx, tpl);
  209. };
  210. }(labelFormatter);
  211. } else if (zrUtil.isString(labelFormatter)) {
  212. return function (tpl) {
  213. return function (tick) {
  214. // For category axis, get raw value; for numeric axis,
  215. // get formatted label like '1,333,444'.
  216. var label = axis.scale.getLabel(tick);
  217. var text = tpl.replace('{value}', label != null ? label : '');
  218. return text;
  219. };
  220. }(labelFormatter);
  221. } else if (zrUtil.isFunction(labelFormatter)) {
  222. return function (cb) {
  223. return function (tick, idx) {
  224. // The original intention of `idx` is "the index of the tick in all ticks".
  225. // But the previous implementation of category axis do not consider the
  226. // `axisLabel.interval`, which cause that, for example, the `interval` is
  227. // `1`, then the ticks "name5", "name7", "name9" are displayed, where the
  228. // corresponding `idx` are `0`, `2`, `4`, but not `0`, `1`, `2`. So we keep
  229. // the definition here for back compatibility.
  230. if (categoryTickStart != null) {
  231. idx = tick.value - categoryTickStart;
  232. }
  233. return cb(getAxisRawValue(axis, tick), idx, tick.level != null ? {
  234. level: tick.level
  235. } : null);
  236. };
  237. }(labelFormatter);
  238. } else {
  239. return function (tick) {
  240. return axis.scale.getLabel(tick);
  241. };
  242. }
  243. }
  244. export function getAxisRawValue(axis, tick) {
  245. // In category axis with data zoom, tick is not the original
  246. // index of axis.data. So tick should not be exposed to user
  247. // in category axis.
  248. return axis.type === 'category' ? axis.scale.getLabel(tick) : tick.value;
  249. }
  250. /**
  251. * @param axis
  252. * @return Be null/undefined if no labels.
  253. */
  254. export function estimateLabelUnionRect(axis) {
  255. var axisModel = axis.model;
  256. var scale = axis.scale;
  257. if (!axisModel.get(['axisLabel', 'show']) || scale.isBlank()) {
  258. return;
  259. }
  260. var realNumberScaleTicks;
  261. var tickCount;
  262. var categoryScaleExtent = scale.getExtent(); // Optimize for large category data, avoid call `getTicks()`.
  263. if (scale instanceof OrdinalScale) {
  264. tickCount = scale.count();
  265. } else {
  266. realNumberScaleTicks = scale.getTicks();
  267. tickCount = realNumberScaleTicks.length;
  268. }
  269. var axisLabelModel = axis.getLabelModel();
  270. var labelFormatter = makeLabelFormatter(axis);
  271. var rect;
  272. var step = 1; // Simple optimization for large amount of labels
  273. if (tickCount > 40) {
  274. step = Math.ceil(tickCount / 40);
  275. }
  276. for (var i = 0; i < tickCount; i += step) {
  277. var tick = realNumberScaleTicks ? realNumberScaleTicks[i] : {
  278. value: categoryScaleExtent[0] + i
  279. };
  280. var label = labelFormatter(tick, i);
  281. var unrotatedSingleRect = axisLabelModel.getTextRect(label);
  282. var singleRect = rotateTextRect(unrotatedSingleRect, axisLabelModel.get('rotate') || 0);
  283. rect ? rect.union(singleRect) : rect = singleRect;
  284. }
  285. return rect;
  286. }
  287. function rotateTextRect(textRect, rotate) {
  288. var rotateRadians = rotate * Math.PI / 180;
  289. var beforeWidth = textRect.width;
  290. var beforeHeight = textRect.height;
  291. var afterWidth = beforeWidth * Math.abs(Math.cos(rotateRadians)) + Math.abs(beforeHeight * Math.sin(rotateRadians));
  292. var afterHeight = beforeWidth * Math.abs(Math.sin(rotateRadians)) + Math.abs(beforeHeight * Math.cos(rotateRadians));
  293. var rotatedRect = new BoundingRect(textRect.x, textRect.y, afterWidth, afterHeight);
  294. return rotatedRect;
  295. }
  296. /**
  297. * @param model axisLabelModel or axisTickModel
  298. * @return {number|String} Can be null|'auto'|number|function
  299. */
  300. export function getOptionCategoryInterval(model) {
  301. var interval = model.get('interval');
  302. return interval == null ? 'auto' : interval;
  303. }
  304. /**
  305. * Set `categoryInterval` as 0 implicitly indicates that
  306. * show all labels regardless of overlap.
  307. * @param {Object} axis axisModel.axis
  308. */
  309. export function shouldShowAllLabels(axis) {
  310. return axis.type === 'category' && getOptionCategoryInterval(axis.getLabelModel()) === 0;
  311. }
  312. export function getDataDimensionsOnAxis(data, axisDim) {
  313. // Remove duplicated dat dimensions caused by `getStackedDimension`.
  314. var dataDimMap = {}; // Currently `mapDimensionsAll` will contain stack result dimension ('__\0ecstackresult').
  315. // PENDING: is it reasonable? Do we need to remove the original dim from "coord dim" since
  316. // there has been stacked result dim?
  317. zrUtil.each(data.mapDimensionsAll(axisDim), function (dataDim) {
  318. // For example, the extent of the original dimension
  319. // is [0.1, 0.5], the extent of the `stackResultDimension`
  320. // is [7, 9], the final extent should NOT include [0.1, 0.5],
  321. // because there is no graphic corresponding to [0.1, 0.5].
  322. // See the case in `test/area-stack.html` `main1`, where area line
  323. // stack needs `yAxis` not start from 0.
  324. dataDimMap[getStackedDimension(data, dataDim)] = true;
  325. });
  326. return zrUtil.keys(dataDimMap);
  327. }
  328. export function unionAxisExtentFromData(dataExtent, data, axisDim) {
  329. if (data) {
  330. zrUtil.each(getDataDimensionsOnAxis(data, axisDim), function (dim) {
  331. var seriesExtent = data.getApproximateExtent(dim);
  332. seriesExtent[0] < dataExtent[0] && (dataExtent[0] = seriesExtent[0]);
  333. seriesExtent[1] > dataExtent[1] && (dataExtent[1] = seriesExtent[1]);
  334. });
  335. }
  336. }