Single.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 SingleAxis = require("./SingleAxis");
  20. var axisHelper = require("../axisHelper");
  21. var _layout = require("../../util/layout");
  22. var getLayoutRect = _layout.getLayoutRect;
  23. var _util = require("zrender/lib/core/util");
  24. var each = _util.each;
  25. /*
  26. * Licensed to the Apache Software Foundation (ASF) under one
  27. * or more contributor license agreements. See the NOTICE file
  28. * distributed with this work for additional information
  29. * regarding copyright ownership. The ASF licenses this file
  30. * to you under the Apache License, Version 2.0 (the
  31. * "License"); you may not use this file except in compliance
  32. * with the License. You may obtain a copy of the License at
  33. *
  34. * http://www.apache.org/licenses/LICENSE-2.0
  35. *
  36. * Unless required by applicable law or agreed to in writing,
  37. * software distributed under the License is distributed on an
  38. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  39. * KIND, either express or implied. See the License for the
  40. * specific language governing permissions and limitations
  41. * under the License.
  42. */
  43. /**
  44. * Single coordinates system.
  45. */
  46. /**
  47. * Create a single coordinates system.
  48. *
  49. * @param {module:echarts/coord/single/AxisModel} axisModel
  50. * @param {module:echarts/model/Global} ecModel
  51. * @param {module:echarts/ExtensionAPI} api
  52. */
  53. function Single(axisModel, ecModel, api) {
  54. /**
  55. * @type {string}
  56. * @readOnly
  57. */
  58. this.dimension = 'single';
  59. /**
  60. * Add it just for draw tooltip.
  61. *
  62. * @type {Array.<string>}
  63. * @readOnly
  64. */
  65. this.dimensions = ['single'];
  66. /**
  67. * @private
  68. * @type {module:echarts/coord/single/SingleAxis}.
  69. */
  70. this._axis = null;
  71. /**
  72. * @private
  73. * @type {module:zrender/core/BoundingRect}
  74. */
  75. this._rect;
  76. this._init(axisModel, ecModel, api);
  77. /**
  78. * @type {module:echarts/coord/single/AxisModel}
  79. */
  80. this.model = axisModel;
  81. }
  82. Single.prototype = {
  83. type: 'singleAxis',
  84. axisPointerEnabled: true,
  85. constructor: Single,
  86. /**
  87. * Initialize single coordinate system.
  88. *
  89. * @param {module:echarts/coord/single/AxisModel} axisModel
  90. * @param {module:echarts/model/Global} ecModel
  91. * @param {module:echarts/ExtensionAPI} api
  92. * @private
  93. */
  94. _init: function (axisModel, ecModel, api) {
  95. var dim = this.dimension;
  96. var axis = new SingleAxis(dim, axisHelper.createScaleByModel(axisModel), [0, 0], axisModel.get('type'), axisModel.get('position'));
  97. var isCategory = axis.type === 'category';
  98. axis.onBand = isCategory && axisModel.get('boundaryGap');
  99. axis.inverse = axisModel.get('inverse');
  100. axis.orient = axisModel.get('orient');
  101. axisModel.axis = axis;
  102. axis.model = axisModel;
  103. axis.coordinateSystem = this;
  104. this._axis = axis;
  105. },
  106. /**
  107. * Update axis scale after data processed
  108. * @param {module:echarts/model/Global} ecModel
  109. * @param {module:echarts/ExtensionAPI} api
  110. */
  111. update: function (ecModel, api) {
  112. ecModel.eachSeries(function (seriesModel) {
  113. if (seriesModel.coordinateSystem === this) {
  114. var data = seriesModel.getData();
  115. each(data.mapDimension(this.dimension, true), function (dim) {
  116. this._axis.scale.unionExtentFromData(data, dim);
  117. }, this);
  118. axisHelper.niceScaleExtent(this._axis.scale, this._axis.model);
  119. }
  120. }, this);
  121. },
  122. /**
  123. * Resize the single coordinate system.
  124. *
  125. * @param {module:echarts/coord/single/AxisModel} axisModel
  126. * @param {module:echarts/ExtensionAPI} api
  127. */
  128. resize: function (axisModel, api) {
  129. this._rect = getLayoutRect({
  130. left: axisModel.get('left'),
  131. top: axisModel.get('top'),
  132. right: axisModel.get('right'),
  133. bottom: axisModel.get('bottom'),
  134. width: axisModel.get('width'),
  135. height: axisModel.get('height')
  136. }, {
  137. width: api.getWidth(),
  138. height: api.getHeight()
  139. });
  140. this._adjustAxis();
  141. },
  142. /**
  143. * @return {module:zrender/core/BoundingRect}
  144. */
  145. getRect: function () {
  146. return this._rect;
  147. },
  148. /**
  149. * @private
  150. */
  151. _adjustAxis: function () {
  152. var rect = this._rect;
  153. var axis = this._axis;
  154. var isHorizontal = axis.isHorizontal();
  155. var extent = isHorizontal ? [0, rect.width] : [0, rect.height];
  156. var idx = axis.reverse ? 1 : 0;
  157. axis.setExtent(extent[idx], extent[1 - idx]);
  158. this._updateAxisTransform(axis, isHorizontal ? rect.x : rect.y);
  159. },
  160. /**
  161. * @param {module:echarts/coord/single/SingleAxis} axis
  162. * @param {number} coordBase
  163. */
  164. _updateAxisTransform: function (axis, coordBase) {
  165. var axisExtent = axis.getExtent();
  166. var extentSum = axisExtent[0] + axisExtent[1];
  167. var isHorizontal = axis.isHorizontal();
  168. axis.toGlobalCoord = isHorizontal ? function (coord) {
  169. return coord + coordBase;
  170. } : function (coord) {
  171. return extentSum - coord + coordBase;
  172. };
  173. axis.toLocalCoord = isHorizontal ? function (coord) {
  174. return coord - coordBase;
  175. } : function (coord) {
  176. return extentSum - coord + coordBase;
  177. };
  178. },
  179. /**
  180. * Get axis.
  181. *
  182. * @return {module:echarts/coord/single/SingleAxis}
  183. */
  184. getAxis: function () {
  185. return this._axis;
  186. },
  187. /**
  188. * Get axis, add it just for draw tooltip.
  189. *
  190. * @return {[type]} [description]
  191. */
  192. getBaseAxis: function () {
  193. return this._axis;
  194. },
  195. /**
  196. * @return {Array.<module:echarts/coord/Axis>}
  197. */
  198. getAxes: function () {
  199. return [this._axis];
  200. },
  201. /**
  202. * @return {Object} {baseAxes: [], otherAxes: []}
  203. */
  204. getTooltipAxes: function () {
  205. return {
  206. baseAxes: [this.getAxis()]
  207. };
  208. },
  209. /**
  210. * If contain point.
  211. *
  212. * @param {Array.<number>} point
  213. * @return {boolean}
  214. */
  215. containPoint: function (point) {
  216. var rect = this.getRect();
  217. var axis = this.getAxis();
  218. var orient = axis.orient;
  219. if (orient === 'horizontal') {
  220. return axis.contain(axis.toLocalCoord(point[0])) && point[1] >= rect.y && point[1] <= rect.y + rect.height;
  221. } else {
  222. return axis.contain(axis.toLocalCoord(point[1])) && point[0] >= rect.y && point[0] <= rect.y + rect.height;
  223. }
  224. },
  225. /**
  226. * @param {Array.<number>} point
  227. * @return {Array.<number>}
  228. */
  229. pointToData: function (point) {
  230. var axis = this.getAxis();
  231. return [axis.coordToData(axis.toLocalCoord(point[axis.orient === 'horizontal' ? 0 : 1]))];
  232. },
  233. /**
  234. * Convert the series data to concrete point.
  235. *
  236. * @param {number|Array.<number>} val
  237. * @return {Array.<number>}
  238. */
  239. dataToPoint: function (val) {
  240. var axis = this.getAxis();
  241. var rect = this.getRect();
  242. var pt = [];
  243. var idx = axis.orient === 'horizontal' ? 0 : 1;
  244. if (val instanceof Array) {
  245. val = val[0];
  246. }
  247. pt[idx] = axis.toGlobalCoord(axis.dataToCoord(+val));
  248. pt[1 - idx] = idx === 0 ? rect.y + rect.height / 2 : rect.x + rect.width / 2;
  249. return pt;
  250. }
  251. };
  252. var _default = Single;
  253. module.exports = _default;