SingleAxisPointer.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var graphic = require("../../util/graphic");
  2. var BaseAxisPointer = require("./BaseAxisPointer");
  3. var viewHelper = require("./viewHelper");
  4. var singleAxisHelper = require("../axis/singleAxisHelper");
  5. var AxisView = require("../axis/AxisView");
  6. var XY = ['x', 'y'];
  7. var WH = ['width', 'height'];
  8. var SingleAxisPointer = BaseAxisPointer.extend({
  9. /**
  10. * @override
  11. */
  12. makeElOption: function (elOption, value, axisModel, axisPointerModel, api) {
  13. var axis = axisModel.axis;
  14. var coordSys = axis.coordinateSystem;
  15. var otherExtent = getGlobalExtent(coordSys, 1 - getPointDimIndex(axis));
  16. var pixelValue = coordSys.dataToPoint(value)[0];
  17. var axisPointerType = axisPointerModel.get('type');
  18. if (axisPointerType && axisPointerType !== 'none') {
  19. var elStyle = viewHelper.buildElStyle(axisPointerModel);
  20. var pointerOption = pointerShapeBuilder[axisPointerType](axis, pixelValue, otherExtent, elStyle);
  21. pointerOption.style = elStyle;
  22. elOption.graphicKey = pointerOption.type;
  23. elOption.pointer = pointerOption;
  24. }
  25. var layoutInfo = singleAxisHelper.layout(axisModel);
  26. viewHelper.buildCartesianSingleLabelElOption(value, elOption, layoutInfo, axisModel, axisPointerModel, api);
  27. },
  28. /**
  29. * @override
  30. */
  31. getHandleTransform: function (value, axisModel, axisPointerModel) {
  32. var layoutInfo = singleAxisHelper.layout(axisModel, {
  33. labelInside: false
  34. });
  35. layoutInfo.labelMargin = axisPointerModel.get('handle.margin');
  36. return {
  37. position: viewHelper.getTransformedPosition(axisModel.axis, value, layoutInfo),
  38. rotation: layoutInfo.rotation + (layoutInfo.labelDirection < 0 ? Math.PI : 0)
  39. };
  40. },
  41. /**
  42. * @override
  43. */
  44. updateHandleTransform: function (transform, delta, axisModel, axisPointerModel) {
  45. var axis = axisModel.axis;
  46. var coordSys = axis.coordinateSystem;
  47. var dimIndex = getPointDimIndex(axis);
  48. var axisExtent = getGlobalExtent(coordSys, dimIndex);
  49. var currPosition = transform.position;
  50. currPosition[dimIndex] += delta[dimIndex];
  51. currPosition[dimIndex] = Math.min(axisExtent[1], currPosition[dimIndex]);
  52. currPosition[dimIndex] = Math.max(axisExtent[0], currPosition[dimIndex]);
  53. var otherExtent = getGlobalExtent(coordSys, 1 - dimIndex);
  54. var cursorOtherValue = (otherExtent[1] + otherExtent[0]) / 2;
  55. var cursorPoint = [cursorOtherValue, cursorOtherValue];
  56. cursorPoint[dimIndex] = currPosition[dimIndex];
  57. return {
  58. position: currPosition,
  59. rotation: transform.rotation,
  60. cursorPoint: cursorPoint,
  61. tooltipOption: {
  62. verticalAlign: 'middle'
  63. }
  64. };
  65. }
  66. });
  67. var pointerShapeBuilder = {
  68. line: function (axis, pixelValue, otherExtent, elStyle) {
  69. var targetShape = viewHelper.makeLineShape([pixelValue, otherExtent[0]], [pixelValue, otherExtent[1]], getPointDimIndex(axis));
  70. graphic.subPixelOptimizeLine({
  71. shape: targetShape,
  72. style: elStyle
  73. });
  74. return {
  75. type: 'Line',
  76. shape: targetShape
  77. };
  78. },
  79. shadow: function (axis, pixelValue, otherExtent, elStyle) {
  80. var bandWidth = axis.getBandWidth();
  81. var span = otherExtent[1] - otherExtent[0];
  82. return {
  83. type: 'Rect',
  84. shape: viewHelper.makeRectShape([pixelValue - bandWidth / 2, otherExtent[0]], [bandWidth, span], getPointDimIndex(axis))
  85. };
  86. }
  87. };
  88. function getPointDimIndex(axis) {
  89. return axis.isHorizontal() ? 0 : 1;
  90. }
  91. function getGlobalExtent(coordSys, dimIndex) {
  92. var rect = coordSys.getRect();
  93. return [rect[XY[dimIndex]], rect[XY[dimIndex]] + rect[WH[dimIndex]]];
  94. }
  95. AxisView.registerAxisPointerClass('SingleAxisPointer', SingleAxisPointer);
  96. var _default = SingleAxisPointer;
  97. module.exports = _default;