Ordinal.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 =
  51. /** @class */
  52. function (_super) {
  53. __extends(OrdinalScale, _super);
  54. function OrdinalScale(setting) {
  55. var _this = _super.call(this, setting) || this;
  56. _this.type = 'ordinal';
  57. var ordinalMeta = _this.getSetting('ordinalMeta'); // Caution: Should not use instanceof, consider ec-extensions using
  58. // import approach to get OrdinalMeta class.
  59. if (!ordinalMeta) {
  60. ordinalMeta = new OrdinalMeta({});
  61. }
  62. if (isArray(ordinalMeta)) {
  63. ordinalMeta = new OrdinalMeta({
  64. categories: map(ordinalMeta, function (item) {
  65. return isObject(item) ? item.value : item;
  66. })
  67. });
  68. }
  69. _this._ordinalMeta = ordinalMeta;
  70. _this._extent = _this.getSetting('extent') || [0, ordinalMeta.categories.length - 1];
  71. return _this;
  72. }
  73. OrdinalScale.prototype.parse = function (val) {
  74. // Caution: Math.round(null) will return `0` rather than `NaN`
  75. if (val == null) {
  76. return NaN;
  77. }
  78. return isString(val) ? this._ordinalMeta.getOrdinal(val) // 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 = []; // 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. } // Handle that `series.data` only covers part of the `axis.category.data`.
  136. var unusedOrdinal = 0;
  137. for (; tickNum < allCategoryLen; ++tickNum) {
  138. while (ticksByOrdinal[unusedOrdinal] != null) {
  139. unusedOrdinal++;
  140. }
  141. ;
  142. ordinalsByTick.push(unusedOrdinal);
  143. ticksByOrdinal[unusedOrdinal] = tickNum;
  144. }
  145. };
  146. OrdinalScale.prototype._getTickNumber = function (ordinal) {
  147. var ticksByOrdinalNumber = this._ticksByOrdinalNumber; // also support ordinal out of range of `ordinalMeta.categories.length`,
  148. // where ordinal numbers are used as tick value directly.
  149. return ticksByOrdinalNumber && ordinal >= 0 && ordinal < ticksByOrdinalNumber.length ? ticksByOrdinalNumber[ordinal] : ordinal;
  150. };
  151. /**
  152. * @usage
  153. * ```js
  154. * const ordinalNumber = ordinalScale.getRawOrdinalNumber(tickVal);
  155. *
  156. * // case0
  157. * const rawOrdinalValue = axisModel.getCategories()[ordinalNumber];
  158. * // case1
  159. * const rawOrdinalValue = this._ordinalMeta.categories[ordinalNumber];
  160. * // case2
  161. * const coord = axis.dataToCoord(ordinalNumber);
  162. * ```
  163. *
  164. * @param {OrdinalNumber} tickNumber index of display
  165. */
  166. OrdinalScale.prototype.getRawOrdinalNumber = function (tickNumber) {
  167. var ordinalNumbersByTick = this._ordinalNumbersByTick; // tickNumber may be out of range, e.g., when axis max is larger than `ordinalMeta.categories.length`.,
  168. // where ordinal numbers are used as tick value directly.
  169. return ordinalNumbersByTick && tickNumber >= 0 && tickNumber < ordinalNumbersByTick.length ? ordinalNumbersByTick[tickNumber] : tickNumber;
  170. };
  171. /**
  172. * Get item on tick
  173. */
  174. OrdinalScale.prototype.getLabel = function (tick) {
  175. if (!this.isBlank()) {
  176. var ordinalNumber = this.getRawOrdinalNumber(tick.value);
  177. var cateogry = this._ordinalMeta.categories[ordinalNumber]; // Note that if no data, ordinalMeta.categories is an empty array.
  178. // Return empty if it's not exist.
  179. return cateogry == null ? '' : cateogry + '';
  180. }
  181. };
  182. OrdinalScale.prototype.count = function () {
  183. return this._extent[1] - this._extent[0] + 1;
  184. };
  185. OrdinalScale.prototype.unionExtentFromData = function (data, dim) {
  186. this.unionExtent(data.getApproximateExtent(dim));
  187. };
  188. /**
  189. * @override
  190. * If value is in extent range
  191. */
  192. OrdinalScale.prototype.isInExtentRange = function (value) {
  193. value = this._getTickNumber(value);
  194. return this._extent[0] <= value && this._extent[1] >= value;
  195. };
  196. OrdinalScale.prototype.getOrdinalMeta = function () {
  197. return this._ordinalMeta;
  198. };
  199. OrdinalScale.prototype.calcNiceTicks = function () {};
  200. OrdinalScale.prototype.calcNiceExtent = function () {};
  201. OrdinalScale.type = 'ordinal';
  202. return OrdinalScale;
  203. }(Scale);
  204. Scale.registerClass(OrdinalScale);
  205. export default OrdinalScale;