Single.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 { 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. this._rect = getLayoutRect({
  97. left: axisModel.get('left'),
  98. top: axisModel.get('top'),
  99. right: axisModel.get('right'),
  100. bottom: axisModel.get('bottom'),
  101. width: axisModel.get('width'),
  102. height: axisModel.get('height')
  103. }, {
  104. width: api.getWidth(),
  105. height: api.getHeight()
  106. });
  107. this._adjustAxis();
  108. };
  109. Single.prototype.getRect = function () {
  110. return this._rect;
  111. };
  112. Single.prototype._adjustAxis = function () {
  113. var rect = this._rect;
  114. var axis = this._axis;
  115. var isHorizontal = axis.isHorizontal();
  116. var extent = isHorizontal ? [0, rect.width] : [0, rect.height];
  117. var idx = axis.inverse ? 1 : 0;
  118. axis.setExtent(extent[idx], extent[1 - idx]);
  119. this._updateAxisTransform(axis, isHorizontal ? rect.x : rect.y);
  120. };
  121. Single.prototype._updateAxisTransform = function (axis, coordBase) {
  122. var axisExtent = axis.getExtent();
  123. var extentSum = axisExtent[0] + axisExtent[1];
  124. var isHorizontal = axis.isHorizontal();
  125. axis.toGlobalCoord = isHorizontal ? function (coord) {
  126. return coord + coordBase;
  127. } : function (coord) {
  128. return extentSum - coord + coordBase;
  129. };
  130. axis.toLocalCoord = isHorizontal ? function (coord) {
  131. return coord - coordBase;
  132. } : function (coord) {
  133. return extentSum - coord + coordBase;
  134. };
  135. };
  136. /**
  137. * Get axis.
  138. */
  139. Single.prototype.getAxis = function () {
  140. return this._axis;
  141. };
  142. /**
  143. * Get axis, add it just for draw tooltip.
  144. */
  145. Single.prototype.getBaseAxis = function () {
  146. return this._axis;
  147. };
  148. Single.prototype.getAxes = function () {
  149. return [this._axis];
  150. };
  151. Single.prototype.getTooltipAxes = function () {
  152. return {
  153. baseAxes: [this.getAxis()],
  154. // Empty otherAxes
  155. otherAxes: []
  156. };
  157. };
  158. /**
  159. * If contain point.
  160. */
  161. Single.prototype.containPoint = function (point) {
  162. var rect = this.getRect();
  163. var axis = this.getAxis();
  164. var orient = axis.orient;
  165. if (orient === 'horizontal') {
  166. return axis.contain(axis.toLocalCoord(point[0])) && point[1] >= rect.y && point[1] <= rect.y + rect.height;
  167. } else {
  168. return axis.contain(axis.toLocalCoord(point[1])) && point[0] >= rect.y && point[0] <= rect.y + rect.height;
  169. }
  170. };
  171. Single.prototype.pointToData = function (point) {
  172. var axis = this.getAxis();
  173. return [axis.coordToData(axis.toLocalCoord(point[axis.orient === 'horizontal' ? 0 : 1]))];
  174. };
  175. /**
  176. * Convert the series data to concrete point.
  177. * Can be [val] | val
  178. */
  179. Single.prototype.dataToPoint = function (val) {
  180. var axis = this.getAxis();
  181. var rect = this.getRect();
  182. var pt = [];
  183. var idx = axis.orient === 'horizontal' ? 0 : 1;
  184. if (val instanceof Array) {
  185. val = val[0];
  186. }
  187. pt[idx] = axis.toGlobalCoord(axis.dataToCoord(+val));
  188. pt[1 - idx] = idx === 0 ? rect.y + rect.height / 2 : rect.x + rect.width / 2;
  189. return pt;
  190. };
  191. Single.prototype.convertToPixel = function (ecModel, finder, value) {
  192. var coordSys = getCoordSys(finder);
  193. return coordSys === this ? this.dataToPoint(value) : null;
  194. };
  195. Single.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  196. var coordSys = getCoordSys(finder);
  197. return coordSys === this ? this.pointToData(pixel) : null;
  198. };
  199. return Single;
  200. }();
  201. function getCoordSys(finder) {
  202. var seriesModel = finder.seriesModel;
  203. var singleModel = finder.singleAxisModel;
  204. return singleModel && singleModel.coordinateSystem || seriesModel && seriesModel.coordinateSystem;
  205. }
  206. export default Single;