Cartesian2D.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 { __extends } from "tslib";
  41. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  42. import Cartesian from './Cartesian.js';
  43. import { invert } from 'zrender/lib/core/matrix.js';
  44. import { applyTransform } from 'zrender/lib/core/vector.js';
  45. export var cartesian2DDimensions = ['x', 'y'];
  46. function canCalculateAffineTransform(scale) {
  47. return scale.type === 'interval' || scale.type === 'time';
  48. }
  49. var Cartesian2D = /** @class */function (_super) {
  50. __extends(Cartesian2D, _super);
  51. function Cartesian2D() {
  52. var _this = _super !== null && _super.apply(this, arguments) || this;
  53. _this.type = 'cartesian2d';
  54. _this.dimensions = cartesian2DDimensions;
  55. return _this;
  56. }
  57. /**
  58. * Calculate an affine transform matrix if two axes are time or value.
  59. * It's mainly for accelartion on the large time series data.
  60. */
  61. Cartesian2D.prototype.calcAffineTransform = function () {
  62. this._transform = this._invTransform = null;
  63. var xAxisScale = this.getAxis('x').scale;
  64. var yAxisScale = this.getAxis('y').scale;
  65. if (!canCalculateAffineTransform(xAxisScale) || !canCalculateAffineTransform(yAxisScale)) {
  66. return;
  67. }
  68. var xScaleExtent = xAxisScale.getExtent();
  69. var yScaleExtent = yAxisScale.getExtent();
  70. var start = this.dataToPoint([xScaleExtent[0], yScaleExtent[0]]);
  71. var end = this.dataToPoint([xScaleExtent[1], yScaleExtent[1]]);
  72. var xScaleSpan = xScaleExtent[1] - xScaleExtent[0];
  73. var yScaleSpan = yScaleExtent[1] - yScaleExtent[0];
  74. if (!xScaleSpan || !yScaleSpan) {
  75. return;
  76. }
  77. // Accelerate data to point calculation on the special large time series data.
  78. var scaleX = (end[0] - start[0]) / xScaleSpan;
  79. var scaleY = (end[1] - start[1]) / yScaleSpan;
  80. var translateX = start[0] - xScaleExtent[0] * scaleX;
  81. var translateY = start[1] - yScaleExtent[0] * scaleY;
  82. var m = this._transform = [scaleX, 0, 0, scaleY, translateX, translateY];
  83. this._invTransform = invert([], m);
  84. };
  85. /**
  86. * Base axis will be used on stacking.
  87. */
  88. Cartesian2D.prototype.getBaseAxis = function () {
  89. return this.getAxesByScale('ordinal')[0] || this.getAxesByScale('time')[0] || this.getAxis('x');
  90. };
  91. Cartesian2D.prototype.containPoint = function (point) {
  92. var axisX = this.getAxis('x');
  93. var axisY = this.getAxis('y');
  94. return axisX.contain(axisX.toLocalCoord(point[0])) && axisY.contain(axisY.toLocalCoord(point[1]));
  95. };
  96. Cartesian2D.prototype.containData = function (data) {
  97. return this.getAxis('x').containData(data[0]) && this.getAxis('y').containData(data[1]);
  98. };
  99. Cartesian2D.prototype.containZone = function (data1, data2) {
  100. var zoneDiag1 = this.dataToPoint(data1);
  101. var zoneDiag2 = this.dataToPoint(data2);
  102. var area = this.getArea();
  103. var zone = new BoundingRect(zoneDiag1[0], zoneDiag1[1], zoneDiag2[0] - zoneDiag1[0], zoneDiag2[1] - zoneDiag1[1]);
  104. return area.intersect(zone);
  105. };
  106. Cartesian2D.prototype.dataToPoint = function (data, clamp, out) {
  107. out = out || [];
  108. var xVal = data[0];
  109. var yVal = data[1];
  110. // Fast path
  111. if (this._transform
  112. // It's supported that if data is like `[Inifity, 123]`, where only Y pixel calculated.
  113. && xVal != null && isFinite(xVal) && yVal != null && isFinite(yVal)) {
  114. return applyTransform(out, data, this._transform);
  115. }
  116. var xAxis = this.getAxis('x');
  117. var yAxis = this.getAxis('y');
  118. out[0] = xAxis.toGlobalCoord(xAxis.dataToCoord(xVal, clamp));
  119. out[1] = yAxis.toGlobalCoord(yAxis.dataToCoord(yVal, clamp));
  120. return out;
  121. };
  122. Cartesian2D.prototype.clampData = function (data, out) {
  123. var xScale = this.getAxis('x').scale;
  124. var yScale = this.getAxis('y').scale;
  125. var xAxisExtent = xScale.getExtent();
  126. var yAxisExtent = yScale.getExtent();
  127. var x = xScale.parse(data[0]);
  128. var y = yScale.parse(data[1]);
  129. out = out || [];
  130. out[0] = Math.min(Math.max(Math.min(xAxisExtent[0], xAxisExtent[1]), x), Math.max(xAxisExtent[0], xAxisExtent[1]));
  131. out[1] = Math.min(Math.max(Math.min(yAxisExtent[0], yAxisExtent[1]), y), Math.max(yAxisExtent[0], yAxisExtent[1]));
  132. return out;
  133. };
  134. Cartesian2D.prototype.pointToData = function (point, clamp) {
  135. var out = [];
  136. if (this._invTransform) {
  137. return applyTransform(out, point, this._invTransform);
  138. }
  139. var xAxis = this.getAxis('x');
  140. var yAxis = this.getAxis('y');
  141. out[0] = xAxis.coordToData(xAxis.toLocalCoord(point[0]), clamp);
  142. out[1] = yAxis.coordToData(yAxis.toLocalCoord(point[1]), clamp);
  143. return out;
  144. };
  145. Cartesian2D.prototype.getOtherAxis = function (axis) {
  146. return this.getAxis(axis.dim === 'x' ? 'y' : 'x');
  147. };
  148. /**
  149. * Get rect area of cartesian.
  150. * Area will have a contain function to determine if a point is in the coordinate system.
  151. */
  152. Cartesian2D.prototype.getArea = function (tolerance) {
  153. tolerance = tolerance || 0;
  154. var xExtent = this.getAxis('x').getGlobalExtent();
  155. var yExtent = this.getAxis('y').getGlobalExtent();
  156. var x = Math.min(xExtent[0], xExtent[1]) - tolerance;
  157. var y = Math.min(yExtent[0], yExtent[1]) - tolerance;
  158. var width = Math.max(xExtent[0], xExtent[1]) - x + tolerance;
  159. var height = Math.max(yExtent[0], yExtent[1]) - y + tolerance;
  160. return new BoundingRect(x, y, width, height);
  161. };
  162. return Cartesian2D;
  163. }(Cartesian);
  164. ;
  165. export default Cartesian2D;