OrdinalMeta.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. function OrdinalMeta(opt) {
  44. this.categories = opt.categories || [];
  45. this._needCollect = opt.needCollect;
  46. this._deduplication = opt.deduplication;
  47. this.uid = ++uidBase;
  48. }
  49. OrdinalMeta.createByAxisModel = function (axisModel) {
  50. var option = axisModel.option;
  51. var data = option.data;
  52. var categories = data && map(data, getName);
  53. return new OrdinalMeta({
  54. categories: categories,
  55. needCollect: !categories,
  56. // deduplication is default in axis.
  57. deduplication: option.dedplication !== false
  58. });
  59. };
  60. ;
  61. OrdinalMeta.prototype.getOrdinal = function (category) {
  62. // @ts-ignore
  63. return this._getOrCreateMap().get(category);
  64. };
  65. /**
  66. * @return The ordinal. If not found, return NaN.
  67. */
  68. OrdinalMeta.prototype.parseAndCollect = function (category) {
  69. var index;
  70. var needCollect = this._needCollect;
  71. // The value of category dim can be the index of the given category set.
  72. // This feature is only supported when !needCollect, because we should
  73. // consider a common case: a value is 2017, which is a number but is
  74. // expected to be tread as a category. This case usually happen in dataset,
  75. // where it happent to be no need of the index feature.
  76. if (!isString(category) && !needCollect) {
  77. return category;
  78. }
  79. // Optimize for the scenario:
  80. // category is ['2012-01-01', '2012-01-02', ...], where the input
  81. // data has been ensured not duplicate and is large data.
  82. // Notice, if a dataset dimension provide categroies, usually echarts
  83. // should remove duplication except user tell echarts dont do that
  84. // (set axis.deduplication = false), because echarts do not know whether
  85. // the values in the category dimension has duplication (consider the
  86. // parallel-aqi example)
  87. if (needCollect && !this._deduplication) {
  88. index = this.categories.length;
  89. this.categories[index] = category;
  90. return index;
  91. }
  92. var map = this._getOrCreateMap();
  93. // @ts-ignore
  94. index = map.get(category);
  95. if (index == null) {
  96. if (needCollect) {
  97. index = this.categories.length;
  98. this.categories[index] = category;
  99. // @ts-ignore
  100. map.set(category, index);
  101. } else {
  102. index = NaN;
  103. }
  104. }
  105. return index;
  106. };
  107. // Consider big data, do not create map until needed.
  108. OrdinalMeta.prototype._getOrCreateMap = function () {
  109. return this._map || (this._map = createHashMap(this.categories));
  110. };
  111. return OrdinalMeta;
  112. }();
  113. function getName(obj) {
  114. if (isObject(obj) && obj.value != null) {
  115. return obj.value;
  116. } else {
  117. return obj + '';
  118. }
  119. }
  120. export default OrdinalMeta;