Ordinal.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 zrUtil = require("zrender/lib/core/util");
  20. var Scale = require("./Scale");
  21. var OrdinalMeta = require("../data/OrdinalMeta");
  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. /**
  41. * Linear continuous scale
  42. * @module echarts/coord/scale/Ordinal
  43. *
  44. * http://en.wikipedia.org/wiki/Level_of_measurement
  45. */
  46. // FIXME only one data
  47. var scaleProto = Scale.prototype;
  48. var OrdinalScale = Scale.extend({
  49. type: 'ordinal',
  50. /**
  51. * @param {module:echarts/data/OrdianlMeta|Array.<string>} ordinalMeta
  52. */
  53. init: function (ordinalMeta, extent) {
  54. // Caution: Should not use instanceof, consider ec-extensions using
  55. // import approach to get OrdinalMeta class.
  56. if (!ordinalMeta || zrUtil.isArray(ordinalMeta)) {
  57. ordinalMeta = new OrdinalMeta({
  58. categories: ordinalMeta
  59. });
  60. }
  61. this._ordinalMeta = ordinalMeta;
  62. this._extent = extent || [0, ordinalMeta.categories.length - 1];
  63. },
  64. parse: function (val) {
  65. return typeof val === 'string' ? this._ordinalMeta.getOrdinal(val) // val might be float.
  66. : Math.round(val);
  67. },
  68. contain: function (rank) {
  69. rank = this.parse(rank);
  70. return scaleProto.contain.call(this, rank) && this._ordinalMeta.categories[rank] != null;
  71. },
  72. /**
  73. * Normalize given rank or name to linear [0, 1]
  74. * @param {number|string} [val]
  75. * @return {number}
  76. */
  77. normalize: function (val) {
  78. return scaleProto.normalize.call(this, this.parse(val));
  79. },
  80. scale: function (val) {
  81. return Math.round(scaleProto.scale.call(this, val));
  82. },
  83. /**
  84. * @return {Array}
  85. */
  86. getTicks: function () {
  87. var ticks = [];
  88. var extent = this._extent;
  89. var rank = extent[0];
  90. while (rank <= extent[1]) {
  91. ticks.push(rank);
  92. rank++;
  93. }
  94. return ticks;
  95. },
  96. /**
  97. * Get item on rank n
  98. * @param {number} n
  99. * @return {string}
  100. */
  101. getLabel: function (n) {
  102. if (!this.isBlank()) {
  103. // Note that if no data, ordinalMeta.categories is an empty array.
  104. return this._ordinalMeta.categories[n];
  105. }
  106. },
  107. /**
  108. * @return {number}
  109. */
  110. count: function () {
  111. return this._extent[1] - this._extent[0] + 1;
  112. },
  113. /**
  114. * @override
  115. */
  116. unionExtentFromData: function (data, dim) {
  117. this.unionExtent(data.getApproximateExtent(dim));
  118. },
  119. getOrdinalMeta: function () {
  120. return this._ordinalMeta;
  121. },
  122. niceTicks: zrUtil.noop,
  123. niceExtent: zrUtil.noop
  124. });
  125. /**
  126. * @return {module:echarts/scale/Time}
  127. */
  128. OrdinalScale.create = function () {
  129. return new OrdinalScale();
  130. };
  131. var _default = OrdinalScale;
  132. module.exports = _default;