OrdinalMeta.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 =
  43. /** @class */
  44. function () {
  45. function OrdinalMeta(opt) {
  46. this.categories = opt.categories || [];
  47. this._needCollect = opt.needCollect;
  48. this._deduplication = opt.deduplication;
  49. this.uid = ++uidBase;
  50. }
  51. OrdinalMeta.createByAxisModel = function (axisModel) {
  52. var option = axisModel.option;
  53. var data = option.data;
  54. var categories = data && map(data, getName);
  55. return new OrdinalMeta({
  56. categories: categories,
  57. needCollect: !categories,
  58. // deduplication is default in axis.
  59. deduplication: option.dedplication !== false
  60. });
  61. };
  62. ;
  63. OrdinalMeta.prototype.getOrdinal = function (category) {
  64. // @ts-ignore
  65. return this._getOrCreateMap().get(category);
  66. };
  67. /**
  68. * @return The ordinal. If not found, return NaN.
  69. */
  70. OrdinalMeta.prototype.parseAndCollect = function (category) {
  71. var index;
  72. var needCollect = this._needCollect; // The value of category dim can be the index of the given category set.
  73. // This feature is only supported when !needCollect, because we should
  74. // consider a common case: a value is 2017, which is a number but is
  75. // expected to be tread as a category. This case usually happen in dataset,
  76. // where it happent to be no need of the index feature.
  77. if (!isString(category) && !needCollect) {
  78. return category;
  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(); // @ts-ignore
  93. index = map.get(category);
  94. if (index == null) {
  95. if (needCollect) {
  96. index = this.categories.length;
  97. this.categories[index] = category; // @ts-ignore
  98. map.set(category, index);
  99. } else {
  100. index = NaN;
  101. }
  102. }
  103. return index;
  104. }; // Consider big data, do not create map until needed.
  105. OrdinalMeta.prototype._getOrCreateMap = function () {
  106. return this._map || (this._map = createHashMap(this.categories));
  107. };
  108. return OrdinalMeta;
  109. }();
  110. function getName(obj) {
  111. if (isObject(obj) && obj.value != null) {
  112. return obj.value;
  113. } else {
  114. return obj + '';
  115. }
  116. }
  117. export default OrdinalMeta;