selector.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 polygonContain = require("zrender/lib/contain/polygon");
  20. var BoundingRect = require("zrender/lib/core/BoundingRect");
  21. var _graphic = require("../../util/graphic");
  22. var linePolygonIntersect = _graphic.linePolygonIntersect;
  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. // Key of the first level is brushType: `line`, `rect`, `polygon`.
  42. // Key of the second level is chart element type: `point`, `rect`.
  43. // See moudule:echarts/component/helper/BrushController
  44. // function param:
  45. // {Object} itemLayout fetch from data.getItemLayout(dataIndex)
  46. // {Object} selectors {point: selector, rect: selector, ...}
  47. // {Object} area {range: [[], [], ..], boudingRect}
  48. // function return:
  49. // {boolean} Whether in the given brush.
  50. var selector = {
  51. lineX: getLineSelectors(0),
  52. lineY: getLineSelectors(1),
  53. rect: {
  54. point: function (itemLayout, selectors, area) {
  55. return itemLayout && area.boundingRect.contain(itemLayout[0], itemLayout[1]);
  56. },
  57. rect: function (itemLayout, selectors, area) {
  58. return itemLayout && area.boundingRect.intersect(itemLayout);
  59. }
  60. },
  61. polygon: {
  62. point: function (itemLayout, selectors, area) {
  63. return itemLayout && area.boundingRect.contain(itemLayout[0], itemLayout[1]) && polygonContain.contain(area.range, itemLayout[0], itemLayout[1]);
  64. },
  65. rect: function (itemLayout, selectors, area) {
  66. var points = area.range;
  67. if (!itemLayout || points.length <= 1) {
  68. return false;
  69. }
  70. var x = itemLayout.x;
  71. var y = itemLayout.y;
  72. var width = itemLayout.width;
  73. var height = itemLayout.height;
  74. var p = points[0];
  75. 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)) {
  76. return true;
  77. }
  78. }
  79. }
  80. };
  81. function getLineSelectors(xyIndex) {
  82. var xy = ['x', 'y'];
  83. var wh = ['width', 'height'];
  84. return {
  85. point: function (itemLayout, selectors, area) {
  86. if (itemLayout) {
  87. var range = area.range;
  88. var p = itemLayout[xyIndex];
  89. return inLineRange(p, range);
  90. }
  91. },
  92. rect: function (itemLayout, selectors, area) {
  93. if (itemLayout) {
  94. var range = area.range;
  95. var layoutRange = [itemLayout[xy[xyIndex]], itemLayout[xy[xyIndex]] + itemLayout[wh[xyIndex]]];
  96. layoutRange[1] < layoutRange[0] && layoutRange.reverse();
  97. return inLineRange(layoutRange[0], range) || inLineRange(layoutRange[1], range) || inLineRange(range[0], layoutRange) || inLineRange(range[1], layoutRange);
  98. }
  99. }
  100. };
  101. }
  102. function inLineRange(p, range) {
  103. return range[0] <= p && p <= range[1];
  104. }
  105. var _default = selector;
  106. module.exports = _default;