@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,734 @@
1
+ import { nelderMead, bisect, conjugateGradient, zeros, zerosM, norm2, scale } from 'fmin';
2
+ import { intersectionArea, circleOverlap, circleCircleIntersection, distance } from './circleintersection';
3
+
4
+ /** given a list of set objects, and their corresponding overlaps.
5
+ updates the (x, y, radius) attribute on each set such that their positions
6
+ roughly correspond to the desired overlaps */
7
+ export function venn(areas, parameters?: any) {
8
+ parameters = parameters || {};
9
+ parameters.maxIterations = parameters.maxIterations || 500;
10
+ const initialLayout = parameters.initialLayout || bestInitialLayout;
11
+ const loss = parameters.lossFunction || lossFunction;
12
+
13
+ // add in missing pairwise areas as having 0 size
14
+ areas = addMissingAreas(areas);
15
+
16
+ // initial layout is done greedily
17
+ const circles = initialLayout(areas, parameters);
18
+
19
+ // transform x/y coordinates to a vector to optimize
20
+ const initial = [],
21
+ setids = [];
22
+ let setid;
23
+ for (setid in circles) {
24
+ // eslint-disable-next-line
25
+ if (circles.hasOwnProperty(setid)) {
26
+ initial.push(circles[setid].x);
27
+ initial.push(circles[setid].y);
28
+ setids.push(setid);
29
+ }
30
+ }
31
+
32
+ // optimize initial layout from our loss function
33
+ const solution = nelderMead(
34
+ function (values) {
35
+ const current = {};
36
+ for (let i = 0; i < setids.length; ++i) {
37
+ const setid = setids[i];
38
+ current[setid] = {
39
+ x: values[2 * i],
40
+ y: values[2 * i + 1],
41
+ radius: circles[setid].radius,
42
+ // size : circles[setid].size
43
+ };
44
+ }
45
+ return loss(current, areas);
46
+ },
47
+ initial,
48
+ parameters
49
+ );
50
+
51
+ // transform solution vector back to x/y points
52
+ const positions = solution.x;
53
+ for (let i = 0; i < setids.length; ++i) {
54
+ setid = setids[i];
55
+ circles[setid].x = positions[2 * i];
56
+ circles[setid].y = positions[2 * i + 1];
57
+ }
58
+
59
+ return circles;
60
+ }
61
+
62
+ const SMALL = 1e-10;
63
+
64
+ /** Returns the distance necessary for two circles of radius r1 + r2 to
65
+ have the overlap area 'overlap' */
66
+ export function distanceFromIntersectArea(r1, r2, overlap) {
67
+ // handle complete overlapped circles
68
+ if (Math.min(r1, r2) * Math.min(r1, r2) * Math.PI <= overlap + SMALL) {
69
+ return Math.abs(r1 - r2);
70
+ }
71
+
72
+ return bisect(
73
+ function (distance) {
74
+ return circleOverlap(r1, r2, distance) - overlap;
75
+ },
76
+ 0,
77
+ r1 + r2
78
+ );
79
+ }
80
+
81
+ /** Missing pair-wise intersection area data can cause problems:
82
+ treating as an unknown means that sets will be laid out overlapping,
83
+ which isn't what people expect. To reflect that we want disjoint sets
84
+ here, set the overlap to 0 for all missing pairwise set intersections */
85
+ function addMissingAreas(areas) {
86
+ areas = areas.slice();
87
+
88
+ // two circle intersections that aren't defined
89
+ const ids: number[] = [],
90
+ pairs: any = {};
91
+ let i, j, a, b;
92
+ for (i = 0; i < areas.length; ++i) {
93
+ const area = areas[i];
94
+ if (area.sets.length == 1) {
95
+ ids.push(area.sets[0]);
96
+ } else if (area.sets.length == 2) {
97
+ a = area.sets[0];
98
+ b = area.sets[1];
99
+ // @ts-ignore
100
+ pairs[[a, b]] = true;
101
+ // @ts-ignore
102
+ pairs[[b, a]] = true;
103
+ }
104
+ }
105
+ ids.sort((a, b) => {
106
+ return a > b ? 1 : -1;
107
+ });
108
+
109
+ for (i = 0; i < ids.length; ++i) {
110
+ a = ids[i];
111
+ for (j = i + 1; j < ids.length; ++j) {
112
+ b = ids[j];
113
+ // @ts-ignore
114
+ if (!([a, b] in pairs)) {
115
+ areas.push({ sets: [a, b], size: 0 });
116
+ }
117
+ }
118
+ }
119
+ return areas;
120
+ }
121
+
122
+ /// Returns two matrices, one of the euclidean distances between the sets
123
+ /// and the other indicating if there are subset or disjoint set relationships
124
+ export function getDistanceMatrices(areas, sets, setids) {
125
+ // initialize an empty distance matrix between all the points
126
+ const distances = zerosM(sets.length, sets.length),
127
+ constraints = zerosM(sets.length, sets.length);
128
+
129
+ // compute required distances between all the sets such that
130
+ // the areas match
131
+ areas
132
+ .filter(function (x) {
133
+ return x.sets.length == 2;
134
+ })
135
+ .map(function (current) {
136
+ const left = setids[current.sets[0]],
137
+ right = setids[current.sets[1]],
138
+ r1 = Math.sqrt(sets[left].size / Math.PI),
139
+ r2 = Math.sqrt(sets[right].size / Math.PI),
140
+ distance = distanceFromIntersectArea(r1, r2, current.size);
141
+
142
+ distances[left][right] = distances[right][left] = distance;
143
+
144
+ // also update constraints to indicate if its a subset or disjoint
145
+ // relationship
146
+ let c = 0;
147
+ if (current.size + 1e-10 >= Math.min(sets[left].size, sets[right].size)) {
148
+ c = 1;
149
+ } else if (current.size <= 1e-10) {
150
+ c = -1;
151
+ }
152
+ constraints[left][right] = constraints[right][left] = c;
153
+ });
154
+
155
+ return { distances: distances, constraints: constraints };
156
+ }
157
+
158
+ /// computes the gradient and loss simulatenously for our constrained MDS optimizer
159
+ function constrainedMDSGradient(x, fxprime, distances, constraints) {
160
+ let loss = 0,
161
+ i;
162
+ for (i = 0; i < fxprime.length; ++i) {
163
+ fxprime[i] = 0;
164
+ }
165
+
166
+ for (i = 0; i < distances.length; ++i) {
167
+ const xi = x[2 * i],
168
+ yi = x[2 * i + 1];
169
+ for (let j = i + 1; j < distances.length; ++j) {
170
+ const xj = x[2 * j],
171
+ yj = x[2 * j + 1],
172
+ dij = distances[i][j],
173
+ constraint = constraints[i][j];
174
+
175
+ const squaredDistance = (xj - xi) * (xj - xi) + (yj - yi) * (yj - yi),
176
+ distance = Math.sqrt(squaredDistance),
177
+ delta = squaredDistance - dij * dij;
178
+
179
+ if ((constraint > 0 && distance <= dij) || (constraint < 0 && distance >= dij)) {
180
+ continue;
181
+ }
182
+
183
+ loss += 2 * delta * delta;
184
+
185
+ fxprime[2 * i] += 4 * delta * (xi - xj);
186
+ fxprime[2 * i + 1] += 4 * delta * (yi - yj);
187
+
188
+ fxprime[2 * j] += 4 * delta * (xj - xi);
189
+ fxprime[2 * j + 1] += 4 * delta * (yj - yi);
190
+ }
191
+ }
192
+ return loss;
193
+ }
194
+
195
+ /// takes the best working variant of either constrained MDS or greedy
196
+ export function bestInitialLayout(areas, params) {
197
+ let initial = greedyLayout(areas, params);
198
+ const loss = params.lossFunction || lossFunction;
199
+
200
+ // greedylayout is sufficient for all 2/3 circle cases. try out
201
+ // constrained MDS for higher order problems, take its output
202
+ // if it outperforms. (greedy is aesthetically better on 2/3 circles
203
+ // since it axis aligns)
204
+ if (areas.length >= 8) {
205
+ const constrained = constrainedMDSLayout(areas, params),
206
+ constrainedLoss = loss(constrained, areas),
207
+ greedyLoss = loss(initial, areas);
208
+
209
+ if (constrainedLoss + 1e-8 < greedyLoss) {
210
+ initial = constrained;
211
+ }
212
+ }
213
+ return initial;
214
+ }
215
+
216
+ /// use the constrained MDS variant to generate an initial layout
217
+ export function constrainedMDSLayout(areas, params) {
218
+ params = params || {};
219
+ const restarts = params.restarts || 10;
220
+
221
+ // bidirectionally map sets to a rowid (so we can create a matrix)
222
+ const sets = [],
223
+ setids = {};
224
+ let i;
225
+ for (i = 0; i < areas.length; ++i) {
226
+ const area = areas[i];
227
+ if (area.sets.length == 1) {
228
+ setids[area.sets[0]] = sets.length;
229
+ sets.push(area);
230
+ }
231
+ }
232
+
233
+ const matrices = getDistanceMatrices(areas, sets, setids);
234
+ let distances = matrices.distances;
235
+ const constraints = matrices.constraints;
236
+
237
+ // keep distances bounded, things get messed up otherwise.
238
+ // TODO: proper preconditioner?
239
+ const norm = norm2(distances.map(norm2)) / distances.length;
240
+ distances = distances.map(function (row) {
241
+ return row.map(function (value) {
242
+ return value / norm;
243
+ });
244
+ });
245
+
246
+ const obj = function (x, fxprime) {
247
+ return constrainedMDSGradient(x, fxprime, distances, constraints);
248
+ };
249
+
250
+ let best, current;
251
+ for (i = 0; i < restarts; ++i) {
252
+ const initial = zeros(distances.length * 2).map(Math.random);
253
+
254
+ current = conjugateGradient(obj, initial, params);
255
+ if (!best || current.fx < best.fx) {
256
+ best = current;
257
+ }
258
+ }
259
+ const positions = best.x;
260
+
261
+ // translate rows back to (x,y,radius) coordinates
262
+ const circles = {};
263
+ for (i = 0; i < sets.length; ++i) {
264
+ const set = sets[i];
265
+ circles[set.sets[0]] = {
266
+ x: positions[2 * i] * norm,
267
+ y: positions[2 * i + 1] * norm,
268
+ radius: Math.sqrt(set.size / Math.PI),
269
+ };
270
+ }
271
+
272
+ if (params.history) {
273
+ for (i = 0; i < params.history.length; ++i) {
274
+ scale(params.history[i].x, norm);
275
+ }
276
+ }
277
+ return circles;
278
+ }
279
+
280
+ /** Lays out a Venn diagram greedily, going from most overlapped sets to
281
+ least overlapped, attempting to position each new set such that the
282
+ overlapping areas to already positioned sets are basically right */
283
+ export function greedyLayout(areas, params) {
284
+ const loss = params && params.lossFunction ? params.lossFunction : lossFunction;
285
+ // define a circle for each set
286
+ const circles = {},
287
+ setOverlaps = {};
288
+ let set;
289
+ for (let i = 0; i < areas.length; ++i) {
290
+ const area = areas[i];
291
+ if (area.sets.length == 1) {
292
+ set = area.sets[0];
293
+ circles[set] = {
294
+ x: 1e10,
295
+ y: 1e10,
296
+ // rowid: circles.length, // fix to ->
297
+ rowid: Object.keys(circles).length,
298
+ size: area.size,
299
+ radius: Math.sqrt(area.size / Math.PI),
300
+ };
301
+ setOverlaps[set] = [];
302
+ }
303
+ }
304
+ areas = areas.filter(function (a) {
305
+ return a.sets.length == 2;
306
+ });
307
+
308
+ // map each set to a list of all the other sets that overlap it
309
+ for (let i = 0; i < areas.length; ++i) {
310
+ const current = areas[i];
311
+ // eslint-disable-next-line
312
+ let weight = current.hasOwnProperty('weight') ? current.weight : 1.0;
313
+ const left = current.sets[0],
314
+ right = current.sets[1];
315
+
316
+ // completely overlapped circles shouldn't be positioned early here
317
+ if (current.size + SMALL >= Math.min(circles[left].size, circles[right].size)) {
318
+ weight = 0;
319
+ }
320
+
321
+ setOverlaps[left].push({ set: right, size: current.size, weight: weight });
322
+ setOverlaps[right].push({ set: left, size: current.size, weight: weight });
323
+ }
324
+
325
+ // get list of most overlapped sets
326
+ const mostOverlapped = [];
327
+ for (set in setOverlaps) {
328
+ // eslint-disable-next-line
329
+ if (setOverlaps.hasOwnProperty(set)) {
330
+ let size = 0;
331
+ for (let i = 0; i < setOverlaps[set].length; ++i) {
332
+ size += setOverlaps[set][i].size * setOverlaps[set][i].weight;
333
+ }
334
+
335
+ mostOverlapped.push({ set: set, size: size });
336
+ }
337
+ }
338
+
339
+ // sort by size desc
340
+ function sortOrder(a, b) {
341
+ return b.size - a.size;
342
+ }
343
+ mostOverlapped.sort(sortOrder);
344
+
345
+ // keep track of what sets have been laid out
346
+ const positioned = {};
347
+ function isPositioned(element) {
348
+ return element.set in positioned;
349
+ }
350
+
351
+ // adds a point to the output
352
+ function positionSet(point, index) {
353
+ circles[index].x = point.x;
354
+ circles[index].y = point.y;
355
+ positioned[index] = true;
356
+ }
357
+
358
+ // add most overlapped set at (0,0)
359
+ positionSet({ x: 0, y: 0 }, mostOverlapped[0].set);
360
+
361
+ // get distances between all points. TODO, necessary?
362
+ // answer: probably not
363
+ // var distances = venn.getDistanceMatrices(circles, areas).distances;
364
+ for (let i = 1; i < mostOverlapped.length; ++i) {
365
+ const setIndex = mostOverlapped[i].set,
366
+ overlap = setOverlaps[setIndex].filter(isPositioned);
367
+ set = circles[setIndex];
368
+ overlap.sort(sortOrder);
369
+
370
+ if (overlap.length === 0) {
371
+ // this shouldn't happen anymore with addMissingAreas
372
+ throw 'ERROR: missing pairwise overlap information';
373
+ }
374
+
375
+ const points = [];
376
+ for (let j = 0; j < overlap.length; ++j) {
377
+ // get appropriate distance from most overlapped already added set
378
+ const p1 = circles[overlap[j].set],
379
+ d1 = distanceFromIntersectArea(set.radius, p1.radius, overlap[j].size);
380
+
381
+ // sample positions at 90 degrees for maximum aesthetics
382
+ points.push({ x: p1.x + d1, y: p1.y });
383
+ points.push({ x: p1.x - d1, y: p1.y });
384
+ points.push({ y: p1.y + d1, x: p1.x });
385
+ points.push({ y: p1.y - d1, x: p1.x });
386
+
387
+ // if we have at least 2 overlaps, then figure out where the
388
+ // set should be positioned analytically and try those too
389
+ for (let k = j + 1; k < overlap.length; ++k) {
390
+ const p2 = circles[overlap[k].set],
391
+ d2 = distanceFromIntersectArea(set.radius, p2.radius, overlap[k].size);
392
+
393
+ const extraPoints = circleCircleIntersection(
394
+ { x: p1.x, y: p1.y, radius: d1 },
395
+ { x: p2.x, y: p2.y, radius: d2 }
396
+ );
397
+
398
+ for (let l = 0; l < extraPoints.length; ++l) {
399
+ points.push(extraPoints[l]);
400
+ }
401
+ }
402
+ }
403
+
404
+ // we have some candidate positions for the set, examine loss
405
+ // at each position to figure out where to put it at
406
+ let bestLoss = 1e50,
407
+ bestPoint = points[0];
408
+ for (let j = 0; j < points.length; ++j) {
409
+ circles[setIndex].x = points[j].x;
410
+ circles[setIndex].y = points[j].y;
411
+ const localLoss = loss(circles, areas);
412
+ if (localLoss < bestLoss) {
413
+ bestLoss = localLoss;
414
+ bestPoint = points[j];
415
+ }
416
+ }
417
+
418
+ positionSet(bestPoint, setIndex);
419
+ }
420
+
421
+ return circles;
422
+ }
423
+
424
+ /** Given a bunch of sets, and the desired overlaps between these sets - computes
425
+ the distance from the actual overlaps to the desired overlaps. Note that
426
+ this method ignores overlaps of more than 2 circles */
427
+ export function lossFunction(sets, overlaps) {
428
+ let output = 0;
429
+
430
+ function getCircles(indices) {
431
+ return indices.map(function (i) {
432
+ return sets[i];
433
+ });
434
+ }
435
+
436
+ for (let i = 0; i < overlaps.length; ++i) {
437
+ const area = overlaps[i];
438
+ let overlap;
439
+ if (area.sets.length == 1) {
440
+ continue;
441
+ } else if (area.sets.length == 2) {
442
+ const left = sets[area.sets[0]],
443
+ right = sets[area.sets[1]];
444
+ overlap = circleOverlap(left.radius, right.radius, distance(left, right));
445
+ } else {
446
+ overlap = intersectionArea(getCircles(area.sets));
447
+ }
448
+
449
+ // eslint-disable-next-line
450
+ const weight = area.hasOwnProperty('weight') ? area.weight : 1.0;
451
+ output += weight * (overlap - area.size) * (overlap - area.size);
452
+ }
453
+
454
+ return output;
455
+ }
456
+
457
+ // orientates a bunch of circles to point in orientation
458
+ function orientateCircles(circles, orientation, orientationOrder) {
459
+ if (orientationOrder === null) {
460
+ circles.sort(function (a, b) {
461
+ return b.radius - a.radius;
462
+ });
463
+ } else {
464
+ circles.sort(orientationOrder);
465
+ }
466
+
467
+ let i;
468
+ // shift circles so largest circle is at (0, 0)
469
+ if (circles.length > 0) {
470
+ const largestX = circles[0].x,
471
+ largestY = circles[0].y;
472
+
473
+ for (i = 0; i < circles.length; ++i) {
474
+ circles[i].x -= largestX;
475
+ circles[i].y -= largestY;
476
+ }
477
+ }
478
+
479
+ if (circles.length == 2) {
480
+ // if the second circle is a subset of the first, arrange so that
481
+ // it is off to one side. hack for https://github.com/benfred/venn.js/issues/120
482
+ const dist = distance(circles[0], circles[1]);
483
+ if (dist < Math.abs(circles[1].radius - circles[0].radius)) {
484
+ circles[1].x = circles[0].x + circles[0].radius - circles[1].radius - 1e-10;
485
+ circles[1].y = circles[0].y;
486
+ }
487
+ }
488
+
489
+ // rotate circles so that second largest is at an angle of 'orientation'
490
+ // from largest
491
+ if (circles.length > 1) {
492
+ const rotation = Math.atan2(circles[1].x, circles[1].y) - orientation;
493
+ let x, y;
494
+ const c = Math.cos(rotation),
495
+ s = Math.sin(rotation);
496
+ for (i = 0; i < circles.length; ++i) {
497
+ x = circles[i].x;
498
+ y = circles[i].y;
499
+ circles[i].x = c * x - s * y;
500
+ circles[i].y = s * x + c * y;
501
+ }
502
+ }
503
+
504
+ // mirror solution if third solution is above plane specified by
505
+ // first two circles
506
+ if (circles.length > 2) {
507
+ let angle = Math.atan2(circles[2].x, circles[2].y) - orientation;
508
+ while (angle < 0) {
509
+ angle += 2 * Math.PI;
510
+ }
511
+ while (angle > 2 * Math.PI) {
512
+ angle -= 2 * Math.PI;
513
+ }
514
+ if (angle > Math.PI) {
515
+ const slope = circles[1].y / (1e-10 + circles[1].x);
516
+ for (i = 0; i < circles.length; ++i) {
517
+ const d = (circles[i].x + slope * circles[i].y) / (1 + slope * slope);
518
+ circles[i].x = 2 * d - circles[i].x;
519
+ circles[i].y = 2 * d * slope - circles[i].y;
520
+ }
521
+ }
522
+ }
523
+ }
524
+
525
+ export function disjointCluster(circles) {
526
+ // union-find clustering to get disjoint sets
527
+ circles.map(function (circle) {
528
+ circle.parent = circle;
529
+ });
530
+
531
+ // path compression step in union find
532
+ function find(circle) {
533
+ if (circle.parent !== circle) {
534
+ circle.parent = find(circle.parent);
535
+ }
536
+ return circle.parent;
537
+ }
538
+
539
+ function union(x, y) {
540
+ const xRoot = find(x),
541
+ yRoot = find(y);
542
+ xRoot.parent = yRoot;
543
+ }
544
+
545
+ // get the union of all overlapping sets
546
+ for (let i = 0; i < circles.length; ++i) {
547
+ for (let j = i + 1; j < circles.length; ++j) {
548
+ const maxDistance = circles[i].radius + circles[j].radius;
549
+ if (distance(circles[i], circles[j]) + 1e-10 < maxDistance) {
550
+ union(circles[j], circles[i]);
551
+ }
552
+ }
553
+ }
554
+
555
+ // find all the disjoint clusters and group them together
556
+ const disjointClusters = {};
557
+ let setid;
558
+ for (let i = 0; i < circles.length; ++i) {
559
+ setid = find(circles[i]).parent.setid;
560
+ if (!(setid in disjointClusters)) {
561
+ disjointClusters[setid] = [];
562
+ }
563
+ disjointClusters[setid].push(circles[i]);
564
+ }
565
+
566
+ // cleanup bookkeeping
567
+ circles.map(function (circle) {
568
+ delete circle.parent;
569
+ });
570
+
571
+ // return in more usable form
572
+ const ret = [];
573
+ for (setid in disjointClusters) {
574
+ // eslint-disable-next-line
575
+ if (disjointClusters.hasOwnProperty(setid)) {
576
+ ret.push(disjointClusters[setid]);
577
+ }
578
+ }
579
+ return ret;
580
+ }
581
+
582
+ function getBoundingBox(circles) {
583
+ const minMax = function (d) {
584
+ const hi = Math.max.apply(
585
+ null,
586
+ circles.map(function (c) {
587
+ return c[d] + c.radius;
588
+ })
589
+ ),
590
+ lo = Math.min.apply(
591
+ null,
592
+ circles.map(function (c) {
593
+ return c[d] - c.radius;
594
+ })
595
+ );
596
+ return { max: hi, min: lo };
597
+ };
598
+
599
+ return { xRange: minMax('x'), yRange: minMax('y') };
600
+ }
601
+
602
+ export function normalizeSolution(solution, orientation, orientationOrder) {
603
+ if (orientation === null) {
604
+ orientation = Math.PI / 2;
605
+ }
606
+
607
+ // work with a list instead of a dictionary, and take a copy so we
608
+ // don't mutate input
609
+ let circles = [],
610
+ i,
611
+ setid;
612
+ for (setid in solution) {
613
+ // eslint-disable-next-line
614
+ if (solution.hasOwnProperty(setid)) {
615
+ const previous = solution[setid];
616
+ circles.push({ x: previous.x, y: previous.y, radius: previous.radius, setid: setid });
617
+ }
618
+ }
619
+
620
+ // get all the disjoint clusters
621
+ const clusters = disjointCluster(circles);
622
+
623
+ // orientate all disjoint sets, get sizes
624
+ for (i = 0; i < clusters.length; ++i) {
625
+ orientateCircles(clusters[i], orientation, orientationOrder);
626
+ const bounds = getBoundingBox(clusters[i]);
627
+ clusters[i].size = (bounds.xRange.max - bounds.xRange.min) * (bounds.yRange.max - bounds.yRange.min);
628
+ clusters[i].bounds = bounds;
629
+ }
630
+ clusters.sort(function (a, b) {
631
+ return b.size - a.size;
632
+ });
633
+
634
+ // orientate the largest at 0,0, and get the bounds
635
+ circles = clusters[0];
636
+ // @ts-ignore fixme 从逻辑上看似乎是不对的,后续看看
637
+ let returnBounds = circles.bounds;
638
+
639
+ const spacing = (returnBounds.xRange.max - returnBounds.xRange.min) / 50;
640
+
641
+ function addCluster(cluster, right, bottom) {
642
+ if (!cluster) return;
643
+
644
+ const bounds = cluster.bounds;
645
+ let xOffset, yOffset, centreing;
646
+
647
+ if (right) {
648
+ xOffset = returnBounds.xRange.max - bounds.xRange.min + spacing;
649
+ } else {
650
+ xOffset = returnBounds.xRange.max - bounds.xRange.max;
651
+ centreing = (bounds.xRange.max - bounds.xRange.min) / 2 - (returnBounds.xRange.max - returnBounds.xRange.min) / 2;
652
+ if (centreing < 0) xOffset += centreing;
653
+ }
654
+
655
+ if (bottom) {
656
+ yOffset = returnBounds.yRange.max - bounds.yRange.min + spacing;
657
+ } else {
658
+ yOffset = returnBounds.yRange.max - bounds.yRange.max;
659
+ centreing = (bounds.yRange.max - bounds.yRange.min) / 2 - (returnBounds.yRange.max - returnBounds.yRange.min) / 2;
660
+ if (centreing < 0) yOffset += centreing;
661
+ }
662
+
663
+ for (let j = 0; j < cluster.length; ++j) {
664
+ cluster[j].x += xOffset;
665
+ cluster[j].y += yOffset;
666
+ circles.push(cluster[j]);
667
+ }
668
+ }
669
+
670
+ let index = 1;
671
+ while (index < clusters.length) {
672
+ addCluster(clusters[index], true, false);
673
+ addCluster(clusters[index + 1], false, true);
674
+ addCluster(clusters[index + 2], true, true);
675
+ index += 3;
676
+
677
+ // have one cluster (in top left). lay out next three relative
678
+ // to it in a grid
679
+ returnBounds = getBoundingBox(circles);
680
+ }
681
+
682
+ // convert back to solution form
683
+ const ret = {};
684
+ for (i = 0; i < circles.length; ++i) {
685
+ ret[circles[i].setid] = circles[i];
686
+ }
687
+ return ret;
688
+ }
689
+
690
+ /** Scales a solution from venn.venn or venn.greedyLayout such that it fits in
691
+ a rectangle of width/height - with padding around the borders. also
692
+ centers the diagram in the available space at the same time */
693
+ export function scaleSolution(solution, width, height, padding) {
694
+ const circles = [],
695
+ setids = [];
696
+ for (const setid in solution) {
697
+ // eslint-disable-next-line
698
+ if (solution.hasOwnProperty(setid)) {
699
+ setids.push(setid);
700
+ circles.push(solution[setid]);
701
+ }
702
+ }
703
+
704
+ width -= 2 * padding;
705
+ height -= 2 * padding;
706
+
707
+ const bounds = getBoundingBox(circles),
708
+ xRange = bounds.xRange,
709
+ yRange = bounds.yRange;
710
+
711
+ if (xRange.max == xRange.min || yRange.max == yRange.min) {
712
+ console.log('not scaling solution: zero size detected');
713
+ return solution;
714
+ }
715
+
716
+ const xScaling = width / (xRange.max - xRange.min),
717
+ yScaling = height / (yRange.max - yRange.min),
718
+ scaling = Math.min(yScaling, xScaling),
719
+ // while we're at it, center the diagram too
720
+ xOffset = (width - (xRange.max - xRange.min) * scaling) / 2,
721
+ yOffset = (height - (yRange.max - yRange.min) * scaling) / 2;
722
+
723
+ const scaled = {};
724
+ for (let i = 0; i < circles.length; ++i) {
725
+ const circle = circles[i];
726
+ scaled[setids[i]] = {
727
+ radius: scaling * circle.radius,
728
+ x: padding + xOffset + (circle.x - xRange.min) * scaling,
729
+ y: padding + yOffset + (circle.y - yRange.min) * scaling,
730
+ };
731
+ }
732
+
733
+ return scaled;
734
+ }