OrdinalMeta.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. var _util = require("zrender/lib/core/util");
  20. var createHashMap = _util.createHashMap;
  21. var isObject = _util.isObject;
  22. var map = _util.map;
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. /**
  42. * @constructor
  43. * @param {Object} [opt]
  44. * @param {Object} [opt.categories=[]]
  45. * @param {Object} [opt.needCollect=false]
  46. * @param {Object} [opt.deduplication=false]
  47. */
  48. function OrdinalMeta(opt) {
  49. /**
  50. * @readOnly
  51. * @type {Array.<string>}
  52. */
  53. this.categories = opt.categories || [];
  54. /**
  55. * @private
  56. * @type {boolean}
  57. */
  58. this._needCollect = opt.needCollect;
  59. /**
  60. * @private
  61. * @type {boolean}
  62. */
  63. this._deduplication = opt.deduplication;
  64. /**
  65. * @private
  66. * @type {boolean}
  67. */
  68. this._map;
  69. }
  70. /**
  71. * @param {module:echarts/model/Model} axisModel
  72. * @return {module:echarts/data/OrdinalMeta}
  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. var proto = OrdinalMeta.prototype;
  86. /**
  87. * @param {string} category
  88. * @return {number} ordinal
  89. */
  90. proto.getOrdinal = function (category) {
  91. return getOrCreateMap(this).get(category);
  92. };
  93. /**
  94. * @param {*} category
  95. * @return {number} The ordinal. If not found, return NaN.
  96. */
  97. proto.parseAndCollect = function (category) {
  98. var index;
  99. var needCollect = this._needCollect; // The value of category dim can be the index of the given category set.
  100. // This feature is only supported when !needCollect, because we should
  101. // consider a common case: a value is 2017, which is a number but is
  102. // expected to be tread as a category. This case usually happen in dataset,
  103. // where it happent to be no need of the index feature.
  104. if (typeof category !== 'string' && !needCollect) {
  105. return category;
  106. } // Optimize for the scenario:
  107. // category is ['2012-01-01', '2012-01-02', ...], where the input
  108. // data has been ensured not duplicate and is large data.
  109. // Notice, if a dataset dimension provide categroies, usually echarts
  110. // should remove duplication except user tell echarts dont do that
  111. // (set axis.deduplication = false), because echarts do not know whether
  112. // the values in the category dimension has duplication (consider the
  113. // parallel-aqi example)
  114. if (needCollect && !this._deduplication) {
  115. index = this.categories.length;
  116. this.categories[index] = category;
  117. return index;
  118. }
  119. var map = getOrCreateMap(this);
  120. index = map.get(category);
  121. if (index == null) {
  122. if (needCollect) {
  123. index = this.categories.length;
  124. this.categories[index] = category;
  125. map.set(category, index);
  126. } else {
  127. index = NaN;
  128. }
  129. }
  130. return index;
  131. }; // Consider big data, do not create map until needed.
  132. function getOrCreateMap(ordinalMeta) {
  133. return ordinalMeta._map || (ordinalMeta._map = createHashMap(ordinalMeta.categories));
  134. }
  135. function getName(obj) {
  136. if (isObject(obj) && obj.value != null) {
  137. return obj.value;
  138. } else {
  139. return obj + '';
  140. }
  141. }
  142. var _default = OrdinalMeta;
  143. module.exports = _default;