SingleAxisPointer.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 BaseAxisPointer = require("./BaseAxisPointer");
  20. var viewHelper = require("./viewHelper");
  21. var singleAxisHelper = require("../../coord/single/singleAxisHelper");
  22. var AxisView = require("../axis/AxisView");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. var XY = ['x', 'y'];
  42. var WH = ['width', 'height'];
  43. var SingleAxisPointer = BaseAxisPointer.extend({
  44. /**
  45. * @override
  46. */
  47. makeElOption: function (elOption, value, axisModel, axisPointerModel, api) {
  48. var axis = axisModel.axis;
  49. var coordSys = axis.coordinateSystem;
  50. var otherExtent = getGlobalExtent(coordSys, 1 - getPointDimIndex(axis));
  51. var pixelValue = coordSys.dataToPoint(value)[0];
  52. var axisPointerType = axisPointerModel.get('type');
  53. if (axisPointerType && axisPointerType !== 'none') {
  54. var elStyle = viewHelper.buildElStyle(axisPointerModel);
  55. var pointerOption = pointerShapeBuilder[axisPointerType](axis, pixelValue, otherExtent);
  56. pointerOption.style = elStyle;
  57. elOption.graphicKey = pointerOption.type;
  58. elOption.pointer = pointerOption;
  59. }
  60. var layoutInfo = singleAxisHelper.layout(axisModel);
  61. viewHelper.buildCartesianSingleLabelElOption(value, elOption, layoutInfo, axisModel, axisPointerModel, api);
  62. },
  63. /**
  64. * @override
  65. */
  66. getHandleTransform: function (value, axisModel, axisPointerModel) {
  67. var layoutInfo = singleAxisHelper.layout(axisModel, {
  68. labelInside: false
  69. });
  70. layoutInfo.labelMargin = axisPointerModel.get('handle.margin');
  71. return {
  72. position: viewHelper.getTransformedPosition(axisModel.axis, value, layoutInfo),
  73. rotation: layoutInfo.rotation + (layoutInfo.labelDirection < 0 ? Math.PI : 0)
  74. };
  75. },
  76. /**
  77. * @override
  78. */
  79. updateHandleTransform: function (transform, delta, axisModel, axisPointerModel) {
  80. var axis = axisModel.axis;
  81. var coordSys = axis.coordinateSystem;
  82. var dimIndex = getPointDimIndex(axis);
  83. var axisExtent = getGlobalExtent(coordSys, dimIndex);
  84. var currPosition = transform.position;
  85. currPosition[dimIndex] += delta[dimIndex];
  86. currPosition[dimIndex] = Math.min(axisExtent[1], currPosition[dimIndex]);
  87. currPosition[dimIndex] = Math.max(axisExtent[0], currPosition[dimIndex]);
  88. var otherExtent = getGlobalExtent(coordSys, 1 - dimIndex);
  89. var cursorOtherValue = (otherExtent[1] + otherExtent[0]) / 2;
  90. var cursorPoint = [cursorOtherValue, cursorOtherValue];
  91. cursorPoint[dimIndex] = currPosition[dimIndex];
  92. return {
  93. position: currPosition,
  94. rotation: transform.rotation,
  95. cursorPoint: cursorPoint,
  96. tooltipOption: {
  97. verticalAlign: 'middle'
  98. }
  99. };
  100. }
  101. });
  102. var pointerShapeBuilder = {
  103. line: function (axis, pixelValue, otherExtent) {
  104. var targetShape = viewHelper.makeLineShape([pixelValue, otherExtent[0]], [pixelValue, otherExtent[1]], getPointDimIndex(axis));
  105. return {
  106. type: 'Line',
  107. subPixelOptimize: true,
  108. shape: targetShape
  109. };
  110. },
  111. shadow: function (axis, pixelValue, otherExtent) {
  112. var bandWidth = axis.getBandWidth();
  113. var span = otherExtent[1] - otherExtent[0];
  114. return {
  115. type: 'Rect',
  116. shape: viewHelper.makeRectShape([pixelValue - bandWidth / 2, otherExtent[0]], [bandWidth, span], getPointDimIndex(axis))
  117. };
  118. }
  119. };
  120. function getPointDimIndex(axis) {
  121. return axis.isHorizontal() ? 0 : 1;
  122. }
  123. function getGlobalExtent(coordSys, dimIndex) {
  124. var rect = coordSys.getRect();
  125. return [rect[XY[dimIndex]], rect[XY[dimIndex]] + rect[WH[dimIndex]]];
  126. }
  127. AxisView.registerAxisPointerClass('SingleAxisPointer', SingleAxisPointer);
  128. var _default = SingleAxisPointer;
  129. module.exports = _default;