selector.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import * as polygonContain from 'zrender/lib/contain/polygon.js';
  41. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  42. import { linePolygonIntersect } from '../../util/graphic.js';
  43. export function makeBrushCommonSelectorForSeries(area) {
  44. var brushType = area.brushType; // Do not use function binding or curry for performance.
  45. var selectors = {
  46. point: function (itemLayout) {
  47. return selector[brushType].point(itemLayout, selectors, area);
  48. },
  49. rect: function (itemLayout) {
  50. return selector[brushType].rect(itemLayout, selectors, area);
  51. }
  52. };
  53. return selectors;
  54. }
  55. var selector = {
  56. lineX: getLineSelectors(0),
  57. lineY: getLineSelectors(1),
  58. rect: {
  59. point: function (itemLayout, selectors, area) {
  60. return itemLayout && area.boundingRect.contain(itemLayout[0], itemLayout[1]);
  61. },
  62. rect: function (itemLayout, selectors, area) {
  63. return itemLayout && area.boundingRect.intersect(itemLayout);
  64. }
  65. },
  66. polygon: {
  67. point: function (itemLayout, selectors, area) {
  68. return itemLayout && area.boundingRect.contain(itemLayout[0], itemLayout[1]) && polygonContain.contain(area.range, itemLayout[0], itemLayout[1]);
  69. },
  70. rect: function (itemLayout, selectors, area) {
  71. var points = area.range;
  72. if (!itemLayout || points.length <= 1) {
  73. return false;
  74. }
  75. var x = itemLayout.x;
  76. var y = itemLayout.y;
  77. var width = itemLayout.width;
  78. var height = itemLayout.height;
  79. var p = points[0];
  80. if (polygonContain.contain(points, x, y) || polygonContain.contain(points, x + width, y) || polygonContain.contain(points, x, y + height) || polygonContain.contain(points, x + width, y + height) || BoundingRect.create(itemLayout).contain(p[0], p[1]) || linePolygonIntersect(x, y, x + width, y, points) || linePolygonIntersect(x, y, x, y + height, points) || linePolygonIntersect(x + width, y, x + width, y + height, points) || linePolygonIntersect(x, y + height, x + width, y + height, points)) {
  81. return true;
  82. }
  83. }
  84. }
  85. };
  86. function getLineSelectors(xyIndex) {
  87. var xy = ['x', 'y'];
  88. var wh = ['width', 'height'];
  89. return {
  90. point: function (itemLayout, selectors, area) {
  91. if (itemLayout) {
  92. var range = area.range;
  93. var p = itemLayout[xyIndex];
  94. return inLineRange(p, range);
  95. }
  96. },
  97. rect: function (itemLayout, selectors, area) {
  98. if (itemLayout) {
  99. var range = area.range;
  100. var layoutRange = [itemLayout[xy[xyIndex]], itemLayout[xy[xyIndex]] + itemLayout[wh[xyIndex]]];
  101. layoutRange[1] < layoutRange[0] && layoutRange.reverse();
  102. return inLineRange(layoutRange[0], range) || inLineRange(layoutRange[1], range) || inLineRange(range[0], layoutRange) || inLineRange(range[1], layoutRange);
  103. }
  104. }
  105. };
  106. }
  107. function inLineRange(p, range) {
  108. return range[0] <= p && p <= range[1];
  109. }
  110. export default selector;