OrdinalMeta.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 { createHashMap, isObject, map, isString } from 'zrender/lib/core/util.js';
  41. var uidBase = 0;
  42. var OrdinalMeta = /** @class */function () {
  43. /**
  44. * PENDING - Regarding forcibly converting to string:
  45. * In the early days, the underlying hash map impl used JS plain object and converted the key to
  46. * string; later in https://github.com/ecomfe/zrender/pull/966 it was changed to a JS Map (in supported
  47. * platforms), which does not require string keys. But consider any input that `scale/Ordinal['parse']`
  48. * is involved, a number input represents an `OrdinalNumber` (i.e., an index), and affect the query
  49. * behavior:
  50. * - If forcbily converting to string:
  51. * pros: users can use numeric string (such as, '123') to query the raw data (123), tho it's probably
  52. * still confusing.
  53. * cons: NaN/null/undefined in data will be equals to 'NaN'/'null'/'undefined', if simply using
  54. * `val + ''` to convert them, like currently `getName` does.
  55. * - Otherwise:
  56. * pros: see NaN/null/undefined case above.
  57. * cons: users cannot query the raw data (123) any more.
  58. * There are two inconsistent behaviors in the current impl:
  59. * - Force conversion is applied on the case `xAxis{data: ['aaa', 'bbb', ...]}`,
  60. * but no conversion applied to the case `xAxis{data: [{value: 'aaa'}, ...]}` and
  61. * the case `dataset: {source: [['aaa', 123], ['bbb', 234], ...]}`.
  62. * - behaves differently according to whether JS Map is supported (the polyfill is simply using JS
  63. * plain object) (tho it seems rare platform that do not support it).
  64. * Since there's no sufficient good solution to offset cost of the breaking change, we preserve the
  65. * current behavior, until real issues is reported.
  66. */
  67. function OrdinalMeta(opt) {
  68. this.categories = opt.categories || [];
  69. this._needCollect = opt.needCollect;
  70. this._deduplication = opt.deduplication;
  71. this.uid = ++uidBase;
  72. this._onCollect = opt.onCollect;
  73. }
  74. OrdinalMeta.createByAxisModel = function (axisModel) {
  75. var option = axisModel.option;
  76. var data = option.data;
  77. var categories = data && map(data, getName);
  78. return new OrdinalMeta({
  79. categories: categories,
  80. needCollect: !categories,
  81. // deduplication is default in axis.
  82. deduplication: option.dedplication !== false
  83. });
  84. };
  85. ;
  86. OrdinalMeta.prototype.getOrdinal = function (category) {
  87. return this._getOrCreateMap().get(category);
  88. };
  89. /**
  90. * @return The ordinal. If not found, return NaN.
  91. */
  92. OrdinalMeta.prototype.parseAndCollect = function (category) {
  93. var index;
  94. var needCollect = this._needCollect;
  95. // The value of category dim can be the index of the given category set.
  96. // This feature is only supported when !needCollect, because we should
  97. // consider a common case: a value is 2017, which is a number but is
  98. // expected to be tread as a category. This case usually happen in dataset,
  99. // where it happent to be no need of the index feature.
  100. if (!isString(category) && !needCollect) {
  101. return category;
  102. }
  103. // Optimize for the scenario:
  104. // category is ['2012-01-01', '2012-01-02', ...], where the input
  105. // data has been ensured not duplicate and is large data.
  106. // Notice, if a dataset dimension provide categroies, usually echarts
  107. // should remove duplication except user tell echarts dont do that
  108. // (set axis.deduplication = false), because echarts do not know whether
  109. // the values in the category dimension has duplication (consider the
  110. // parallel-aqi example)
  111. if (needCollect && !this._deduplication) {
  112. index = this.categories.length;
  113. this.categories[index] = category;
  114. this._onCollect && this._onCollect(category, index);
  115. return index;
  116. }
  117. var map = this._getOrCreateMap();
  118. index = map.get(category);
  119. if (index == null) {
  120. if (needCollect) {
  121. index = this.categories.length;
  122. this.categories[index] = category;
  123. map.set(category, index);
  124. this._onCollect && this._onCollect(category, index);
  125. } else {
  126. index = NaN;
  127. }
  128. }
  129. return index;
  130. };
  131. // Consider big data, do not create map until needed.
  132. OrdinalMeta.prototype._getOrCreateMap = function () {
  133. return this._map || (this._map = createHashMap(this.categories));
  134. };
  135. return OrdinalMeta;
  136. }();
  137. function getName(obj) {
  138. if (isObject(obj) && obj.value != null) {
  139. return obj.value;
  140. } else {
  141. return obj + '';
  142. }
  143. }
  144. export default OrdinalMeta;