-
Notifications
You must be signed in to change notification settings - Fork 2
/
area_coverage_mingwei.js
29 lines (29 loc) · 996 Bytes
/
area_coverage_mingwei.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function areaCoverage(labelDoms){
// labelDoms - a list of DOM elements for the labels.
// If you are using d3, it is yourLabelSelection.nodes(), see:
// https://github.com/d3/d3-selection/blob/v2.0.0/README.md#selection_nodes
// d.getBoundingClientRect() gives you the bounding box (={x, y, width, height, ...}) of the label element in pixels, and it automatically considers the current zoom level of the view.
let labelArea = 0;
let xmin = +Infinity,
xmax = -Infinity,
ymin = +Infinity,
ymax = -Infinity;
for(let d of labelDoms){
let bbox = d.getBoundingClientRect();
labelArea += bbox.width * bbox.height;
if(bbox.x < xmin){
xmin = bbox.x;
}
if(bbox.x + bbox.width > xmax){
xmax = bbox.x + bbox.width;
}
if(bbox.y < ymin){
ymin = bbox.y;
}
if(bbox.y + bbox.height > ymax){
ymax = bbox.y + bbox.height;
}
}
let boundingArea = (ymax - ymin) * (xmax - xmin);
return labelArea / boundingArea;
}