Ordinal.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 { __extends } from "tslib";
  41. /**
  42. * Linear continuous scale
  43. * http://en.wikipedia.org/wiki/Level_of_measurement
  44. */
  45. // FIXME only one data
  46. import Scale from './Scale.js';
  47. import OrdinalMeta from '../data/OrdinalMeta.js';
  48. import * as scaleHelper from './helper.js';
  49. import { isArray, map, isObject, isString } from 'zrender/lib/core/util.js';
  50. var OrdinalScale = /** @class */function (_super) {
  51. __extends(OrdinalScale, _super);
  52. function OrdinalScale(setting) {
  53. var _this = _super.call(this, setting) || this;
  54. _this.type = 'ordinal';
  55. var ordinalMeta = _this.getSetting('ordinalMeta');
  56. // Caution: Should not use instanceof, consider ec-extensions using
  57. // import approach to get OrdinalMeta class.
  58. if (!ordinalMeta) {
  59. ordinalMeta = new OrdinalMeta({});
  60. }
  61. if (isArray(ordinalMeta)) {
  62. ordinalMeta = new OrdinalMeta({
  63. categories: map(ordinalMeta, function (item) {
  64. return isObject(item) ? item.value : item;
  65. })
  66. });
  67. }
  68. _this._ordinalMeta = ordinalMeta;
  69. _this._extent = _this.getSetting('extent') || [0, ordinalMeta.categories.length - 1];
  70. return _this;
  71. }
  72. OrdinalScale.prototype.parse = function (val) {
  73. // Caution: Math.round(null) will return `0` rather than `NaN`
  74. if (val == null) {
  75. return NaN;
  76. }
  77. return isString(val) ? this._ordinalMeta.getOrdinal(val)
  78. // val might be float.
  79. : Math.round(val);
  80. };
  81. OrdinalScale.prototype.contain = function (rank) {
  82. rank = this.parse(rank);
  83. return scaleHelper.contain(rank, this._extent) && this._ordinalMeta.categories[rank] != null;
  84. };
  85. /**
  86. * Normalize given rank or name to linear [0, 1]
  87. * @param val raw ordinal number.
  88. * @return normalized value in [0, 1].
  89. */
  90. OrdinalScale.prototype.normalize = function (val) {
  91. val = this._getTickNumber(this.parse(val));
  92. return scaleHelper.normalize(val, this._extent);
  93. };
  94. /**
  95. * @param val normalized value in [0, 1].
  96. * @return raw ordinal number.
  97. */
  98. OrdinalScale.prototype.scale = function (val) {
  99. val = Math.round(scaleHelper.scale(val, this._extent));
  100. return this.getRawOrdinalNumber(val);
  101. };
  102. OrdinalScale.prototype.getTicks = function () {
  103. var ticks = [];
  104. var extent = this._extent;
  105. var rank = extent[0];
  106. while (rank <= extent[1]) {
  107. ticks.push({
  108. value: rank
  109. });
  110. rank++;
  111. }
  112. return ticks;
  113. };
  114. OrdinalScale.prototype.getMinorTicks = function (splitNumber) {
  115. // Not support.
  116. return;
  117. };
  118. /**
  119. * @see `Ordinal['_ordinalNumbersByTick']`
  120. */
  121. OrdinalScale.prototype.setSortInfo = function (info) {
  122. if (info == null) {
  123. this._ordinalNumbersByTick = this._ticksByOrdinalNumber = null;
  124. return;
  125. }
  126. var infoOrdinalNumbers = info.ordinalNumbers;
  127. var ordinalsByTick = this._ordinalNumbersByTick = [];
  128. var ticksByOrdinal = this._ticksByOrdinalNumber = [];
  129. // Unnecessary support negative tick in `realtimeSort`.
  130. var tickNum = 0;
  131. var allCategoryLen = this._ordinalMeta.categories.length;
  132. for (var len = Math.min(allCategoryLen, infoOrdinalNumbers.length); tickNum < len; ++tickNum) {
  133. var ordinalNumber = infoOrdinalNumbers[tickNum];
  134. ordinalsByTick[tickNum] = ordinalNumber;
  135. ticksByOrdinal[ordinalNumber] = tickNum;
  136. }
  137. // Handle that `series.data` only covers part of the `axis.category.data`.
  138. var unusedOrdinal = 0;
  139. for (; tickNum < allCategoryLen; ++tickNum) {
  140. while (ticksByOrdinal[unusedOrdinal] != null) {
  141. unusedOrdinal++;
  142. }
  143. ;
  144. ordinalsByTick.push(unusedOrdinal);
  145. ticksByOrdinal[unusedOrdinal] = tickNum;
  146. }
  147. };
  148. OrdinalScale.prototype._getTickNumber = function (ordinal) {
  149. var ticksByOrdinalNumber = this._ticksByOrdinalNumber;
  150. // also support ordinal out of range of `ordinalMeta.categories.length`,
  151. // where ordinal numbers are used as tick value directly.
  152. return ticksByOrdinalNumber && ordinal >= 0 && ordinal < ticksByOrdinalNumber.length ? ticksByOrdinalNumber[ordinal] : ordinal;
  153. };
  154. /**
  155. * @usage
  156. * ```js
  157. * const ordinalNumber = ordinalScale.getRawOrdinalNumber(tickVal);
  158. *
  159. * // case0
  160. * const rawOrdinalValue = axisModel.getCategories()[ordinalNumber];
  161. * // case1
  162. * const rawOrdinalValue = this._ordinalMeta.categories[ordinalNumber];
  163. * // case2
  164. * const coord = axis.dataToCoord(ordinalNumber);
  165. * ```
  166. *
  167. * @param {OrdinalNumber} tickNumber index of display
  168. */
  169. OrdinalScale.prototype.getRawOrdinalNumber = function (tickNumber) {
  170. var ordinalNumbersByTick = this._ordinalNumbersByTick;
  171. // tickNumber may be out of range, e.g., when axis max is larger than `ordinalMeta.categories.length`.,
  172. // where ordinal numbers are used as tick value directly.
  173. return ordinalNumbersByTick && tickNumber >= 0 && tickNumber < ordinalNumbersByTick.length ? ordinalNumbersByTick[tickNumber] : tickNumber;
  174. };
  175. /**
  176. * Get item on tick
  177. */
  178. OrdinalScale.prototype.getLabel = function (tick) {
  179. if (!this.isBlank()) {
  180. var ordinalNumber = this.getRawOrdinalNumber(tick.value);
  181. var cateogry = this._ordinalMeta.categories[ordinalNumber];
  182. // Note that if no data, ordinalMeta.categories is an empty array.
  183. // Return empty if it's not exist.
  184. return cateogry == null ? '' : cateogry + '';
  185. }
  186. };
  187. OrdinalScale.prototype.count = function () {
  188. return this._extent[1] - this._extent[0] + 1;
  189. };
  190. OrdinalScale.prototype.unionExtentFromData = function (data, dim) {
  191. this.unionExtent(data.getApproximateExtent(dim));
  192. };
  193. /**
  194. * @override
  195. * If value is in extent range
  196. */
  197. OrdinalScale.prototype.isInExtentRange = function (value) {
  198. value = this._getTickNumber(value);
  199. return this._extent[0] <= value && this._extent[1] >= value;
  200. };
  201. OrdinalScale.prototype.getOrdinalMeta = function () {
  202. return this._ordinalMeta;
  203. };
  204. OrdinalScale.prototype.calcNiceTicks = function () {};
  205. OrdinalScale.prototype.calcNiceExtent = function () {};
  206. OrdinalScale.type = 'ordinal';
  207. return OrdinalScale;
  208. }(Scale);
  209. Scale.registerClass(OrdinalScale);
  210. export default OrdinalScale;