Ordinal.js 7.5 KB

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