Cartesian2D.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 BoundingRect = require("zrender/lib/core/BoundingRect");
  21. var Cartesian = require("./Cartesian");
  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. function Cartesian2D(name) {
  41. Cartesian.call(this, name);
  42. }
  43. Cartesian2D.prototype = {
  44. constructor: Cartesian2D,
  45. type: 'cartesian2d',
  46. /**
  47. * @type {Array.<string>}
  48. * @readOnly
  49. */
  50. dimensions: ['x', 'y'],
  51. /**
  52. * Base axis will be used on stacking.
  53. *
  54. * @return {module:echarts/coord/cartesian/Axis2D}
  55. */
  56. getBaseAxis: function () {
  57. return this.getAxesByScale('ordinal')[0] || this.getAxesByScale('time')[0] || this.getAxis('x');
  58. },
  59. /**
  60. * If contain point
  61. * @param {Array.<number>} point
  62. * @return {boolean}
  63. */
  64. containPoint: function (point) {
  65. var axisX = this.getAxis('x');
  66. var axisY = this.getAxis('y');
  67. return axisX.contain(axisX.toLocalCoord(point[0])) && axisY.contain(axisY.toLocalCoord(point[1]));
  68. },
  69. /**
  70. * If contain data
  71. * @param {Array.<number>} data
  72. * @return {boolean}
  73. */
  74. containData: function (data) {
  75. return this.getAxis('x').containData(data[0]) && this.getAxis('y').containData(data[1]);
  76. },
  77. /**
  78. * @param {Array.<number>} data
  79. * @param {Array.<number>} out
  80. * @return {Array.<number>}
  81. */
  82. dataToPoint: function (data, reserved, out) {
  83. var xAxis = this.getAxis('x');
  84. var yAxis = this.getAxis('y');
  85. out = out || [];
  86. out[0] = xAxis.toGlobalCoord(xAxis.dataToCoord(data[0]));
  87. out[1] = yAxis.toGlobalCoord(yAxis.dataToCoord(data[1]));
  88. return out;
  89. },
  90. /**
  91. * @param {Array.<number>} data
  92. * @param {Array.<number>} out
  93. * @return {Array.<number>}
  94. */
  95. clampData: function (data, out) {
  96. var xScale = this.getAxis('x').scale;
  97. var yScale = this.getAxis('y').scale;
  98. var xAxisExtent = xScale.getExtent();
  99. var yAxisExtent = yScale.getExtent();
  100. var x = xScale.parse(data[0]);
  101. var y = yScale.parse(data[1]);
  102. out = out || [];
  103. out[0] = Math.min(Math.max(Math.min(xAxisExtent[0], xAxisExtent[1]), x), Math.max(xAxisExtent[0], xAxisExtent[1]));
  104. out[1] = Math.min(Math.max(Math.min(yAxisExtent[0], yAxisExtent[1]), y), Math.max(yAxisExtent[0], yAxisExtent[1]));
  105. return out;
  106. },
  107. /**
  108. * @param {Array.<number>} point
  109. * @param {Array.<number>} out
  110. * @return {Array.<number>}
  111. */
  112. pointToData: function (point, out) {
  113. var xAxis = this.getAxis('x');
  114. var yAxis = this.getAxis('y');
  115. out = out || [];
  116. out[0] = xAxis.coordToData(xAxis.toLocalCoord(point[0]));
  117. out[1] = yAxis.coordToData(yAxis.toLocalCoord(point[1]));
  118. return out;
  119. },
  120. /**
  121. * Get other axis
  122. * @param {module:echarts/coord/cartesian/Axis2D} axis
  123. */
  124. getOtherAxis: function (axis) {
  125. return this.getAxis(axis.dim === 'x' ? 'y' : 'x');
  126. },
  127. /**
  128. * Get rect area of cartesian.
  129. * Area will have a contain function to determine if a point is in the coordinate system.
  130. * @return {BoundingRect}
  131. */
  132. getArea: function () {
  133. var xExtent = this.getAxis('x').getGlobalExtent();
  134. var yExtent = this.getAxis('y').getGlobalExtent();
  135. var x = Math.min(xExtent[0], xExtent[1]);
  136. var y = Math.min(yExtent[0], yExtent[1]);
  137. var width = Math.max(xExtent[0], xExtent[1]) - x;
  138. var height = Math.max(yExtent[0], yExtent[1]) - y;
  139. var rect = new BoundingRect(x, y, width, height);
  140. return rect;
  141. }
  142. };
  143. zrUtil.inherits(Cartesian2D, Cartesian);
  144. var _default = Cartesian2D;
  145. module.exports = _default;