cartesianAxisHelper.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var zrUtil = require("zrender/lib/core/util");
  2. /**
  3. * @param {Object} opt {labelInside}
  4. * @return {Object} {
  5. * position, rotation, labelDirection, labelOffset,
  6. * tickDirection, labelRotate, labelInterval, z2
  7. * }
  8. */
  9. function layout(gridModel, axisModel, opt) {
  10. opt = opt || {};
  11. var grid = gridModel.coordinateSystem;
  12. var axis = axisModel.axis;
  13. var layout = {};
  14. var rawAxisPosition = axis.position;
  15. var axisPosition = axis.onZero ? 'onZero' : rawAxisPosition;
  16. var axisDim = axis.dim;
  17. var rect = grid.getRect();
  18. var rectBound = [rect.x, rect.x + rect.width, rect.y, rect.y + rect.height];
  19. var idx = {
  20. left: 0,
  21. right: 1,
  22. top: 0,
  23. bottom: 1,
  24. onZero: 2
  25. };
  26. var axisOffset = axisModel.get('offset') || 0;
  27. var posBound = axisDim === 'x' ? [rectBound[2] - axisOffset, rectBound[3] + axisOffset] : [rectBound[0] - axisOffset, rectBound[1] + axisOffset];
  28. if (axis.onZero) {
  29. var otherAxis = grid.getAxis(axisDim === 'x' ? 'y' : 'x', axis.onZeroAxisIndex);
  30. var onZeroCoord = otherAxis.toGlobalCoord(otherAxis.dataToCoord(0));
  31. posBound[idx['onZero']] = Math.max(Math.min(onZeroCoord, posBound[1]), posBound[0]);
  32. } // Axis position
  33. layout.position = [axisDim === 'y' ? posBound[idx[axisPosition]] : rectBound[0], axisDim === 'x' ? posBound[idx[axisPosition]] : rectBound[3]]; // Axis rotation
  34. layout.rotation = Math.PI / 2 * (axisDim === 'x' ? 0 : 1); // Tick and label direction, x y is axisDim
  35. var dirMap = {
  36. top: -1,
  37. bottom: 1,
  38. left: -1,
  39. right: 1
  40. };
  41. layout.labelDirection = layout.tickDirection = layout.nameDirection = dirMap[rawAxisPosition];
  42. layout.labelOffset = axis.onZero ? posBound[idx[rawAxisPosition]] - posBound[idx['onZero']] : 0;
  43. if (axisModel.get('axisTick.inside')) {
  44. layout.tickDirection = -layout.tickDirection;
  45. }
  46. if (zrUtil.retrieve(opt.labelInside, axisModel.get('axisLabel.inside'))) {
  47. layout.labelDirection = -layout.labelDirection;
  48. } // Special label rotation
  49. var labelRotate = axisModel.get('axisLabel.rotate');
  50. layout.labelRotate = axisPosition === 'top' ? -labelRotate : labelRotate; // label interval when auto mode.
  51. layout.labelInterval = axis.getLabelInterval(); // Over splitLine and splitArea
  52. layout.z2 = 1;
  53. return layout;
  54. }
  55. exports.layout = layout;