@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.
- package/CHANGELOG.md +1270 -0
- package/{README.zh-CN.md → README.en-US.md} +33 -42
- package/README.md +43 -32
- package/dist/g2plot.min.js +3 -18
- package/dist/g2plot.min.js.map +1 -1
- package/esm/adaptor/common.d.ts +1 -0
- package/esm/adaptor/common.js +1 -0
- package/esm/adaptor/common.js.map +1 -1
- package/esm/adaptor/pattern.d.ts +10 -0
- package/esm/adaptor/pattern.js +54 -0
- package/esm/adaptor/pattern.js.map +1 -0
- package/esm/core/plot.d.ts +11 -1
- package/esm/core/plot.js +42 -1
- package/esm/core/plot.js.map +1 -1
- package/esm/index.d.ts +5 -1
- package/esm/index.js +5 -1
- package/esm/index.js.map +1 -1
- package/esm/plots/area/adaptor.js +2 -2
- package/esm/plots/area/adaptor.js.map +1 -1
- package/esm/plots/circle-packing/adaptor.js +4 -4
- package/esm/plots/circle-packing/adaptor.js.map +1 -1
- package/esm/plots/circle-packing/utils.d.ts +0 -5
- package/esm/plots/circle-packing/utils.js +1 -20
- package/esm/plots/circle-packing/utils.js.map +1 -1
- package/esm/plots/column/adaptor.js +3 -2
- package/esm/plots/column/adaptor.js.map +1 -1
- package/esm/plots/dual-axes/adaptor.js +6 -4
- package/esm/plots/dual-axes/adaptor.js.map +1 -1
- package/esm/plots/dual-axes/types.d.ts +2 -1
- package/esm/plots/dual-axes/types.js.map +1 -1
- package/esm/plots/dual-axes/util/geometry.js +3 -2
- package/esm/plots/dual-axes/util/geometry.js.map +1 -1
- package/esm/plots/dual-axes/util/legend.js +4 -1
- package/esm/plots/dual-axes/util/legend.js.map +1 -1
- package/esm/plots/funnel/geometries/basic.js +2 -7
- package/esm/plots/funnel/geometries/basic.js.map +1 -1
- package/esm/plots/heatmap/adaptor.js +2 -2
- package/esm/plots/heatmap/adaptor.js.map +1 -1
- package/esm/plots/heatmap/types.d.ts +4 -2
- package/esm/plots/heatmap/types.js.map +1 -1
- package/esm/plots/histogram/adaptor.js +2 -1
- package/esm/plots/histogram/adaptor.js.map +1 -1
- package/esm/plots/liquid/adaptor.js +2 -2
- package/esm/plots/liquid/adaptor.js.map +1 -1
- package/esm/plots/pie/adaptor.js +2 -1
- package/esm/plots/pie/adaptor.js.map +1 -1
- package/esm/plots/radial-bar/adaptor.js +2 -1
- package/esm/plots/radial-bar/adaptor.js.map +1 -1
- package/esm/plots/rose/adaptor.js +2 -2
- package/esm/plots/rose/adaptor.js.map +1 -1
- package/esm/plots/sunburst/adaptor.js +2 -2
- package/esm/plots/sunburst/adaptor.js.map +1 -1
- package/esm/plots/tiny-area/adaptor.js +2 -2
- package/esm/plots/tiny-area/adaptor.js.map +1 -1
- package/esm/plots/tiny-column/adaptor.js +2 -2
- package/esm/plots/tiny-column/adaptor.js.map +1 -1
- package/esm/plots/treemap/adaptor.js +2 -1
- package/esm/plots/treemap/adaptor.js.map +1 -1
- package/esm/plots/venn/adaptor.d.ts +23 -0
- package/esm/plots/venn/adaptor.js +177 -0
- package/esm/plots/venn/adaptor.js.map +1 -0
- package/esm/plots/venn/constant.d.ts +7 -0
- package/esm/plots/venn/constant.js +33 -0
- package/esm/plots/venn/constant.js.map +1 -0
- package/esm/plots/venn/index.d.ts +24 -0
- package/esm/plots/venn/index.js +47 -0
- package/esm/plots/venn/index.js.map +1 -0
- package/esm/plots/venn/interaction/action.d.ts +18 -0
- package/esm/plots/venn/interaction/action.js +77 -0
- package/esm/plots/venn/interaction/action.js.map +1 -0
- package/esm/plots/venn/interaction/index.d.ts +1 -0
- package/esm/plots/venn/interaction/index.js +15 -0
- package/esm/plots/venn/interaction/index.js.map +1 -0
- package/esm/plots/venn/label.d.ts +1 -0
- package/esm/plots/venn/label.js +31 -0
- package/esm/plots/venn/label.js.map +1 -0
- package/esm/plots/venn/layout/circleintersection.d.ts +29 -0
- package/esm/plots/venn/layout/circleintersection.js +186 -0
- package/esm/plots/venn/layout/circleintersection.js.map +1 -0
- package/esm/plots/venn/layout/diagram.d.ts +17 -0
- package/esm/plots/venn/layout/diagram.js +193 -0
- package/esm/plots/venn/layout/diagram.js.map +1 -0
- package/esm/plots/venn/layout/layout.d.ts +27 -0
- package/esm/plots/venn/layout/layout.js +593 -0
- package/esm/plots/venn/layout/layout.js.map +1 -0
- package/esm/plots/venn/shape.d.ts +1 -0
- package/esm/plots/venn/shape.js +42 -0
- package/esm/plots/venn/shape.js.map +1 -0
- package/esm/plots/venn/types.d.ts +27 -0
- package/esm/plots/venn/types.js +2 -0
- package/esm/plots/venn/types.js.map +1 -0
- package/esm/plots/venn/utils.d.ts +28 -0
- package/esm/plots/venn/utils.js +93 -0
- package/esm/plots/venn/utils.js.map +1 -0
- package/esm/types/annotation.d.ts +5 -1
- package/esm/types/annotation.js.map +1 -1
- package/esm/types/attr.d.ts +4 -1
- package/esm/types/attr.js.map +1 -1
- package/esm/types/common.d.ts +3 -1
- package/esm/types/common.js.map +1 -1
- package/esm/types/index.d.ts +1 -0
- package/esm/types/index.js +1 -0
- package/esm/types/index.js.map +1 -1
- package/esm/types/pattern.d.ts +47 -0
- package/esm/types/pattern.js +1 -0
- package/esm/types/pattern.js.map +1 -0
- package/esm/utils/color/blend.d.ts +20 -0
- package/esm/utils/color/blend.js +92 -0
- package/esm/utils/color/blend.js.map +1 -0
- package/esm/utils/padding.d.ts +5 -0
- package/esm/utils/padding.js +19 -0
- package/esm/utils/padding.js.map +1 -1
- package/esm/utils/pattern/dot.d.ts +32 -0
- package/esm/utils/pattern/dot.js +69 -0
- package/esm/utils/pattern/dot.js.map +1 -0
- package/esm/utils/pattern/index.d.ts +17 -0
- package/esm/utils/pattern/index.js +27 -0
- package/esm/utils/pattern/index.js.map +1 -0
- package/esm/utils/pattern/line.d.ts +25 -0
- package/esm/utils/pattern/line.js +56 -0
- package/esm/utils/pattern/line.js.map +1 -0
- package/esm/utils/pattern/square.d.ts +28 -0
- package/esm/utils/pattern/square.js +61 -0
- package/esm/utils/pattern/square.js.map +1 -0
- package/esm/utils/pattern/util.d.ts +50 -0
- package/esm/utils/pattern/util.js +90 -0
- package/esm/utils/pattern/util.js.map +1 -0
- package/lib/adaptor/common.d.ts +1 -0
- package/lib/adaptor/common.js +2 -0
- package/lib/adaptor/common.js.map +1 -1
- package/lib/adaptor/pattern.d.ts +10 -0
- package/lib/adaptor/pattern.js +58 -0
- package/lib/adaptor/pattern.js.map +1 -0
- package/lib/core/plot.d.ts +11 -1
- package/lib/core/plot.js +41 -0
- package/lib/core/plot.js.map +1 -1
- package/lib/index.d.ts +5 -1
- package/lib/index.js +7 -1
- package/lib/index.js.map +1 -1
- package/lib/plots/area/adaptor.js +1 -1
- package/lib/plots/area/adaptor.js.map +1 -1
- package/lib/plots/circle-packing/adaptor.js +2 -2
- package/lib/plots/circle-packing/adaptor.js.map +1 -1
- package/lib/plots/circle-packing/utils.d.ts +0 -5
- package/lib/plots/circle-packing/utils.js +2 -22
- package/lib/plots/circle-packing/utils.js.map +1 -1
- package/lib/plots/column/adaptor.js +3 -2
- package/lib/plots/column/adaptor.js.map +1 -1
- package/lib/plots/dual-axes/adaptor.js +6 -4
- package/lib/plots/dual-axes/adaptor.js.map +1 -1
- package/lib/plots/dual-axes/types.d.ts +2 -1
- package/lib/plots/dual-axes/types.js.map +1 -1
- package/lib/plots/dual-axes/util/geometry.js +2 -1
- package/lib/plots/dual-axes/util/geometry.js.map +1 -1
- package/lib/plots/dual-axes/util/legend.js +4 -1
- package/lib/plots/dual-axes/util/legend.js.map +1 -1
- package/lib/plots/funnel/geometries/basic.js +1 -6
- package/lib/plots/funnel/geometries/basic.js.map +1 -1
- package/lib/plots/heatmap/adaptor.js +1 -1
- package/lib/plots/heatmap/adaptor.js.map +1 -1
- package/lib/plots/heatmap/types.d.ts +4 -2
- package/lib/plots/heatmap/types.js.map +1 -1
- package/lib/plots/histogram/adaptor.js +2 -1
- package/lib/plots/histogram/adaptor.js.map +1 -1
- package/lib/plots/liquid/adaptor.js +1 -1
- package/lib/plots/liquid/adaptor.js.map +1 -1
- package/lib/plots/pie/adaptor.js +2 -1
- package/lib/plots/pie/adaptor.js.map +1 -1
- package/lib/plots/radial-bar/adaptor.js +2 -1
- package/lib/plots/radial-bar/adaptor.js.map +1 -1
- package/lib/plots/rose/adaptor.js +1 -1
- package/lib/plots/rose/adaptor.js.map +1 -1
- package/lib/plots/sunburst/adaptor.js +1 -1
- package/lib/plots/sunburst/adaptor.js.map +1 -1
- package/lib/plots/tiny-area/adaptor.js +1 -1
- package/lib/plots/tiny-area/adaptor.js.map +1 -1
- package/lib/plots/tiny-column/adaptor.js +1 -1
- package/lib/plots/tiny-column/adaptor.js.map +1 -1
- package/lib/plots/treemap/adaptor.js +2 -1
- package/lib/plots/treemap/adaptor.js.map +1 -1
- package/lib/plots/venn/adaptor.d.ts +23 -0
- package/lib/plots/venn/adaptor.js +183 -0
- package/lib/plots/venn/adaptor.js.map +1 -0
- package/lib/plots/venn/constant.d.ts +7 -0
- package/lib/plots/venn/constant.js +36 -0
- package/lib/plots/venn/constant.js.map +1 -0
- package/lib/plots/venn/index.d.ts +24 -0
- package/lib/plots/venn/index.js +50 -0
- package/lib/plots/venn/index.js.map +1 -0
- package/lib/plots/venn/interaction/action.d.ts +18 -0
- package/lib/plots/venn/interaction/action.js +80 -0
- package/lib/plots/venn/interaction/action.js.map +1 -0
- package/lib/plots/venn/interaction/index.d.ts +1 -0
- package/lib/plots/venn/interaction/index.js +17 -0
- package/lib/plots/venn/interaction/index.js.map +1 -0
- package/lib/plots/venn/label.d.ts +1 -0
- package/lib/plots/venn/label.js +33 -0
- package/lib/plots/venn/label.js.map +1 -0
- package/lib/plots/venn/layout/circleintersection.d.ts +29 -0
- package/lib/plots/venn/layout/circleintersection.js +196 -0
- package/lib/plots/venn/layout/circleintersection.js.map +1 -0
- package/lib/plots/venn/layout/diagram.d.ts +17 -0
- package/lib/plots/venn/layout/diagram.js +201 -0
- package/lib/plots/venn/layout/diagram.js.map +1 -0
- package/lib/plots/venn/layout/layout.d.ts +27 -0
- package/lib/plots/venn/layout/layout.js +606 -0
- package/lib/plots/venn/layout/layout.js.map +1 -0
- package/lib/plots/venn/shape.d.ts +1 -0
- package/lib/plots/venn/shape.js +44 -0
- package/lib/plots/venn/shape.js.map +1 -0
- package/lib/plots/venn/types.d.ts +27 -0
- package/lib/plots/venn/types.js +4 -0
- package/lib/plots/venn/types.js.map +1 -0
- package/lib/plots/venn/utils.d.ts +28 -0
- package/lib/plots/venn/utils.js +98 -0
- package/lib/plots/venn/utils.js.map +1 -0
- package/lib/types/annotation.d.ts +5 -1
- package/lib/types/annotation.js.map +1 -1
- package/lib/types/attr.d.ts +4 -1
- package/lib/types/attr.js.map +1 -1
- package/lib/types/common.d.ts +3 -1
- package/lib/types/common.js.map +1 -1
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +1 -0
- package/lib/types/index.js.map +1 -1
- package/lib/types/pattern.d.ts +47 -0
- package/lib/types/pattern.js +3 -0
- package/lib/types/pattern.js.map +1 -0
- package/lib/utils/color/blend.d.ts +20 -0
- package/lib/utils/color/blend.js +98 -0
- package/lib/utils/color/blend.js.map +1 -0
- package/lib/utils/padding.d.ts +5 -0
- package/lib/utils/padding.js +21 -1
- package/lib/utils/padding.js.map +1 -1
- package/lib/utils/pattern/dot.d.ts +32 -0
- package/lib/utils/pattern/dot.js +74 -0
- package/lib/utils/pattern/dot.js.map +1 -0
- package/lib/utils/pattern/index.d.ts +17 -0
- package/lib/utils/pattern/index.js +31 -0
- package/lib/utils/pattern/index.js.map +1 -0
- package/lib/utils/pattern/line.d.ts +25 -0
- package/lib/utils/pattern/line.js +61 -0
- package/lib/utils/pattern/line.js.map +1 -0
- package/lib/utils/pattern/square.d.ts +28 -0
- package/lib/utils/pattern/square.js +66 -0
- package/lib/utils/pattern/square.js.map +1 -0
- package/lib/utils/pattern/util.d.ts +50 -0
- package/lib/utils/pattern/util.js +99 -0
- package/lib/utils/pattern/util.js.map +1 -0
- package/package.json +10 -8
- package/src/adaptor/common.ts +2 -0
- package/src/adaptor/pattern.ts +66 -0
- package/src/core/plot.ts +43 -1
- package/src/index.ts +8 -1
- package/src/plots/area/adaptor.ts +3 -2
- package/src/plots/circle-packing/adaptor.ts +12 -3
- package/src/plots/circle-packing/utils.ts +1 -21
- package/src/plots/column/adaptor.ts +3 -1
- package/src/plots/dual-axes/adaptor.ts +6 -4
- package/src/plots/dual-axes/types.ts +11 -1
- package/src/plots/dual-axes/util/geometry.ts +3 -2
- package/src/plots/dual-axes/util/legend.ts +4 -1
- package/src/plots/funnel/geometries/basic.ts +2 -10
- package/src/plots/heatmap/adaptor.ts +12 -1
- package/src/plots/heatmap/types.ts +6 -2
- package/src/plots/histogram/adaptor.ts +13 -1
- package/src/plots/liquid/adaptor.ts +2 -2
- package/src/plots/pie/adaptor.ts +2 -0
- package/src/plots/radial-bar/adaptor.ts +2 -0
- package/src/plots/rose/adaptor.ts +2 -1
- package/src/plots/sunburst/adaptor.ts +2 -1
- package/src/plots/tiny-area/adaptor.ts +2 -2
- package/src/plots/tiny-column/adaptor.ts +2 -2
- package/src/plots/treemap/adaptor.ts +13 -1
- package/src/plots/venn/adaptor.ts +227 -0
- package/src/plots/venn/constant.ts +35 -0
- package/src/plots/venn/index.ts +47 -0
- package/src/plots/venn/interaction/action.ts +67 -0
- package/src/plots/venn/interaction/index.ts +17 -0
- package/src/plots/venn/label.ts +26 -0
- package/src/plots/venn/layout/circleintersection.ts +223 -0
- package/src/plots/venn/layout/diagram.ts +219 -0
- package/src/plots/venn/layout/layout.ts +734 -0
- package/src/plots/venn/shape.ts +53 -0
- package/src/plots/venn/types.ts +25 -0
- package/src/plots/venn/utils.ts +100 -0
- package/src/types/annotation.ts +3 -1
- package/src/types/attr.ts +7 -1
- package/src/types/common.ts +3 -1
- package/src/types/index.ts +1 -0
- package/src/types/pattern.ts +50 -0
- package/src/utils/color/blend.ts +103 -0
- package/src/utils/padding.ts +20 -0
- package/src/utils/pattern/dot.ts +86 -0
- package/src/utils/pattern/index.ts +45 -0
- package/src/utils/pattern/line.ts +70 -0
- package/src/utils/pattern/square.ts +78 -0
- 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
|