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