axis.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. "use strict";
  2. var Util = require('../../util/common');
  3. var Axis = require('../../component/axis/');
  4. var Global = require('../../global');
  5. var _require = require('../../graphic/index'),
  6. Shape = _require.Shape;
  7. function formatTicks(ticks) {
  8. var tmp = ticks.slice(0);
  9. if (tmp.length > 0) {
  10. var first = tmp[0];
  11. var last = tmp[tmp.length - 1];
  12. if (first.value !== 0) {
  13. tmp.unshift({
  14. value: 0
  15. });
  16. }
  17. if (last.value !== 1) {
  18. tmp.push({
  19. value: 1
  20. });
  21. }
  22. }
  23. return tmp;
  24. }
  25. var AxisController = /*#__PURE__*/function () {
  26. function AxisController(cfg) {
  27. this.axisCfg = {};
  28. this.frontPlot = null;
  29. this.backPlot = null;
  30. this.axes = {}; // store the axes's options
  31. Util.mix(this, cfg);
  32. }
  33. var _proto = AxisController.prototype;
  34. _proto._isHide = function _isHide(field) {
  35. var axisCfg = this.axisCfg;
  36. return !axisCfg || axisCfg[field] === false;
  37. };
  38. _proto._getLinePosition = function _getLinePosition(scale, dimType, index, transposed) {
  39. var position = '';
  40. var field = scale.field;
  41. var axisCfg = this.axisCfg;
  42. if (axisCfg[field] && axisCfg[field].position) {
  43. position = axisCfg[field].position;
  44. } else if (dimType === 'x') {
  45. position = transposed ? 'left' : 'bottom';
  46. } else if (dimType === 'y') {
  47. position = index ? 'right' : 'left';
  48. if (transposed) {
  49. position = 'bottom';
  50. }
  51. }
  52. return position;
  53. };
  54. _proto._getLineCfg = function _getLineCfg(coord, dimType, position) {
  55. var start;
  56. var end;
  57. var factor = 1; // Mark clockwise or counterclockwise
  58. if (dimType === 'x') {
  59. start = {
  60. x: 0,
  61. y: 0
  62. };
  63. end = {
  64. x: 1,
  65. y: 0
  66. };
  67. } else {
  68. if (position === 'right') {
  69. // there will be several y axes
  70. start = {
  71. x: 1,
  72. y: 0
  73. };
  74. end = {
  75. x: 1,
  76. y: 1
  77. };
  78. } else {
  79. start = {
  80. x: 0,
  81. y: 0
  82. };
  83. end = {
  84. x: 0,
  85. y: 1
  86. };
  87. factor = -1;
  88. }
  89. }
  90. if (coord.transposed) {
  91. factor *= -1;
  92. }
  93. return {
  94. offsetFactor: factor,
  95. start: coord.convertPoint(start),
  96. end: coord.convertPoint(end)
  97. };
  98. };
  99. _proto._getCircleCfg = function _getCircleCfg(coord) {
  100. return {
  101. startAngle: coord.startAngle,
  102. endAngle: coord.endAngle,
  103. center: coord.center,
  104. radius: coord.circleRadius
  105. };
  106. };
  107. _proto._getRadiusCfg = function _getRadiusCfg(coord) {
  108. var transposed = coord.transposed;
  109. var start;
  110. var end;
  111. if (transposed) {
  112. start = {
  113. x: 0,
  114. y: 0
  115. };
  116. end = {
  117. x: 1,
  118. y: 0
  119. };
  120. } else {
  121. start = {
  122. x: 0,
  123. y: 0
  124. };
  125. end = {
  126. x: 0,
  127. y: 1
  128. };
  129. }
  130. return {
  131. offsetFactor: -1,
  132. start: coord.convertPoint(start),
  133. end: coord.convertPoint(end)
  134. };
  135. };
  136. _proto._getAxisCfg = function _getAxisCfg(coord, scale, verticalScale, dimType, defaultCfg) {
  137. var self = this;
  138. var axisCfg = this.axisCfg;
  139. var ticks = scale.getTicks();
  140. var cfg = Util.deepMix({
  141. ticks: ticks,
  142. frontContainer: this.frontPlot,
  143. backContainer: this.backPlot
  144. }, defaultCfg, axisCfg[scale.field]);
  145. var labels = [];
  146. var label = cfg.label;
  147. var count = ticks.length;
  148. var maxWidth = 0;
  149. var maxHeight = 0;
  150. var labelCfg = label;
  151. Util.each(ticks, function (tick, index) {
  152. if (Util.isFunction(label)) {
  153. var executedLabel = label(tick.text, index, count);
  154. labelCfg = executedLabel ? Util.mix({}, Global._defaultAxis.label, executedLabel) : null;
  155. }
  156. if (labelCfg) {
  157. var textStyle = {};
  158. if (labelCfg.textAlign) {
  159. textStyle.textAlign = labelCfg.textAlign;
  160. }
  161. if (labelCfg.textBaseline) {
  162. textStyle.textBaseline = labelCfg.textBaseline;
  163. }
  164. var axisLabel = new Shape.Text({
  165. className: 'axis-label',
  166. attrs: Util.mix({
  167. x: 0,
  168. y: 0,
  169. text: tick.text,
  170. fontFamily: self.chart.get('canvas').get('fontFamily')
  171. }, labelCfg),
  172. value: tick.value,
  173. textStyle: textStyle,
  174. top: labelCfg.top,
  175. context: self.chart.get('canvas').get('context')
  176. });
  177. labels.push(axisLabel);
  178. var _axisLabel$getBBox = axisLabel.getBBox(),
  179. width = _axisLabel$getBBox.width,
  180. height = _axisLabel$getBBox.height;
  181. maxWidth = Math.max(maxWidth, width);
  182. maxHeight = Math.max(maxHeight, height);
  183. }
  184. });
  185. cfg.labels = labels;
  186. cfg.maxWidth = maxWidth;
  187. cfg.maxHeight = maxHeight;
  188. return cfg;
  189. };
  190. _proto._createAxis = function _createAxis(coord, scale, verticalScale, dimType, index) {
  191. if (index === void 0) {
  192. index = '';
  193. }
  194. var self = this;
  195. var coordType = coord.type;
  196. var transposed = coord.transposed;
  197. var type;
  198. var key;
  199. var defaultCfg;
  200. if (coordType === 'cartesian' || coordType === 'rect') {
  201. var position = self._getLinePosition(scale, dimType, index, transposed);
  202. defaultCfg = Global.axis[position];
  203. defaultCfg.position = position;
  204. type = 'Line';
  205. key = position;
  206. } else {
  207. if (dimType === 'x' && !transposed || dimType === 'y' && transposed) {
  208. defaultCfg = Global.axis.circle;
  209. type = 'Circle';
  210. key = 'circle';
  211. } else {
  212. defaultCfg = Global.axis.radius;
  213. type = 'Line';
  214. key = 'radius';
  215. }
  216. }
  217. var cfg = self._getAxisCfg(coord, scale, verticalScale, dimType, defaultCfg);
  218. cfg.type = type;
  219. cfg.dimType = dimType;
  220. cfg.verticalScale = verticalScale;
  221. cfg.index = index;
  222. this.axes[key] = cfg;
  223. };
  224. _proto.createAxis = function createAxis(coord, xScale, yScales) {
  225. var self = this;
  226. if (xScale && !self._isHide(xScale.field)) {
  227. self._createAxis(coord, xScale, yScales[0], 'x');
  228. }
  229. Util.each(yScales, function (yScale, index) {
  230. if (!self._isHide(yScale.field)) {
  231. self._createAxis(coord, yScale, xScale, 'y', index);
  232. }
  233. });
  234. var axes = this.axes;
  235. var chart = self.chart;
  236. if (chart._isAutoPadding()) {
  237. var userPadding = Util.parsePadding(chart.get('padding'));
  238. var appendPadding = Util.parsePadding(chart.get('appendPadding'));
  239. var legendRange = chart.get('legendRange') || {
  240. top: 0,
  241. right: 0,
  242. bottom: 0,
  243. left: 0
  244. };
  245. var padding = [userPadding[0] === 'auto' ? legendRange.top + appendPadding[0] * 2 : userPadding[0], userPadding[1] === 'auto' ? legendRange.right + appendPadding[1] : userPadding[1], userPadding[2] === 'auto' ? legendRange.bottom + appendPadding[2] : userPadding[2], userPadding[3] === 'auto' ? legendRange.left + appendPadding[3] : userPadding[3]];
  246. if (coord.isPolar) {
  247. var circleAxis = axes.circle;
  248. if (circleAxis) {
  249. var maxHeight = circleAxis.maxHeight,
  250. maxWidth = circleAxis.maxWidth,
  251. labelOffset = circleAxis.labelOffset;
  252. padding[0] += maxHeight + labelOffset;
  253. padding[1] += maxWidth + labelOffset;
  254. padding[2] += maxHeight + labelOffset;
  255. padding[3] += maxWidth + labelOffset;
  256. }
  257. } else {
  258. if (axes.right && userPadding[1] === 'auto') {
  259. var _axes$right = axes.right,
  260. _maxWidth = _axes$right.maxWidth,
  261. _labelOffset = _axes$right.labelOffset;
  262. padding[1] += _maxWidth + _labelOffset;
  263. }
  264. if (axes.left && userPadding[3] === 'auto') {
  265. var _axes$left = axes.left,
  266. _maxWidth2 = _axes$left.maxWidth,
  267. _labelOffset2 = _axes$left.labelOffset;
  268. padding[3] += _maxWidth2 + _labelOffset2;
  269. }
  270. if (axes.bottom && userPadding[2] === 'auto') {
  271. var _axes$bottom = axes.bottom,
  272. _maxHeight = _axes$bottom.maxHeight,
  273. _labelOffset3 = _axes$bottom.labelOffset;
  274. padding[2] += _maxHeight + _labelOffset3;
  275. }
  276. }
  277. chart.set('_padding', padding);
  278. chart._updateLayout(padding);
  279. }
  280. Util.each(axes, function (axis) {
  281. var type = axis.type,
  282. grid = axis.grid,
  283. verticalScale = axis.verticalScale,
  284. ticks = axis.ticks,
  285. dimType = axis.dimType,
  286. position = axis.position,
  287. index = axis.index;
  288. var appendCfg;
  289. if (coord.isPolar) {
  290. if (type === 'Line') {
  291. appendCfg = self._getRadiusCfg(coord);
  292. } else if (type === 'Circle') {
  293. appendCfg = self._getCircleCfg(coord);
  294. }
  295. } else {
  296. appendCfg = self._getLineCfg(coord, dimType, position);
  297. }
  298. if (grid && verticalScale) {
  299. var gridPoints = [];
  300. var verticalTicks = formatTicks(verticalScale.getTicks());
  301. Util.each(ticks, function (tick) {
  302. var subPoints = [];
  303. Util.each(verticalTicks, function (verticalTick) {
  304. var x = dimType === 'x' ? tick.value : verticalTick.value;
  305. var y = dimType === 'x' ? verticalTick.value : tick.value;
  306. if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
  307. var point = coord.convertPoint({
  308. x: x,
  309. y: y
  310. });
  311. subPoints.push(point);
  312. }
  313. });
  314. gridPoints.push({
  315. points: subPoints,
  316. _id: 'axis-' + dimType + index + '-grid-' + tick.tickValue
  317. });
  318. });
  319. axis.gridPoints = gridPoints;
  320. if (coord.isPolar) {
  321. axis.center = coord.center;
  322. axis.startAngle = coord.startAngle;
  323. axis.endAngle = coord.endAngle;
  324. }
  325. }
  326. appendCfg._id = 'axis-' + dimType;
  327. if (!Util.isNil(index)) {
  328. appendCfg._id = 'axis-' + dimType + index;
  329. }
  330. new Axis[type](Util.mix(axis, appendCfg));
  331. });
  332. };
  333. _proto.clear = function clear() {
  334. this.axes = {};
  335. this.frontPlot.clear();
  336. this.backPlot.clear();
  337. };
  338. return AxisController;
  339. }();
  340. module.exports = AxisController;