Single.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /**
  41. * Single coordinates system.
  42. */
  43. import SingleAxis from './SingleAxis.js';
  44. import * as axisHelper from '../axisHelper.js';
  45. import { createBoxLayoutReference, getLayoutRect } from '../../util/layout.js';
  46. import { each } from 'zrender/lib/core/util.js';
  47. export var singleDimensions = ['single'];
  48. /**
  49. * Create a single coordinates system.
  50. */
  51. var Single = /** @class */function () {
  52. function Single(axisModel, ecModel, api) {
  53. this.type = 'single';
  54. this.dimension = 'single';
  55. /**
  56. * Add it just for draw tooltip.
  57. */
  58. this.dimensions = singleDimensions;
  59. this.axisPointerEnabled = true;
  60. this.model = axisModel;
  61. this._init(axisModel, ecModel, api);
  62. }
  63. /**
  64. * Initialize single coordinate system.
  65. */
  66. Single.prototype._init = function (axisModel, ecModel, api) {
  67. var dim = this.dimension;
  68. var axis = new SingleAxis(dim, axisHelper.createScaleByModel(axisModel), [0, 0], axisModel.get('type'), axisModel.get('position'));
  69. var isCategory = axis.type === 'category';
  70. axis.onBand = isCategory && axisModel.get('boundaryGap');
  71. axis.inverse = axisModel.get('inverse');
  72. axis.orient = axisModel.get('orient');
  73. axisModel.axis = axis;
  74. axis.model = axisModel;
  75. axis.coordinateSystem = this;
  76. this._axis = axis;
  77. };
  78. /**
  79. * Update axis scale after data processed
  80. */
  81. Single.prototype.update = function (ecModel, api) {
  82. ecModel.eachSeries(function (seriesModel) {
  83. if (seriesModel.coordinateSystem === this) {
  84. var data_1 = seriesModel.getData();
  85. each(data_1.mapDimensionsAll(this.dimension), function (dim) {
  86. this._axis.scale.unionExtentFromData(data_1, dim);
  87. }, this);
  88. axisHelper.niceScaleExtent(this._axis.scale, this._axis.model);
  89. }
  90. }, this);
  91. };
  92. /**
  93. * Resize the single coordinate system.
  94. */
  95. Single.prototype.resize = function (axisModel, api) {
  96. var refContainer = createBoxLayoutReference(axisModel, api).refContainer;
  97. this._rect = getLayoutRect(axisModel.getBoxLayoutParams(), refContainer);
  98. this._adjustAxis();
  99. };
  100. Single.prototype.getRect = function () {
  101. return this._rect;
  102. };
  103. Single.prototype._adjustAxis = function () {
  104. var rect = this._rect;
  105. var axis = this._axis;
  106. var isHorizontal = axis.isHorizontal();
  107. var extent = isHorizontal ? [0, rect.width] : [0, rect.height];
  108. var idx = axis.inverse ? 1 : 0;
  109. axis.setExtent(extent[idx], extent[1 - idx]);
  110. this._updateAxisTransform(axis, isHorizontal ? rect.x : rect.y);
  111. };
  112. Single.prototype._updateAxisTransform = function (axis, coordBase) {
  113. var axisExtent = axis.getExtent();
  114. var extentSum = axisExtent[0] + axisExtent[1];
  115. var isHorizontal = axis.isHorizontal();
  116. axis.toGlobalCoord = isHorizontal ? function (coord) {
  117. return coord + coordBase;
  118. } : function (coord) {
  119. return extentSum - coord + coordBase;
  120. };
  121. axis.toLocalCoord = isHorizontal ? function (coord) {
  122. return coord - coordBase;
  123. } : function (coord) {
  124. return extentSum - coord + coordBase;
  125. };
  126. };
  127. /**
  128. * Get axis.
  129. */
  130. Single.prototype.getAxis = function () {
  131. return this._axis;
  132. };
  133. /**
  134. * Get axis, add it just for draw tooltip.
  135. */
  136. Single.prototype.getBaseAxis = function () {
  137. return this._axis;
  138. };
  139. Single.prototype.getAxes = function () {
  140. return [this._axis];
  141. };
  142. Single.prototype.getTooltipAxes = function () {
  143. return {
  144. baseAxes: [this.getAxis()],
  145. // Empty otherAxes
  146. otherAxes: []
  147. };
  148. };
  149. /**
  150. * If contain point.
  151. */
  152. Single.prototype.containPoint = function (point) {
  153. var rect = this.getRect();
  154. var axis = this.getAxis();
  155. var orient = axis.orient;
  156. if (orient === 'horizontal') {
  157. return axis.contain(axis.toLocalCoord(point[0])) && point[1] >= rect.y && point[1] <= rect.y + rect.height;
  158. } else {
  159. return axis.contain(axis.toLocalCoord(point[1])) && point[0] >= rect.y && point[0] <= rect.y + rect.height;
  160. }
  161. };
  162. Single.prototype.pointToData = function (point, reserved, out) {
  163. out = out || [];
  164. var axis = this.getAxis();
  165. out[0] = axis.coordToData(axis.toLocalCoord(point[axis.orient === 'horizontal' ? 0 : 1]));
  166. return out;
  167. };
  168. /**
  169. * Convert the series data to concrete point.
  170. * Can be [val] | val
  171. */
  172. Single.prototype.dataToPoint = function (val, reserved, out) {
  173. var axis = this.getAxis();
  174. var rect = this.getRect();
  175. out = out || [];
  176. var idx = axis.orient === 'horizontal' ? 0 : 1;
  177. if (val instanceof Array) {
  178. val = val[0];
  179. }
  180. out[idx] = axis.toGlobalCoord(axis.dataToCoord(+val));
  181. out[1 - idx] = idx === 0 ? rect.y + rect.height / 2 : rect.x + rect.width / 2;
  182. return out;
  183. };
  184. Single.prototype.convertToPixel = function (ecModel, finder, value) {
  185. var coordSys = getCoordSys(finder);
  186. return coordSys === this ? this.dataToPoint(value) : null;
  187. };
  188. Single.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  189. var coordSys = getCoordSys(finder);
  190. return coordSys === this ? this.pointToData(pixel) : null;
  191. };
  192. return Single;
  193. }();
  194. function getCoordSys(finder) {
  195. var seriesModel = finder.seriesModel;
  196. var singleModel = finder.singleAxisModel;
  197. return singleModel && singleModel.coordinateSystem || seriesModel && seriesModel.coordinateSystem;
  198. }
  199. export default Single;