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