axis.js 11 KB

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