axisHelper.js 13 KB

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