Axis.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 { each, map } from 'zrender/lib/core/util.js';
  41. import { linearMap } from '../util/number.js';
  42. import { createAxisTicks, createAxisLabels, calculateCategoryInterval, AxisTickLabelComputingKind, createAxisLabelsComputingContext } from './axisTickLabelBuilder.js';
  43. import { isOrdinalScale } from '../scale/helper.js';
  44. import { calcBandWidth } from './axisBand.js';
  45. import { getTickValueOutermost } from './axisHelper.js';
  46. var NORMALIZED_EXTENT = [0, 1];
  47. /**
  48. * Base class of Axis.
  49. *
  50. * Lifetime: recreate for each main process.
  51. * [NOTICE]: Some caches is stored on the axis instance (e.g., `axisTickLabelBuilder.ts`, `scaleRawExtentInfo.ts`),
  52. * which is based on this lifetime.
  53. */
  54. var Axis = /** @class */function () {
  55. function Axis(dim, scale, extent) {
  56. // NOTICE: Must ensure `true` is only available on 'category' axis.
  57. this.onBand = false;
  58. // Make sure that `extent[0] > extent[1]` only if `inverse: true`.
  59. // `inverse` can be inferred by `extent` unless `extent[0] === extent[1]`.
  60. this.inverse = false;
  61. this.dim = dim;
  62. this.scale = scale;
  63. this._extent = extent || [0, 0];
  64. }
  65. /**
  66. * If axis extent contain given coord
  67. */
  68. Axis.prototype.contain = function (coord) {
  69. var extent = this._extent;
  70. var min = Math.min(extent[0], extent[1]);
  71. var max = Math.max(extent[0], extent[1]);
  72. return coord >= min && coord <= max;
  73. };
  74. /**
  75. * If axis extent contain given data
  76. */
  77. Axis.prototype.containData = function (data) {
  78. return this.scale.contain(this.scale.parse(data));
  79. };
  80. /**
  81. * Get coord extent.
  82. */
  83. Axis.prototype.getExtent = function () {
  84. return this._extent.slice();
  85. };
  86. /**
  87. * Set coord extent
  88. */
  89. Axis.prototype.setExtent = function (start, end) {
  90. var extent = this._extent;
  91. extent[0] = start;
  92. extent[1] = end;
  93. };
  94. /**
  95. * Convert data to coord. Data is the rank if it has an ordinal scale
  96. */
  97. Axis.prototype.dataToCoord = function (data, clamp) {
  98. var scale = this.scale;
  99. data = scale.normalize(scale.parse(data));
  100. return linearMap(data, NORMALIZED_EXTENT, makeExtentWithBands(this), clamp);
  101. };
  102. /**
  103. * Convert coord to data. Data is the rank if it has an ordinal scale
  104. */
  105. Axis.prototype.coordToData = function (coord, clamp) {
  106. var t = linearMap(coord, makeExtentWithBands(this), NORMALIZED_EXTENT, clamp);
  107. return this.scale.scale(t);
  108. };
  109. /**
  110. * Convert pixel point to data in axis
  111. */
  112. Axis.prototype.pointToData = function (point, clamp) {
  113. // Should be implemented in derived class if necessary.
  114. return;
  115. };
  116. /**
  117. * Different from `zrUtil.map(axis.getTicks(), axis.dataToCoord, axis)`,
  118. * `axis.getTicksCoords` considers `onBand`, which is used by
  119. * `boundaryGap:true` of category axis and splitLine and splitArea.
  120. * @param opt.tickModel default: axis.model.getModel('axisTick')
  121. */
  122. Axis.prototype.getTicksCoords = function (opt) {
  123. opt = opt || {};
  124. var tickModel = opt.tickModel || this.getTickModel();
  125. var result = createAxisTicks(this, tickModel, {
  126. breakTicks: opt.breakTicks,
  127. pruneByBreak: opt.pruneByBreak
  128. });
  129. var preTicksCoords = map(result.ticks, function (tick) {
  130. return {
  131. coord: this.dataToCoord(getTickValueOutermost(this.scale, tick)),
  132. tick: tick
  133. };
  134. }, this);
  135. var alignWithLabel = tickModel.get('alignWithLabel');
  136. var onBandModified = fixOnBandTicksCoords(this, preTicksCoords, alignWithLabel);
  137. return map(preTicksCoords, function (item) {
  138. return {
  139. coord: item.coord,
  140. tickValue: item.tick.value,
  141. onBand: onBandModified
  142. };
  143. });
  144. };
  145. Axis.prototype.getMinorTicksCoords = function () {
  146. if (isOrdinalScale(this.scale)) {
  147. // Category axis doesn't support minor ticks
  148. return [];
  149. }
  150. var minorTickModel = this.model.getModel('minorTick');
  151. var splitNumber = minorTickModel.get('splitNumber');
  152. // Protection.
  153. if (!(splitNumber > 0 && splitNumber < 100)) {
  154. splitNumber = 5;
  155. }
  156. var minorTicks = this.scale.getMinorTicks(splitNumber);
  157. var minorTicksCoords = map(minorTicks, function (minorTicksGroup) {
  158. return map(minorTicksGroup, function (minorTick) {
  159. return {
  160. coord: this.dataToCoord(minorTick),
  161. tickValue: minorTick
  162. };
  163. }, this);
  164. }, this);
  165. return minorTicksCoords;
  166. };
  167. Axis.prototype.getViewLabels = function (ctx) {
  168. ctx = ctx || createAxisLabelsComputingContext(AxisTickLabelComputingKind.determine);
  169. return createAxisLabels(this, ctx).labels;
  170. };
  171. Axis.prototype.getLabelModel = function () {
  172. return this.model.getModel('axisLabel');
  173. };
  174. /**
  175. * Notice here we only get the default tick model. For splitLine
  176. * or splitArea, we should pass the splitLineModel or splitAreaModel
  177. * manually when calling `getTicksCoords`.
  178. * In GL, this method may be overridden to:
  179. * `axisModel.getModel('axisTick', grid3DModel.getModel('axisTick'));`
  180. */
  181. Axis.prototype.getTickModel = function () {
  182. return this.model.getModel('axisTick');
  183. };
  184. /**
  185. * @deprecated Use `calcBandWidth` instead.
  186. */
  187. Axis.prototype.getBandWidth = function () {
  188. return calcBandWidth(this, {
  189. min: 1
  190. }).w;
  191. // NOTICE: Do not add logic here. Implement everthing in `calcBandWidth`.
  192. };
  193. /**
  194. * Only be called in category axis.
  195. * Can be overridden, consider other axes like in 3D.
  196. * @return Auto interval for category axis tick and label
  197. */
  198. Axis.prototype.calculateCategoryInterval = function (ctx) {
  199. ctx = ctx || createAxisLabelsComputingContext(AxisTickLabelComputingKind.determine);
  200. return calculateCategoryInterval(this, ctx);
  201. };
  202. return Axis;
  203. }();
  204. function makeExtentWithBands(axis) {
  205. var extent = axis.getExtent();
  206. if (axis.onBand) {
  207. var size = extent[1] - extent[0];
  208. var margin = size / axis.scale.count() / 2;
  209. extent[0] += margin;
  210. extent[1] -= margin;
  211. }
  212. return extent;
  213. }
  214. /**
  215. * `axis.onBand: true` (i.e., `boundaryGap: true` in ec option) and `CategoryTickLabelSplitIntervalOption`
  216. * affects `axisTick`/`axisLabel`/`splitLine`/`splitArea`.
  217. *
  218. * Currently, the visual result is best only when `axisTick/splitLine/splitArea.interval === 0`.
  219. * The typical case is:
  220. * |---|---|---| <= This is the input `preTicksCoords`
  221. * 0 1 2 3 (having been added half band width by `makeExtentWithBands`).
  222. * |---|---|---|---| <= This is the result.
  223. * 0 1 2 3
  224. *
  225. * When `interval > 0`, the visual result may be odd for `axisLabel` and `customValues`, but acceptable
  226. * for `axisTick` `splitLine` and `splitArea`:
  227. * |---~---|---~---~---|---| <= This is the input `preTicksCoords`; `interval: 2; min: 1; max: 7`.
  228. * ₁ ₂ 3 ₄ ₅ 6 ₇ Subscript numbers (`₀`, `₁`, `₃`) indicate axis labels are hidden
  229. * (by default settings) due to off-interval.
  230. * A tilde (`~`) indicates a tick ignored due to off-interval.
  231. * |---~---|---~---~---|---~---| <= This is the result.
  232. * ₁ ₂ 3 ₄ ₅ 6 ₇
  233. *
  234. * NOTE:
  235. * - A inappropriate result may cause misleading (e.g., split 2 bars of a single data item when there
  236. * are two bar series).
  237. * - See also #11176 #11186 .
  238. * PENDING:
  239. * - The show/hide of `axisLabel` may be optimized when `interval > 1 and be an even number`,
  240. * but that may introduce complex and still not perfect in odd number, and may not necessary if
  241. * `axisTick: {show: false}` and `axisLabel` can auto hidden when overlapping.
  242. */
  243. function fixOnBandTicksCoords(axis, preTicksCoords, alignWithLabel) {
  244. var ticksLen = preTicksCoords.length;
  245. if (!axis.onBand || alignWithLabel || !ticksLen) {
  246. return false;
  247. }
  248. // Assume:
  249. // - If `onBand: true`, `bandWidth` has been calculated by `ticksLen + 1` rather than `ticksLen`.
  250. // - If `interval > 0`, some ticks may be ignored, but `ticksCoords` has always included boundary
  251. // ticks of axis extent, and be `offInterval: true` if off-interval.
  252. // - No need to consider breaks, since axis break is not supported in category axis.
  253. var bandWidth = calcBandWidth(axis).w;
  254. if (!bandWidth) {
  255. return false;
  256. }
  257. each(preTicksCoords, function (ticksItem) {
  258. ticksItem.coord -= bandWidth / 2;
  259. });
  260. var dataExtent = axis.scale.getExtent();
  261. var oldLast = preTicksCoords[ticksLen - 1];
  262. if (oldLast.tick.offInterval) {
  263. preTicksCoords.pop();
  264. }
  265. preTicksCoords.push({
  266. coord: oldLast.coord + bandWidth,
  267. tick: {
  268. value: dataExtent[1] + 1
  269. }
  270. });
  271. return true;
  272. }
  273. export default Axis;