Single.js 6.9 KB

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