@antv/g2plot 2.3.31 → 2.3.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (298) hide show
  1. package/CHANGELOG.md +1270 -0
  2. package/{README.zh-CN.md → README.en-US.md} +33 -42
  3. package/README.md +43 -32
  4. package/dist/g2plot.min.js +3 -18
  5. package/dist/g2plot.min.js.map +1 -1
  6. package/esm/adaptor/common.d.ts +1 -0
  7. package/esm/adaptor/common.js +1 -0
  8. package/esm/adaptor/common.js.map +1 -1
  9. package/esm/adaptor/pattern.d.ts +10 -0
  10. package/esm/adaptor/pattern.js +54 -0
  11. package/esm/adaptor/pattern.js.map +1 -0
  12. package/esm/core/plot.d.ts +11 -1
  13. package/esm/core/plot.js +42 -1
  14. package/esm/core/plot.js.map +1 -1
  15. package/esm/index.d.ts +5 -1
  16. package/esm/index.js +5 -1
  17. package/esm/index.js.map +1 -1
  18. package/esm/plots/area/adaptor.js +2 -2
  19. package/esm/plots/area/adaptor.js.map +1 -1
  20. package/esm/plots/circle-packing/adaptor.js +4 -4
  21. package/esm/plots/circle-packing/adaptor.js.map +1 -1
  22. package/esm/plots/circle-packing/utils.d.ts +0 -5
  23. package/esm/plots/circle-packing/utils.js +1 -20
  24. package/esm/plots/circle-packing/utils.js.map +1 -1
  25. package/esm/plots/column/adaptor.js +3 -2
  26. package/esm/plots/column/adaptor.js.map +1 -1
  27. package/esm/plots/dual-axes/adaptor.js +6 -4
  28. package/esm/plots/dual-axes/adaptor.js.map +1 -1
  29. package/esm/plots/dual-axes/types.d.ts +2 -1
  30. package/esm/plots/dual-axes/types.js.map +1 -1
  31. package/esm/plots/dual-axes/util/geometry.js +3 -2
  32. package/esm/plots/dual-axes/util/geometry.js.map +1 -1
  33. package/esm/plots/dual-axes/util/legend.js +4 -1
  34. package/esm/plots/dual-axes/util/legend.js.map +1 -1
  35. package/esm/plots/funnel/geometries/basic.js +2 -7
  36. package/esm/plots/funnel/geometries/basic.js.map +1 -1
  37. package/esm/plots/heatmap/adaptor.js +2 -2
  38. package/esm/plots/heatmap/adaptor.js.map +1 -1
  39. package/esm/plots/heatmap/types.d.ts +4 -2
  40. package/esm/plots/heatmap/types.js.map +1 -1
  41. package/esm/plots/histogram/adaptor.js +2 -1
  42. package/esm/plots/histogram/adaptor.js.map +1 -1
  43. package/esm/plots/liquid/adaptor.js +2 -2
  44. package/esm/plots/liquid/adaptor.js.map +1 -1
  45. package/esm/plots/pie/adaptor.js +2 -1
  46. package/esm/plots/pie/adaptor.js.map +1 -1
  47. package/esm/plots/radial-bar/adaptor.js +2 -1
  48. package/esm/plots/radial-bar/adaptor.js.map +1 -1
  49. package/esm/plots/rose/adaptor.js +2 -2
  50. package/esm/plots/rose/adaptor.js.map +1 -1
  51. package/esm/plots/sunburst/adaptor.js +2 -2
  52. package/esm/plots/sunburst/adaptor.js.map +1 -1
  53. package/esm/plots/tiny-area/adaptor.js +2 -2
  54. package/esm/plots/tiny-area/adaptor.js.map +1 -1
  55. package/esm/plots/tiny-column/adaptor.js +2 -2
  56. package/esm/plots/tiny-column/adaptor.js.map +1 -1
  57. package/esm/plots/treemap/adaptor.js +2 -1
  58. package/esm/plots/treemap/adaptor.js.map +1 -1
  59. package/esm/plots/venn/adaptor.d.ts +23 -0
  60. package/esm/plots/venn/adaptor.js +177 -0
  61. package/esm/plots/venn/adaptor.js.map +1 -0
  62. package/esm/plots/venn/constant.d.ts +7 -0
  63. package/esm/plots/venn/constant.js +33 -0
  64. package/esm/plots/venn/constant.js.map +1 -0
  65. package/esm/plots/venn/index.d.ts +24 -0
  66. package/esm/plots/venn/index.js +47 -0
  67. package/esm/plots/venn/index.js.map +1 -0
  68. package/esm/plots/venn/interaction/action.d.ts +18 -0
  69. package/esm/plots/venn/interaction/action.js +77 -0
  70. package/esm/plots/venn/interaction/action.js.map +1 -0
  71. package/esm/plots/venn/interaction/index.d.ts +1 -0
  72. package/esm/plots/venn/interaction/index.js +15 -0
  73. package/esm/plots/venn/interaction/index.js.map +1 -0
  74. package/esm/plots/venn/label.d.ts +1 -0
  75. package/esm/plots/venn/label.js +31 -0
  76. package/esm/plots/venn/label.js.map +1 -0
  77. package/esm/plots/venn/layout/circleintersection.d.ts +29 -0
  78. package/esm/plots/venn/layout/circleintersection.js +186 -0
  79. package/esm/plots/venn/layout/circleintersection.js.map +1 -0
  80. package/esm/plots/venn/layout/diagram.d.ts +17 -0
  81. package/esm/plots/venn/layout/diagram.js +193 -0
  82. package/esm/plots/venn/layout/diagram.js.map +1 -0
  83. package/esm/plots/venn/layout/layout.d.ts +27 -0
  84. package/esm/plots/venn/layout/layout.js +593 -0
  85. package/esm/plots/venn/layout/layout.js.map +1 -0
  86. package/esm/plots/venn/shape.d.ts +1 -0
  87. package/esm/plots/venn/shape.js +42 -0
  88. package/esm/plots/venn/shape.js.map +1 -0
  89. package/esm/plots/venn/types.d.ts +27 -0
  90. package/esm/plots/venn/types.js +2 -0
  91. package/esm/plots/venn/types.js.map +1 -0
  92. package/esm/plots/venn/utils.d.ts +28 -0
  93. package/esm/plots/venn/utils.js +93 -0
  94. package/esm/plots/venn/utils.js.map +1 -0
  95. package/esm/types/annotation.d.ts +5 -1
  96. package/esm/types/annotation.js.map +1 -1
  97. package/esm/types/attr.d.ts +4 -1
  98. package/esm/types/attr.js.map +1 -1
  99. package/esm/types/common.d.ts +3 -1
  100. package/esm/types/common.js.map +1 -1
  101. package/esm/types/index.d.ts +1 -0
  102. package/esm/types/index.js +1 -0
  103. package/esm/types/index.js.map +1 -1
  104. package/esm/types/pattern.d.ts +47 -0
  105. package/esm/types/pattern.js +1 -0
  106. package/esm/types/pattern.js.map +1 -0
  107. package/esm/utils/color/blend.d.ts +20 -0
  108. package/esm/utils/color/blend.js +92 -0
  109. package/esm/utils/color/blend.js.map +1 -0
  110. package/esm/utils/padding.d.ts +5 -0
  111. package/esm/utils/padding.js +19 -0
  112. package/esm/utils/padding.js.map +1 -1
  113. package/esm/utils/pattern/dot.d.ts +32 -0
  114. package/esm/utils/pattern/dot.js +69 -0
  115. package/esm/utils/pattern/dot.js.map +1 -0
  116. package/esm/utils/pattern/index.d.ts +17 -0
  117. package/esm/utils/pattern/index.js +27 -0
  118. package/esm/utils/pattern/index.js.map +1 -0
  119. package/esm/utils/pattern/line.d.ts +25 -0
  120. package/esm/utils/pattern/line.js +56 -0
  121. package/esm/utils/pattern/line.js.map +1 -0
  122. package/esm/utils/pattern/square.d.ts +28 -0
  123. package/esm/utils/pattern/square.js +61 -0
  124. package/esm/utils/pattern/square.js.map +1 -0
  125. package/esm/utils/pattern/util.d.ts +50 -0
  126. package/esm/utils/pattern/util.js +90 -0
  127. package/esm/utils/pattern/util.js.map +1 -0
  128. package/lib/adaptor/common.d.ts +1 -0
  129. package/lib/adaptor/common.js +2 -0
  130. package/lib/adaptor/common.js.map +1 -1
  131. package/lib/adaptor/pattern.d.ts +10 -0
  132. package/lib/adaptor/pattern.js +58 -0
  133. package/lib/adaptor/pattern.js.map +1 -0
  134. package/lib/core/plot.d.ts +11 -1
  135. package/lib/core/plot.js +41 -0
  136. package/lib/core/plot.js.map +1 -1
  137. package/lib/index.d.ts +5 -1
  138. package/lib/index.js +7 -1
  139. package/lib/index.js.map +1 -1
  140. package/lib/plots/area/adaptor.js +1 -1
  141. package/lib/plots/area/adaptor.js.map +1 -1
  142. package/lib/plots/circle-packing/adaptor.js +2 -2
  143. package/lib/plots/circle-packing/adaptor.js.map +1 -1
  144. package/lib/plots/circle-packing/utils.d.ts +0 -5
  145. package/lib/plots/circle-packing/utils.js +2 -22
  146. package/lib/plots/circle-packing/utils.js.map +1 -1
  147. package/lib/plots/column/adaptor.js +3 -2
  148. package/lib/plots/column/adaptor.js.map +1 -1
  149. package/lib/plots/dual-axes/adaptor.js +6 -4
  150. package/lib/plots/dual-axes/adaptor.js.map +1 -1
  151. package/lib/plots/dual-axes/types.d.ts +2 -1
  152. package/lib/plots/dual-axes/types.js.map +1 -1
  153. package/lib/plots/dual-axes/util/geometry.js +2 -1
  154. package/lib/plots/dual-axes/util/geometry.js.map +1 -1
  155. package/lib/plots/dual-axes/util/legend.js +4 -1
  156. package/lib/plots/dual-axes/util/legend.js.map +1 -1
  157. package/lib/plots/funnel/geometries/basic.js +1 -6
  158. package/lib/plots/funnel/geometries/basic.js.map +1 -1
  159. package/lib/plots/heatmap/adaptor.js +1 -1
  160. package/lib/plots/heatmap/adaptor.js.map +1 -1
  161. package/lib/plots/heatmap/types.d.ts +4 -2
  162. package/lib/plots/heatmap/types.js.map +1 -1
  163. package/lib/plots/histogram/adaptor.js +2 -1
  164. package/lib/plots/histogram/adaptor.js.map +1 -1
  165. package/lib/plots/liquid/adaptor.js +1 -1
  166. package/lib/plots/liquid/adaptor.js.map +1 -1
  167. package/lib/plots/pie/adaptor.js +2 -1
  168. package/lib/plots/pie/adaptor.js.map +1 -1
  169. package/lib/plots/radial-bar/adaptor.js +2 -1
  170. package/lib/plots/radial-bar/adaptor.js.map +1 -1
  171. package/lib/plots/rose/adaptor.js +1 -1
  172. package/lib/plots/rose/adaptor.js.map +1 -1
  173. package/lib/plots/sunburst/adaptor.js +1 -1
  174. package/lib/plots/sunburst/adaptor.js.map +1 -1
  175. package/lib/plots/tiny-area/adaptor.js +1 -1
  176. package/lib/plots/tiny-area/adaptor.js.map +1 -1
  177. package/lib/plots/tiny-column/adaptor.js +1 -1
  178. package/lib/plots/tiny-column/adaptor.js.map +1 -1
  179. package/lib/plots/treemap/adaptor.js +2 -1
  180. package/lib/plots/treemap/adaptor.js.map +1 -1
  181. package/lib/plots/venn/adaptor.d.ts +23 -0
  182. package/lib/plots/venn/adaptor.js +183 -0
  183. package/lib/plots/venn/adaptor.js.map +1 -0
  184. package/lib/plots/venn/constant.d.ts +7 -0
  185. package/lib/plots/venn/constant.js +36 -0
  186. package/lib/plots/venn/constant.js.map +1 -0
  187. package/lib/plots/venn/index.d.ts +24 -0
  188. package/lib/plots/venn/index.js +50 -0
  189. package/lib/plots/venn/index.js.map +1 -0
  190. package/lib/plots/venn/interaction/action.d.ts +18 -0
  191. package/lib/plots/venn/interaction/action.js +80 -0
  192. package/lib/plots/venn/interaction/action.js.map +1 -0
  193. package/lib/plots/venn/interaction/index.d.ts +1 -0
  194. package/lib/plots/venn/interaction/index.js +17 -0
  195. package/lib/plots/venn/interaction/index.js.map +1 -0
  196. package/lib/plots/venn/label.d.ts +1 -0
  197. package/lib/plots/venn/label.js +33 -0
  198. package/lib/plots/venn/label.js.map +1 -0
  199. package/lib/plots/venn/layout/circleintersection.d.ts +29 -0
  200. package/lib/plots/venn/layout/circleintersection.js +196 -0
  201. package/lib/plots/venn/layout/circleintersection.js.map +1 -0
  202. package/lib/plots/venn/layout/diagram.d.ts +17 -0
  203. package/lib/plots/venn/layout/diagram.js +201 -0
  204. package/lib/plots/venn/layout/diagram.js.map +1 -0
  205. package/lib/plots/venn/layout/layout.d.ts +27 -0
  206. package/lib/plots/venn/layout/layout.js +606 -0
  207. package/lib/plots/venn/layout/layout.js.map +1 -0
  208. package/lib/plots/venn/shape.d.ts +1 -0
  209. package/lib/plots/venn/shape.js +44 -0
  210. package/lib/plots/venn/shape.js.map +1 -0
  211. package/lib/plots/venn/types.d.ts +27 -0
  212. package/lib/plots/venn/types.js +4 -0
  213. package/lib/plots/venn/types.js.map +1 -0
  214. package/lib/plots/venn/utils.d.ts +28 -0
  215. package/lib/plots/venn/utils.js +98 -0
  216. package/lib/plots/venn/utils.js.map +1 -0
  217. package/lib/types/annotation.d.ts +5 -1
  218. package/lib/types/annotation.js.map +1 -1
  219. package/lib/types/attr.d.ts +4 -1
  220. package/lib/types/attr.js.map +1 -1
  221. package/lib/types/common.d.ts +3 -1
  222. package/lib/types/common.js.map +1 -1
  223. package/lib/types/index.d.ts +1 -0
  224. package/lib/types/index.js +1 -0
  225. package/lib/types/index.js.map +1 -1
  226. package/lib/types/pattern.d.ts +47 -0
  227. package/lib/types/pattern.js +3 -0
  228. package/lib/types/pattern.js.map +1 -0
  229. package/lib/utils/color/blend.d.ts +20 -0
  230. package/lib/utils/color/blend.js +98 -0
  231. package/lib/utils/color/blend.js.map +1 -0
  232. package/lib/utils/padding.d.ts +5 -0
  233. package/lib/utils/padding.js +21 -1
  234. package/lib/utils/padding.js.map +1 -1
  235. package/lib/utils/pattern/dot.d.ts +32 -0
  236. package/lib/utils/pattern/dot.js +74 -0
  237. package/lib/utils/pattern/dot.js.map +1 -0
  238. package/lib/utils/pattern/index.d.ts +17 -0
  239. package/lib/utils/pattern/index.js +31 -0
  240. package/lib/utils/pattern/index.js.map +1 -0
  241. package/lib/utils/pattern/line.d.ts +25 -0
  242. package/lib/utils/pattern/line.js +61 -0
  243. package/lib/utils/pattern/line.js.map +1 -0
  244. package/lib/utils/pattern/square.d.ts +28 -0
  245. package/lib/utils/pattern/square.js +66 -0
  246. package/lib/utils/pattern/square.js.map +1 -0
  247. package/lib/utils/pattern/util.d.ts +50 -0
  248. package/lib/utils/pattern/util.js +99 -0
  249. package/lib/utils/pattern/util.js.map +1 -0
  250. package/package.json +10 -8
  251. package/src/adaptor/common.ts +2 -0
  252. package/src/adaptor/pattern.ts +66 -0
  253. package/src/core/plot.ts +43 -1
  254. package/src/index.ts +8 -1
  255. package/src/plots/area/adaptor.ts +3 -2
  256. package/src/plots/circle-packing/adaptor.ts +12 -3
  257. package/src/plots/circle-packing/utils.ts +1 -21
  258. package/src/plots/column/adaptor.ts +3 -1
  259. package/src/plots/dual-axes/adaptor.ts +6 -4
  260. package/src/plots/dual-axes/types.ts +11 -1
  261. package/src/plots/dual-axes/util/geometry.ts +3 -2
  262. package/src/plots/dual-axes/util/legend.ts +4 -1
  263. package/src/plots/funnel/geometries/basic.ts +2 -10
  264. package/src/plots/heatmap/adaptor.ts +12 -1
  265. package/src/plots/heatmap/types.ts +6 -2
  266. package/src/plots/histogram/adaptor.ts +13 -1
  267. package/src/plots/liquid/adaptor.ts +2 -2
  268. package/src/plots/pie/adaptor.ts +2 -0
  269. package/src/plots/radial-bar/adaptor.ts +2 -0
  270. package/src/plots/rose/adaptor.ts +2 -1
  271. package/src/plots/sunburst/adaptor.ts +2 -1
  272. package/src/plots/tiny-area/adaptor.ts +2 -2
  273. package/src/plots/tiny-column/adaptor.ts +2 -2
  274. package/src/plots/treemap/adaptor.ts +13 -1
  275. package/src/plots/venn/adaptor.ts +227 -0
  276. package/src/plots/venn/constant.ts +35 -0
  277. package/src/plots/venn/index.ts +47 -0
  278. package/src/plots/venn/interaction/action.ts +67 -0
  279. package/src/plots/venn/interaction/index.ts +17 -0
  280. package/src/plots/venn/label.ts +26 -0
  281. package/src/plots/venn/layout/circleintersection.ts +223 -0
  282. package/src/plots/venn/layout/diagram.ts +219 -0
  283. package/src/plots/venn/layout/layout.ts +734 -0
  284. package/src/plots/venn/shape.ts +53 -0
  285. package/src/plots/venn/types.ts +25 -0
  286. package/src/plots/venn/utils.ts +100 -0
  287. package/src/types/annotation.ts +3 -1
  288. package/src/types/attr.ts +7 -1
  289. package/src/types/common.ts +3 -1
  290. package/src/types/index.ts +1 -0
  291. package/src/types/pattern.ts +50 -0
  292. package/src/utils/color/blend.ts +103 -0
  293. package/src/utils/padding.ts +20 -0
  294. package/src/utils/pattern/dot.ts +86 -0
  295. package/src/utils/pattern/index.ts +45 -0
  296. package/src/utils/pattern/line.ts +70 -0
  297. package/src/utils/pattern/square.ts +78 -0
  298. package/src/utils/pattern/util.ts +104 -0
@@ -0,0 +1,223 @@
1
+ const SMALL = 1e-10;
2
+
3
+ /** Returns the intersection area of a bunch of circles (where each circle
4
+ is an object having an x,y and radius property) */
5
+ export function intersectionArea(circles, stats?: any) {
6
+ // get all the intersection points of the circles
7
+ const intersectionPoints = getIntersectionPoints(circles);
8
+
9
+ // filter out points that aren't included in all the circles
10
+ const innerPoints = intersectionPoints.filter(function (p) {
11
+ return containedInCircles(p, circles);
12
+ });
13
+
14
+ let arcArea = 0,
15
+ polygonArea = 0,
16
+ i;
17
+ const arcs = [];
18
+ // if we have intersection points that are within all the circles,
19
+ // then figure out the area contained by them
20
+ if (innerPoints.length > 1) {
21
+ // sort the points by angle from the center of the polygon, which lets
22
+ // us just iterate over points to get the edges
23
+ const center = getCenter(innerPoints);
24
+ for (i = 0; i < innerPoints.length; ++i) {
25
+ const p = innerPoints[i];
26
+ p.angle = Math.atan2(p.x - center.x, p.y - center.y);
27
+ }
28
+ innerPoints.sort(function (a, b) {
29
+ return b.angle - a.angle;
30
+ });
31
+
32
+ // iterate over all points, get arc between the points
33
+ // and update the areas
34
+ let p2 = innerPoints[innerPoints.length - 1];
35
+ for (i = 0; i < innerPoints.length; ++i) {
36
+ const p1 = innerPoints[i];
37
+
38
+ // polygon area updates easily ...
39
+ polygonArea += (p2.x + p1.x) * (p1.y - p2.y);
40
+
41
+ // updating the arc area is a little more involved
42
+ const midPoint = { x: (p1.x + p2.x) / 2, y: (p1.y + p2.y) / 2 };
43
+ let arc = null;
44
+
45
+ for (let j = 0; j < p1.parentIndex.length; ++j) {
46
+ if (p2.parentIndex.indexOf(p1.parentIndex[j]) > -1) {
47
+ // figure out the angle halfway between the two points
48
+ // on the current circle
49
+ const circle = circles[p1.parentIndex[j]],
50
+ a1 = Math.atan2(p1.x - circle.x, p1.y - circle.y),
51
+ a2 = Math.atan2(p2.x - circle.x, p2.y - circle.y);
52
+
53
+ let angleDiff = a2 - a1;
54
+ if (angleDiff < 0) {
55
+ angleDiff += 2 * Math.PI;
56
+ }
57
+
58
+ // and use that angle to figure out the width of the
59
+ // arc
60
+ const a = a2 - angleDiff / 2;
61
+ let width = distance(midPoint, {
62
+ x: circle.x + circle.radius * Math.sin(a),
63
+ y: circle.y + circle.radius * Math.cos(a),
64
+ });
65
+
66
+ // clamp the width to the largest is can actually be
67
+ // (sometimes slightly overflows because of FP errors)
68
+ if (width > circle.radius * 2) {
69
+ width = circle.radius * 2;
70
+ }
71
+
72
+ // pick the circle whose arc has the smallest width
73
+ if (arc === null || arc.width > width) {
74
+ arc = { circle: circle, width: width, p1: p1, p2: p2 };
75
+ }
76
+ }
77
+ }
78
+
79
+ if (arc !== null) {
80
+ arcs.push(arc);
81
+ arcArea += circleArea(arc.circle.radius, arc.width);
82
+ p2 = p1;
83
+ }
84
+ }
85
+ } else {
86
+ // no intersection points, is either disjoint - or is completely
87
+ // overlapped. figure out which by examining the smallest circle
88
+ let smallest = circles[0];
89
+ for (i = 1; i < circles.length; ++i) {
90
+ if (circles[i].radius < smallest.radius) {
91
+ smallest = circles[i];
92
+ }
93
+ }
94
+
95
+ // make sure the smallest circle is completely contained in all
96
+ // the other circles
97
+ let disjoint = false;
98
+ for (i = 0; i < circles.length; ++i) {
99
+ if (distance(circles[i], smallest) > Math.abs(smallest.radius - circles[i].radius)) {
100
+ disjoint = true;
101
+ break;
102
+ }
103
+ }
104
+
105
+ if (disjoint) {
106
+ arcArea = polygonArea = 0;
107
+ } else {
108
+ arcArea = smallest.radius * smallest.radius * Math.PI;
109
+ arcs.push({
110
+ circle: smallest,
111
+ p1: { x: smallest.x, y: smallest.y + smallest.radius },
112
+ p2: { x: smallest.x - SMALL, y: smallest.y + smallest.radius },
113
+ width: smallest.radius * 2,
114
+ });
115
+ }
116
+ }
117
+
118
+ polygonArea /= 2;
119
+ if (stats) {
120
+ stats.area = arcArea + polygonArea;
121
+ stats.arcArea = arcArea;
122
+ stats.polygonArea = polygonArea;
123
+ stats.arcs = arcs;
124
+ stats.innerPoints = innerPoints;
125
+ stats.intersectionPoints = intersectionPoints;
126
+ }
127
+
128
+ return arcArea + polygonArea;
129
+ }
130
+
131
+ /** returns whether a point is contained by all of a list of circles */
132
+ export function containedInCircles(point, circles) {
133
+ for (let i = 0; i < circles.length; ++i) {
134
+ if (distance(point, circles[i]) > circles[i].radius + SMALL) {
135
+ return false;
136
+ }
137
+ }
138
+ return true;
139
+ }
140
+
141
+ /** Gets all intersection points between a bunch of circles */
142
+ function getIntersectionPoints(circles) {
143
+ const ret = [];
144
+ for (let i = 0; i < circles.length; ++i) {
145
+ for (let j = i + 1; j < circles.length; ++j) {
146
+ const intersect = circleCircleIntersection(circles[i], circles[j]);
147
+ for (let k = 0; k < intersect.length; ++k) {
148
+ const p: any = intersect[k];
149
+ p.parentIndex = [i, j];
150
+ ret.push(p);
151
+ }
152
+ }
153
+ }
154
+ return ret;
155
+ }
156
+
157
+ /** Circular segment area calculation. See http://mathworld.wolfram.com/CircularSegment.html */
158
+ export function circleArea(r, width) {
159
+ return r * r * Math.acos(1 - width / r) - (r - width) * Math.sqrt(width * (2 * r - width));
160
+ }
161
+
162
+ /** euclidean distance between two points */
163
+ export function distance(p1, p2) {
164
+ return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
165
+ }
166
+
167
+ /** Returns the overlap area of two circles of radius r1 and r2 - that
168
+ have their centers separated by distance d. Simpler faster
169
+ circle intersection for only two circles */
170
+ export function circleOverlap(r1, r2, d) {
171
+ // no overlap
172
+ if (d >= r1 + r2) {
173
+ return 0;
174
+ }
175
+
176
+ // completely overlapped
177
+ if (d <= Math.abs(r1 - r2)) {
178
+ return Math.PI * Math.min(r1, r2) * Math.min(r1, r2);
179
+ }
180
+
181
+ const w1 = r1 - (d * d - r2 * r2 + r1 * r1) / (2 * d),
182
+ w2 = r2 - (d * d - r1 * r1 + r2 * r2) / (2 * d);
183
+ return circleArea(r1, w1) + circleArea(r2, w2);
184
+ }
185
+
186
+ /** Given two circles (containing a x/y/radius attributes),
187
+ returns the intersecting points if possible.
188
+ note: doesn't handle cases where there are infinitely many
189
+ intersection points (circles are equivalent):, or only one intersection point*/
190
+ export function circleCircleIntersection(p1, p2) {
191
+ const d = distance(p1, p2),
192
+ r1 = p1.radius,
193
+ r2 = p2.radius;
194
+
195
+ // if to far away, or self contained - can't be done
196
+ if (d >= r1 + r2 || d <= Math.abs(r1 - r2)) {
197
+ return [];
198
+ }
199
+
200
+ const a = (r1 * r1 - r2 * r2 + d * d) / (2 * d),
201
+ h = Math.sqrt(r1 * r1 - a * a),
202
+ x0 = p1.x + (a * (p2.x - p1.x)) / d,
203
+ y0 = p1.y + (a * (p2.y - p1.y)) / d,
204
+ rx = -(p2.y - p1.y) * (h / d),
205
+ ry = -(p2.x - p1.x) * (h / d);
206
+
207
+ return [
208
+ { x: x0 + rx, y: y0 - ry },
209
+ { x: x0 - rx, y: y0 + ry },
210
+ ];
211
+ }
212
+
213
+ /** Returns the center of a bunch of points */
214
+ export function getCenter(points) {
215
+ const center = { x: 0, y: 0 };
216
+ for (let i = 0; i < points.length; ++i) {
217
+ center.x += points[i].x;
218
+ center.y += points[i].y;
219
+ }
220
+ center.x /= points.length;
221
+ center.y /= points.length;
222
+ return center;
223
+ }
@@ -0,0 +1,219 @@
1
+ import { nelderMead } from 'fmin';
2
+ import { intersectionArea, distance, getCenter } from './circleintersection';
3
+
4
+ function circleMargin(current, interior, exterior) {
5
+ let margin = interior[0].radius - distance(interior[0], current),
6
+ i,
7
+ m;
8
+ for (i = 1; i < interior.length; ++i) {
9
+ m = interior[i].radius - distance(interior[i], current);
10
+ if (m <= margin) {
11
+ margin = m;
12
+ }
13
+ }
14
+
15
+ for (i = 0; i < exterior.length; ++i) {
16
+ m = distance(exterior[i], current) - exterior[i].radius;
17
+ if (m <= margin) {
18
+ margin = m;
19
+ }
20
+ }
21
+ return margin;
22
+ }
23
+
24
+ // compute the center of some circles by maximizing the margin of
25
+ // the center point relative to the circles (interior) after subtracting
26
+ // nearby circles (exterior)
27
+ export function computeTextCentre(interior, exterior) {
28
+ // get an initial estimate by sampling around the interior circles
29
+ // and taking the point with the biggest margin
30
+ const points = [];
31
+ let i;
32
+ for (i = 0; i < interior.length; ++i) {
33
+ const c = interior[i];
34
+ points.push({ x: c.x, y: c.y });
35
+ points.push({ x: c.x + c.radius / 2, y: c.y });
36
+ points.push({ x: c.x - c.radius / 2, y: c.y });
37
+ points.push({ x: c.x, y: c.y + c.radius / 2 });
38
+ points.push({ x: c.x, y: c.y - c.radius / 2 });
39
+ }
40
+ let initial = points[0],
41
+ margin = circleMargin(points[0], interior, exterior);
42
+ for (i = 1; i < points.length; ++i) {
43
+ const m = circleMargin(points[i], interior, exterior);
44
+ if (m >= margin) {
45
+ initial = points[i];
46
+ margin = m;
47
+ }
48
+ }
49
+
50
+ // maximize the margin numerically
51
+ const solution = nelderMead(
52
+ function (p) {
53
+ return -1 * circleMargin({ x: p[0], y: p[1] }, interior, exterior);
54
+ },
55
+ [initial.x, initial.y],
56
+ { maxIterations: 500, minErrorDelta: 1e-10 }
57
+ ).x;
58
+ let ret: any = { x: solution[0], y: solution[1] };
59
+
60
+ // check solution, fallback as needed (happens if fully overlapped
61
+ // etc)
62
+ let valid = true;
63
+ for (i = 0; i < interior.length; ++i) {
64
+ if (distance(ret, interior[i]) > interior[i].radius) {
65
+ valid = false;
66
+ break;
67
+ }
68
+ }
69
+
70
+ for (i = 0; i < exterior.length; ++i) {
71
+ if (distance(ret, exterior[i]) < exterior[i].radius) {
72
+ valid = false;
73
+ break;
74
+ }
75
+ }
76
+
77
+ if (!valid) {
78
+ if (interior.length == 1) {
79
+ ret = { x: interior[0].x, y: interior[0].y };
80
+ } else {
81
+ const areaStats: any = {};
82
+ intersectionArea(interior, areaStats);
83
+
84
+ if (areaStats.arcs.length === 0) {
85
+ ret = { x: 0, y: -1000, disjoint: true };
86
+ } else if (areaStats.arcs.length == 1) {
87
+ ret = { x: areaStats.arcs[0].circle.x, y: areaStats.arcs[0].circle.y };
88
+ } else if (exterior.length) {
89
+ // try again without other circles
90
+ ret = computeTextCentre(interior, []);
91
+ } else {
92
+ // take average of all the points in the intersection
93
+ // polygon. this should basically never happen
94
+ // and has some issues:
95
+ // https://github.com/benfred/venn.js/issues/48#issuecomment-146069777
96
+ ret = getCenter(
97
+ areaStats.arcs.map(function (a) {
98
+ return a.p1;
99
+ })
100
+ );
101
+ }
102
+ }
103
+ }
104
+
105
+ return ret;
106
+ }
107
+
108
+ // given a dictionary of {setid : circle}, returns
109
+ // a dictionary of setid to list of circles that completely overlap it
110
+ function getOverlappingCircles(circles) {
111
+ const ret = {},
112
+ circleids = [];
113
+ for (const circleid in circles) {
114
+ circleids.push(circleid);
115
+ ret[circleid] = [];
116
+ }
117
+ for (let i = 0; i < circleids.length; i++) {
118
+ const a = circles[circleids[i]];
119
+ for (let j = i + 1; j < circleids.length; ++j) {
120
+ const b = circles[circleids[j]],
121
+ d = distance(a, b);
122
+
123
+ if (d + b.radius <= a.radius + 1e-10) {
124
+ ret[circleids[j]].push(circleids[i]);
125
+ } else if (d + a.radius <= b.radius + 1e-10) {
126
+ ret[circleids[i]].push(circleids[j]);
127
+ }
128
+ }
129
+ }
130
+ return ret;
131
+ }
132
+
133
+ export function computeTextCentres(circles, areas) {
134
+ const ret = {},
135
+ overlapped = getOverlappingCircles(circles);
136
+ for (let i = 0; i < areas.length; ++i) {
137
+ const area = areas[i].sets,
138
+ areaids = {},
139
+ exclude = {};
140
+ for (let j = 0; j < area.length; ++j) {
141
+ areaids[area[j]] = true;
142
+ const overlaps = overlapped[area[j]];
143
+ // keep track of any circles that overlap this area,
144
+ // and don't consider for purposes of computing the text
145
+ // centre
146
+ for (let k = 0; k < overlaps.length; ++k) {
147
+ exclude[overlaps[k]] = true;
148
+ }
149
+ }
150
+
151
+ const interior = [],
152
+ exterior = [];
153
+ for (const setid in circles) {
154
+ if (setid in areaids) {
155
+ interior.push(circles[setid]);
156
+ } else if (!(setid in exclude)) {
157
+ exterior.push(circles[setid]);
158
+ }
159
+ }
160
+ const centre = computeTextCentre(interior, exterior);
161
+ ret[area] = centre;
162
+ if (centre.disjoint && areas[i].size > 0) {
163
+ console.log('WARNING: area ' + area + ' not represented on screen');
164
+ }
165
+ }
166
+ return ret;
167
+ }
168
+
169
+ /**
170
+ * 根据圆心(x, y) 半径 r 返回圆的绘制 path
171
+ * @param x 圆心点 x
172
+ * @param y 圆心点 y
173
+ * @param r 圆的半径
174
+ * @returns 圆的 path
175
+ */
176
+ export function circlePath(x, y, r) {
177
+ const ret = [];
178
+ // ret.push('\nM', x, y);
179
+ // ret.push('\nm', -r, 0);
180
+ // ret.push('\na', r, r, 0, 1, 0, r * 2, 0);
181
+ // ret.push('\na', r, r, 0, 1, 0, -r * 2, 0);
182
+ const x0 = x - r;
183
+ const y0 = y;
184
+ ret.push('M', x0, y0);
185
+ ret.push('A', r, r, 0, 1, 0, x0 + 2 * r, y0);
186
+ ret.push('A', r, r, 0, 1, 0, x0, y0);
187
+
188
+ return ret.join(' ');
189
+ }
190
+
191
+ // inverse of the circlePath function, returns a circle object from an svg path
192
+ export function circleFromPath(path) {
193
+ const tokens = path.split(' ');
194
+ return { x: parseFloat(tokens[1]), y: parseFloat(tokens[2]), radius: -parseFloat(tokens[4]) };
195
+ }
196
+
197
+ /** returns a svg path of the intersection area of a bunch of circles */
198
+ export function intersectionAreaPath(circles) {
199
+ const stats: any = {};
200
+ intersectionArea(circles, stats);
201
+ const arcs = stats.arcs;
202
+
203
+ if (arcs.length === 0) {
204
+ return 'M 0 0';
205
+ } else if (arcs.length == 1) {
206
+ const circle = arcs[0].circle;
207
+ return circlePath(circle.x, circle.y, circle.radius);
208
+ } else {
209
+ // draw path around arcs
210
+ const ret = ['\nM', arcs[0].p2.x, arcs[0].p2.y];
211
+ for (let i = 0; i < arcs.length; ++i) {
212
+ const arc = arcs[i],
213
+ r = arc.circle.radius,
214
+ wide = arc.width > r;
215
+ ret.push('\nA', r, r, 0, wide ? 1 : 0, 1, arc.p1.x, arc.p1.y);
216
+ }
217
+ return ret.join(' ');
218
+ }
219
+ }