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';
  41. import OrdinalScale from '../scale/Ordinal';
  42. import IntervalScale from '../scale/Interval';
  43. import Scale from '../scale/Scale';
  44. import { prepareLayoutBarSeries, makeColumnLayout, retrieveColumnLayout } from '../layout/barGrid';
  45. import BoundingRect from 'zrender/lib/core/BoundingRect';
  46. import TimeScale from '../scale/Time';
  47. import LogScale from '../scale/Log';
  48. import { getStackedDimension } from '../data/helper/dataStackHelper';
  49. import { ensureScaleRawExtentInfo } from './scaleRawExtentInfo';
  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 initailized 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. scale.setExtent(extent[0], extent[1]);
  144. scale.niceExtent({
  145. splitNumber: splitNumber,
  146. fixMin: extentInfo.fixMin,
  147. fixMax: extentInfo.fixMax,
  148. minInterval: scaleType === 'interval' || scaleType === 'time' ? model.get('minInterval') : null,
  149. maxInterval: scaleType === 'interval' || scaleType === 'time' ? model.get('maxInterval') : null
  150. }); // If some one specified the min, max. And the default calculated interval
  151. // is not good enough. He can specify the interval. It is often appeared
  152. // in angle axis with angle 0 - 360. Interval calculated in interval scale is hard
  153. // to be 60.
  154. // FIXME
  155. var interval = model.get('interval');
  156. if (interval != null) {
  157. scale.setInterval && scale.setInterval(interval);
  158. }
  159. }
  160. /**
  161. * @param axisType Default retrieve from model.type
  162. */
  163. export function createScaleByModel(model, axisType) {
  164. axisType = axisType || model.get('type');
  165. if (axisType) {
  166. switch (axisType) {
  167. // Buildin scale
  168. case 'category':
  169. return new OrdinalScale({
  170. ordinalMeta: model.getOrdinalMeta ? model.getOrdinalMeta() : model.getCategories(),
  171. extent: [Infinity, -Infinity]
  172. });
  173. case 'time':
  174. return new TimeScale({
  175. locale: model.ecModel.getLocaleModel(),
  176. useUTC: model.ecModel.get('useUTC')
  177. });
  178. default:
  179. // case 'value'/'interval', 'log', or others.
  180. return new (Scale.getClass(axisType) || IntervalScale)();
  181. }
  182. }
  183. }
  184. /**
  185. * Check if the axis cross 0
  186. */
  187. export function ifAxisCrossZero(axis) {
  188. var dataExtent = axis.scale.getExtent();
  189. var min = dataExtent[0];
  190. var max = dataExtent[1];
  191. return !(min > 0 && max > 0 || min < 0 && max < 0);
  192. }
  193. /**
  194. * @param axis
  195. * @return Label formatter function.
  196. * param: {number} tickValue,
  197. * param: {number} idx, the index in all ticks.
  198. * If category axis, this param is not required.
  199. * return: {string} label string.
  200. */
  201. export function makeLabelFormatter(axis) {
  202. var labelFormatter = axis.getLabelModel().get('formatter');
  203. var categoryTickStart = axis.type === 'category' ? axis.scale.getExtent()[0] : null;
  204. if (axis.scale.type === 'time') {
  205. return function (tpl) {
  206. return function (tick, idx) {
  207. return axis.scale.getFormattedLabel(tick, idx, tpl);
  208. };
  209. }(labelFormatter);
  210. } else if (typeof labelFormatter === 'string') {
  211. return function (tpl) {
  212. return function (tick) {
  213. // For category axis, get raw value; for numeric axis,
  214. // get formatted label like '1,333,444'.
  215. var label = axis.scale.getLabel(tick);
  216. var text = tpl.replace('{value}', label != null ? label : '');
  217. return text;
  218. };
  219. }(labelFormatter);
  220. } else if (typeof labelFormatter === 'function') {
  221. return function (cb) {
  222. return function (tick, idx) {
  223. // The original intention of `idx` is "the index of the tick in all ticks".
  224. // But the previous implementation of category axis do not consider the
  225. // `axisLabel.interval`, which cause that, for example, the `interval` is
  226. // `1`, then the ticks "name5", "name7", "name9" are displayed, where the
  227. // corresponding `idx` are `0`, `2`, `4`, but not `0`, `1`, `2`. So we keep
  228. // the definition here for back compatibility.
  229. if (categoryTickStart != null) {
  230. idx = tick.value - categoryTickStart;
  231. }
  232. return cb(getAxisRawValue(axis, tick), idx, tick.level != null ? {
  233. level: tick.level
  234. } : null);
  235. };
  236. }(labelFormatter);
  237. } else {
  238. return function (tick) {
  239. return axis.scale.getLabel(tick);
  240. };
  241. }
  242. }
  243. export function getAxisRawValue(axis, tick) {
  244. // In category axis with data zoom, tick is not the original
  245. // index of axis.data. So tick should not be exposed to user
  246. // in category axis.
  247. return axis.type === 'category' ? axis.scale.getLabel(tick) : tick.value;
  248. }
  249. /**
  250. * @param axis
  251. * @return Be null/undefined if no labels.
  252. */
  253. export function estimateLabelUnionRect(axis) {
  254. var axisModel = axis.model;
  255. var scale = axis.scale;
  256. if (!axisModel.get(['axisLabel', 'show']) || scale.isBlank()) {
  257. return;
  258. }
  259. var realNumberScaleTicks;
  260. var tickCount;
  261. var categoryScaleExtent = scale.getExtent(); // Optimize for large category data, avoid call `getTicks()`.
  262. if (scale instanceof OrdinalScale) {
  263. tickCount = scale.count();
  264. } else {
  265. realNumberScaleTicks = scale.getTicks();
  266. tickCount = realNumberScaleTicks.length;
  267. }
  268. var axisLabelModel = axis.getLabelModel();
  269. var labelFormatter = makeLabelFormatter(axis);
  270. var rect;
  271. var step = 1; // Simple optimization for large amount of labels
  272. if (tickCount > 40) {
  273. step = Math.ceil(tickCount / 40);
  274. }
  275. for (var i = 0; i < tickCount; i += step) {
  276. var tick = realNumberScaleTicks ? realNumberScaleTicks[i] : {
  277. value: categoryScaleExtent[0] + i
  278. };
  279. var label = labelFormatter(tick, i);
  280. var unrotatedSingleRect = axisLabelModel.getTextRect(label);
  281. var singleRect = rotateTextRect(unrotatedSingleRect, axisLabelModel.get('rotate') || 0);
  282. rect ? rect.union(singleRect) : rect = singleRect;
  283. }
  284. return rect;
  285. }
  286. function rotateTextRect(textRect, rotate) {
  287. var rotateRadians = rotate * Math.PI / 180;
  288. var beforeWidth = textRect.width;
  289. var beforeHeight = textRect.height;
  290. var afterWidth = beforeWidth * Math.abs(Math.cos(rotateRadians)) + Math.abs(beforeHeight * Math.sin(rotateRadians));
  291. var afterHeight = beforeWidth * Math.abs(Math.sin(rotateRadians)) + Math.abs(beforeHeight * Math.cos(rotateRadians));
  292. var rotatedRect = new BoundingRect(textRect.x, textRect.y, afterWidth, afterHeight);
  293. return rotatedRect;
  294. }
  295. /**
  296. * @param model axisLabelModel or axisTickModel
  297. * @return {number|String} Can be null|'auto'|number|function
  298. */
  299. export function getOptionCategoryInterval(model) {
  300. var interval = model.get('interval');
  301. return interval == null ? 'auto' : interval;
  302. }
  303. /**
  304. * Set `categoryInterval` as 0 implicitly indicates that
  305. * show all labels reguardless of overlap.
  306. * @param {Object} axis axisModel.axis
  307. */
  308. export function shouldShowAllLabels(axis) {
  309. return axis.type === 'category' && getOptionCategoryInterval(axis.getLabelModel()) === 0;
  310. }
  311. export function getDataDimensionsOnAxis(data, axisDim) {
  312. // Remove duplicated dat dimensions caused by `getStackedDimension`.
  313. var dataDimMap = {}; // Currently `mapDimensionsAll` will contain stack result dimension ('__\0ecstackresult').
  314. // PENDING: is it reasonable? Do we need to remove the original dim from "coord dim" since
  315. // there has been stacked result dim?
  316. zrUtil.each(data.mapDimensionsAll(axisDim), function (dataDim) {
  317. // For example, the extent of the original dimension
  318. // is [0.1, 0.5], the extent of the `stackResultDimension`
  319. // is [7, 9], the final extent should NOT include [0.1, 0.5],
  320. // because there is no graphic corresponding to [0.1, 0.5].
  321. // See the case in `test/area-stack.html` `main1`, where area line
  322. // stack needs `yAxis` not start from 0.
  323. dataDimMap[getStackedDimension(data, dataDim)] = true;
  324. });
  325. return zrUtil.keys(dataDimMap);
  326. }
  327. export function unionAxisExtentFromData(dataExtent, data, axisDim) {
  328. if (data) {
  329. zrUtil.each(getDataDimensionsOnAxis(data, axisDim), function (dim) {
  330. var seriesExtent = data.getApproximateExtent(dim);
  331. seriesExtent[0] < dataExtent[0] && (dataExtent[0] = seriesExtent[0]);
  332. seriesExtent[1] > dataExtent[1] && (dataExtent[1] = seriesExtent[1]);
  333. });
  334. }
  335. }