Ordinal.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 (val) {
  82. return scaleHelper.contain(val, this._extent) && val >= 0 && val < this._ordinalMeta.categories.length;
  83. };
  84. /**
  85. * Normalize given rank or name to linear [0, 1]
  86. * @param val raw ordinal number.
  87. * @return normalized value in [0, 1].
  88. */
  89. OrdinalScale.prototype.normalize = function (val) {
  90. val = this._getTickNumber(val);
  91. return this._calculator.normalize(val, this._extent);
  92. };
  93. /**
  94. * @param val normalized value in [0, 1].
  95. * @return raw ordinal number.
  96. */
  97. OrdinalScale.prototype.scale = function (val) {
  98. val = Math.round(this._calculator.scale(val, this._extent));
  99. return this.getRawOrdinalNumber(val);
  100. };
  101. OrdinalScale.prototype.getTicks = function () {
  102. var ticks = [];
  103. var extent = this._extent;
  104. var rank = extent[0];
  105. while (rank <= extent[1]) {
  106. ticks.push({
  107. value: rank
  108. });
  109. rank++;
  110. }
  111. return ticks;
  112. };
  113. OrdinalScale.prototype.getMinorTicks = function (splitNumber) {
  114. // Not support.
  115. return;
  116. };
  117. /**
  118. * @see `Ordinal['_ordinalNumbersByTick']`
  119. */
  120. OrdinalScale.prototype.setSortInfo = function (info) {
  121. if (info == null) {
  122. this._ordinalNumbersByTick = this._ticksByOrdinalNumber = null;
  123. return;
  124. }
  125. var infoOrdinalNumbers = info.ordinalNumbers;
  126. var ordinalsByTick = this._ordinalNumbersByTick = [];
  127. var ticksByOrdinal = this._ticksByOrdinalNumber = [];
  128. // Unnecessary support negative tick in `realtimeSort`.
  129. var tickNum = 0;
  130. var allCategoryLen = this._ordinalMeta.categories.length;
  131. for (var len = Math.min(allCategoryLen, infoOrdinalNumbers.length); tickNum < len; ++tickNum) {
  132. var ordinalNumber = infoOrdinalNumbers[tickNum];
  133. ordinalsByTick[tickNum] = ordinalNumber;
  134. ticksByOrdinal[ordinalNumber] = tickNum;
  135. }
  136. // Handle that `series.data` only covers part of the `axis.category.data`.
  137. var unusedOrdinal = 0;
  138. for (; tickNum < allCategoryLen; ++tickNum) {
  139. while (ticksByOrdinal[unusedOrdinal] != null) {
  140. unusedOrdinal++;
  141. }
  142. ;
  143. ordinalsByTick.push(unusedOrdinal);
  144. ticksByOrdinal[unusedOrdinal] = tickNum;
  145. }
  146. };
  147. OrdinalScale.prototype._getTickNumber = function (ordinal) {
  148. var ticksByOrdinalNumber = this._ticksByOrdinalNumber;
  149. // also support ordinal out of range of `ordinalMeta.categories.length`,
  150. // where ordinal numbers are used as tick value directly.
  151. return ticksByOrdinalNumber && ordinal >= 0 && ordinal < ticksByOrdinalNumber.length ? ticksByOrdinalNumber[ordinal] : ordinal;
  152. };
  153. /**
  154. * @usage
  155. * ```js
  156. * const ordinalNumber = ordinalScale.getRawOrdinalNumber(tickVal);
  157. *
  158. * // case0
  159. * const rawOrdinalValue = axisModel.getCategories()[ordinalNumber];
  160. * // case1
  161. * const rawOrdinalValue = this._ordinalMeta.categories[ordinalNumber];
  162. * // case2
  163. * const coord = axis.dataToCoord(ordinalNumber);
  164. * ```
  165. *
  166. * @param {OrdinalNumber} tickNumber index of display
  167. */
  168. OrdinalScale.prototype.getRawOrdinalNumber = function (tickNumber) {
  169. var ordinalNumbersByTick = this._ordinalNumbersByTick;
  170. // tickNumber may be out of range, e.g., when axis max is larger than `ordinalMeta.categories.length`.,
  171. // where ordinal numbers are used as tick value directly.
  172. return ordinalNumbersByTick && tickNumber >= 0 && tickNumber < ordinalNumbersByTick.length ? ordinalNumbersByTick[tickNumber] : tickNumber;
  173. };
  174. /**
  175. * Get item on tick
  176. */
  177. OrdinalScale.prototype.getLabel = function (tick) {
  178. if (!this.isBlank()) {
  179. var ordinalNumber = this.getRawOrdinalNumber(tick.value);
  180. var cateogry = this._ordinalMeta.categories[ordinalNumber];
  181. // Note that if no data, ordinalMeta.categories is an empty array.
  182. // Return empty if it's not exist.
  183. return cateogry == null ? '' : cateogry + '';
  184. }
  185. };
  186. OrdinalScale.prototype.count = function () {
  187. return this._extent[1] - this._extent[0] + 1;
  188. };
  189. /**
  190. * @override
  191. * If value is in extent range
  192. */
  193. OrdinalScale.prototype.isInExtentRange = function (value) {
  194. value = this._getTickNumber(value);
  195. return this._extent[0] <= value && this._extent[1] >= value;
  196. };
  197. OrdinalScale.prototype.getOrdinalMeta = function () {
  198. return this._ordinalMeta;
  199. };
  200. OrdinalScale.prototype.calcNiceTicks = function () {};
  201. OrdinalScale.prototype.calcNiceExtent = function () {};
  202. OrdinalScale.type = 'ordinal';
  203. return OrdinalScale;
  204. }(Scale);
  205. Scale.registerClass(OrdinalScale);
  206. export default OrdinalScale;