Cartesian.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. /**
  39. * Cartesian coordinate system
  40. * @module echarts/coord/Cartesian
  41. *
  42. */
  43. function dimAxisMapper(dim) {
  44. return this._axes[dim];
  45. }
  46. /**
  47. * @alias module:echarts/coord/Cartesian
  48. * @constructor
  49. */
  50. var Cartesian = function (name) {
  51. this._axes = {};
  52. this._dimList = [];
  53. /**
  54. * @type {string}
  55. */
  56. this.name = name || '';
  57. };
  58. Cartesian.prototype = {
  59. constructor: Cartesian,
  60. type: 'cartesian',
  61. /**
  62. * Get axis
  63. * @param {number|string} dim
  64. * @return {module:echarts/coord/Cartesian~Axis}
  65. */
  66. getAxis: function (dim) {
  67. return this._axes[dim];
  68. },
  69. /**
  70. * Get axes list
  71. * @return {Array.<module:echarts/coord/Cartesian~Axis>}
  72. */
  73. getAxes: function () {
  74. return zrUtil.map(this._dimList, dimAxisMapper, this);
  75. },
  76. /**
  77. * Get axes list by given scale type
  78. */
  79. getAxesByScale: function (scaleType) {
  80. scaleType = scaleType.toLowerCase();
  81. return zrUtil.filter(this.getAxes(), function (axis) {
  82. return axis.scale.type === scaleType;
  83. });
  84. },
  85. /**
  86. * Add axis
  87. * @param {module:echarts/coord/Cartesian.Axis}
  88. */
  89. addAxis: function (axis) {
  90. var dim = axis.dim;
  91. this._axes[dim] = axis;
  92. this._dimList.push(dim);
  93. },
  94. /**
  95. * Convert data to coord in nd space
  96. * @param {Array.<number>|Object.<string, number>} val
  97. * @return {Array.<number>|Object.<string, number>}
  98. */
  99. dataToCoord: function (val) {
  100. return this._dataCoordConvert(val, 'dataToCoord');
  101. },
  102. /**
  103. * Convert coord in nd space to data
  104. * @param {Array.<number>|Object.<string, number>} val
  105. * @return {Array.<number>|Object.<string, number>}
  106. */
  107. coordToData: function (val) {
  108. return this._dataCoordConvert(val, 'coordToData');
  109. },
  110. _dataCoordConvert: function (input, method) {
  111. var dimList = this._dimList;
  112. var output = input instanceof Array ? [] : {};
  113. for (var i = 0; i < dimList.length; i++) {
  114. var dim = dimList[i];
  115. var axis = this._axes[dim];
  116. output[dim] = axis[method](input[dim]);
  117. }
  118. return output;
  119. }
  120. };
  121. var _default = Cartesian;
  122. module.exports = _default;