selector.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var polygonContain = require("zrender/lib/contain/polygon");
  2. var BoundingRect = require("zrender/lib/core/BoundingRect");
  3. // Key of the first level is brushType: `line`, `rect`, `polygon`.
  4. // Key of the second level is chart element type: `point`, `rect`.
  5. // See moudule:echarts/component/helper/BrushController
  6. // function param:
  7. // {Object} itemLayout fetch from data.getItemLayout(dataIndex)
  8. // {Object} selectors {point: selector, rect: selector, ...}
  9. // {Object} area {range: [[], [], ..], boudingRect}
  10. // function return:
  11. // {boolean} Whether in the given brush.
  12. var selector = {
  13. lineX: getLineSelectors(0),
  14. lineY: getLineSelectors(1),
  15. rect: {
  16. point: function (itemLayout, selectors, area) {
  17. return itemLayout && area.boundingRect.contain(itemLayout[0], itemLayout[1]);
  18. },
  19. rect: function (itemLayout, selectors, area) {
  20. return itemLayout && area.boundingRect.intersect(itemLayout);
  21. }
  22. },
  23. polygon: {
  24. point: function (itemLayout, selectors, area) {
  25. return itemLayout && area.boundingRect.contain(itemLayout[0], itemLayout[1]) && polygonContain.contain(area.range, itemLayout[0], itemLayout[1]);
  26. },
  27. rect: function (itemLayout, selectors, area) {
  28. var points = area.range;
  29. if (!itemLayout || points.length <= 1) {
  30. return false;
  31. }
  32. var x = itemLayout.x;
  33. var y = itemLayout.y;
  34. var width = itemLayout.width;
  35. var height = itemLayout.height;
  36. var p = points[0];
  37. 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]) || lineIntersectPolygon(x, y, x + width, y, points) || lineIntersectPolygon(x, y, x, y + height, points) || lineIntersectPolygon(x + width, y, x + width, y + height, points) || lineIntersectPolygon(x, y + height, x + width, y + height, points)) {
  38. return true;
  39. }
  40. }
  41. }
  42. };
  43. function getLineSelectors(xyIndex) {
  44. var xy = ['x', 'y'];
  45. var wh = ['width', 'height'];
  46. return {
  47. point: function (itemLayout, selectors, area) {
  48. if (itemLayout) {
  49. var range = area.range;
  50. var p = itemLayout[xyIndex];
  51. return inLineRange(p, range);
  52. }
  53. },
  54. rect: function (itemLayout, selectors, area) {
  55. if (itemLayout) {
  56. var range = area.range;
  57. var layoutRange = [itemLayout[xy[xyIndex]], itemLayout[xy[xyIndex]] + itemLayout[wh[xyIndex]]];
  58. layoutRange[1] < layoutRange[0] && layoutRange.reverse();
  59. return inLineRange(layoutRange[0], range) || inLineRange(layoutRange[1], range) || inLineRange(range[0], layoutRange) || inLineRange(range[1], layoutRange);
  60. }
  61. }
  62. };
  63. }
  64. function inLineRange(p, range) {
  65. return range[0] <= p && p <= range[1];
  66. }
  67. function lineIntersectPolygon(lx, ly, l2x, l2y, points) {
  68. for (var i = 0, p2 = points[points.length - 1]; i < points.length; i++) {
  69. var p = points[i];
  70. if (lineIntersect(lx, ly, l2x, l2y, p[0], p[1], p2[0], p2[1])) {
  71. return true;
  72. }
  73. p2 = p;
  74. }
  75. } // Code from <http://blog.csdn.net/rickliuxiao/article/details/6259322> with some fix.
  76. // See <https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection>
  77. function lineIntersect(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y) {
  78. var delta = determinant(a2x - a1x, b1x - b2x, a2y - a1y, b1y - b2y);
  79. if (nearZero(delta)) {
  80. // parallel
  81. return false;
  82. }
  83. var namenda = determinant(b1x - a1x, b1x - b2x, b1y - a1y, b1y - b2y) / delta;
  84. if (namenda < 0 || namenda > 1) {
  85. return false;
  86. }
  87. var miu = determinant(a2x - a1x, b1x - a1x, a2y - a1y, b1y - a1y) / delta;
  88. if (miu < 0 || miu > 1) {
  89. return false;
  90. }
  91. return true;
  92. }
  93. function nearZero(val) {
  94. return val <= 1e-6 && val >= -1e-6;
  95. }
  96. function determinant(v1, v2, v3, v4) {
  97. return v1 * v4 - v2 * v3;
  98. }
  99. var _default = selector;
  100. module.exports = _default;