CartesianAxisPointer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 cartesianAxisHelper = require("../../coord/cartesian/cartesianAxisHelper");
  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 CartesianAxisPointer = BaseAxisPointer.extend({
  42. /**
  43. * @override
  44. */
  45. makeElOption: function (elOption, value, axisModel, axisPointerModel, api) {
  46. var axis = axisModel.axis;
  47. var grid = axis.grid;
  48. var axisPointerType = axisPointerModel.get('type');
  49. var otherExtent = getCartesian(grid, axis).getOtherAxis(axis).getGlobalExtent();
  50. var pixelValue = axis.toGlobalCoord(axis.dataToCoord(value, true));
  51. if (axisPointerType && axisPointerType !== 'none') {
  52. var elStyle = viewHelper.buildElStyle(axisPointerModel);
  53. var pointerOption = pointerShapeBuilder[axisPointerType](axis, pixelValue, otherExtent);
  54. pointerOption.style = elStyle;
  55. elOption.graphicKey = pointerOption.type;
  56. elOption.pointer = pointerOption;
  57. }
  58. var layoutInfo = cartesianAxisHelper.layout(grid.model, axisModel);
  59. viewHelper.buildCartesianSingleLabelElOption(value, elOption, layoutInfo, axisModel, axisPointerModel, api);
  60. },
  61. /**
  62. * @override
  63. */
  64. getHandleTransform: function (value, axisModel, axisPointerModel) {
  65. var layoutInfo = cartesianAxisHelper.layout(axisModel.axis.grid.model, axisModel, {
  66. labelInside: false
  67. });
  68. layoutInfo.labelMargin = axisPointerModel.get('handle.margin');
  69. return {
  70. position: viewHelper.getTransformedPosition(axisModel.axis, value, layoutInfo),
  71. rotation: layoutInfo.rotation + (layoutInfo.labelDirection < 0 ? Math.PI : 0)
  72. };
  73. },
  74. /**
  75. * @override
  76. */
  77. updateHandleTransform: function (transform, delta, axisModel, axisPointerModel) {
  78. var axis = axisModel.axis;
  79. var grid = axis.grid;
  80. var axisExtent = axis.getGlobalExtent(true);
  81. var otherExtent = getCartesian(grid, axis).getOtherAxis(axis).getGlobalExtent();
  82. var dimIndex = axis.dim === 'x' ? 0 : 1;
  83. var currPosition = transform.position;
  84. currPosition[dimIndex] += delta[dimIndex];
  85. currPosition[dimIndex] = Math.min(axisExtent[1], currPosition[dimIndex]);
  86. currPosition[dimIndex] = Math.max(axisExtent[0], currPosition[dimIndex]);
  87. var cursorOtherValue = (otherExtent[1] + otherExtent[0]) / 2;
  88. var cursorPoint = [cursorOtherValue, cursorOtherValue];
  89. cursorPoint[dimIndex] = currPosition[dimIndex]; // Make tooltip do not overlap axisPointer and in the middle of the grid.
  90. var tooltipOptions = [{
  91. verticalAlign: 'middle'
  92. }, {
  93. align: 'center'
  94. }];
  95. return {
  96. position: currPosition,
  97. rotation: transform.rotation,
  98. cursorPoint: cursorPoint,
  99. tooltipOption: tooltipOptions[dimIndex]
  100. };
  101. }
  102. });
  103. function getCartesian(grid, axis) {
  104. var opt = {};
  105. opt[axis.dim + 'AxisIndex'] = axis.index;
  106. return grid.getCartesian(opt);
  107. }
  108. var pointerShapeBuilder = {
  109. line: function (axis, pixelValue, otherExtent) {
  110. var targetShape = viewHelper.makeLineShape([pixelValue, otherExtent[0]], [pixelValue, otherExtent[1]], getAxisDimIndex(axis));
  111. return {
  112. type: 'Line',
  113. subPixelOptimize: true,
  114. shape: targetShape
  115. };
  116. },
  117. shadow: function (axis, pixelValue, otherExtent) {
  118. var bandWidth = Math.max(1, axis.getBandWidth());
  119. var span = otherExtent[1] - otherExtent[0];
  120. return {
  121. type: 'Rect',
  122. shape: viewHelper.makeRectShape([pixelValue - bandWidth / 2, otherExtent[0]], [bandWidth, span], getAxisDimIndex(axis))
  123. };
  124. }
  125. };
  126. function getAxisDimIndex(axis) {
  127. return axis.dim === 'x' ? 0 : 1;
  128. }
  129. AxisView.registerAxisPointerClass('CartesianAxisPointer', CartesianAxisPointer);
  130. var _default = CartesianAxisPointer;
  131. module.exports = _default;