我有一个带有矩形的数组。我有另一个数组,其中的矩形重叠。我花了几个小时试图弄清楚如何循环遍历数组并找到重叠记录的新 y 位置,我脑子里有一个大错误......
我只能在 y 轴上移动,因为 x 取决于日期刻度。我真的很感激一些代码示例。矩形的大小不相等。
数据示例:
"people": [
{ "firstName":"John" , "startDate":"2012-01-01", "endDate":"2014-01-01", "basketValue":"10"},
{ "firstName":"Anna" , "startDate":"2011-01-01", "endDate":"2013-04-01", "basketValue":"20" },
{ "firstName":"Victor" , "startDate":"2011-01-01", "endDate":"2013-04-01", "basketValue":"13" },
{ "firstName":"Tom" , "startDate":"2011-01-01", "endDate":"2012-07-01", "basketValue":"20" },
{ "firstName":"Santa" , "startDate":"2011-01-01", "endDate":"2012-12-24", "basketValue":"20" },
{ "firstName":"Peter" , "startDate":"2012-01-01", "endDate":"2012-02-21", "basketValue":"4" }
{ "firstName":"Carol" , "startDate":"2013-01-01", "endDate":"2013-07-05", "basketValue":"14" }
{ "firstName":"Sophie" , "startDate":"2012-09-01", "endDate":"2012-12-24", "basketValue":"8" }
]
while(loop){
//overlappingRects array with those that overlaps
newY= overlappingRects[0].y+overlappingRects[0].barHeight + newY;
log(newY);
//this my logic error arrRec holds all of the recs
for(j=0;j<arrRec.length;j++){
if(arrRec[j].Name!==overlappingRects[0].Name){
log(overlappingRects[0].Name + ' ' + arrRec[j].Name);
//How do I solve this that it not overlap with the other surounding rects
overlap = rectOverlap(overlappingRects[0],arrRec[j]);
if(overlap==false){
//check for date...
overlappingRects[0].y = arrRec[j].y;
overlappingRects[0].endY = overlappingRects[0].barHeight + overlappingRects[0].y;
arrRec[overlappingRects[0].key].y =overlappingRects[0].y;
arrRec[overlappingRects[0].key].endY=overlappingRects[0].endY;
overlappingRects.splice(0,1);
break;
}
}
}
}
if(overlappingRects.length==0 ){loop=false;}
}
我在我的保管箱上提供了一个关于其外观的文件:-((不允许在此处共享)
最佳答案
假设矩形表示如下:
{ "position": {"x": <x coord>, "y": <y coord>},
"width": <pixel value>,
"height": <pixel value>
}
算法应该很简单:
- 根据左上角对矩形进行排序。
- 从第二个矩形开始,检查它是否与前一个矩形重叠
- 如果重叠,则将矩形坐标的 y 值更改为消除重叠所需的最小值
- 重复直到最后一个矩形位置固定。
重叠的测试很简单:
previous.position.y + previous.height > current.position.y
您可以用类似的方式更改位置:
current.position.y = previous.position.y + previous.height + gap
其中gap是行之间间隙的像素大小。
对我来说,您可以通过以下方式优化甘特图(对我来说似乎是Gantt chart):
bar.sort(function(a,b) {
var y = (a.position.y - b.position.y);
return y?y:a.position.x - b.position.x;
});
for (i=1; i<bar.length; i++) {
bar[i].position.y = bar[i-1].position.y + bar[i-1].height + gap;
}
这是第一次尝试,该算法的问题是每行只有一个矩形。为了获得紧凑的 View ,我们需要考虑矩形的 x 位置。
我们可以把事情复杂化一点:
var gap = {"x": 2, "y": 5};
bar.sort(function(a,b) {
var y = (a.position.y - b.position.y);
return y?y:a.position.x - b.position.x;
});
for (i=1; i<bar.length; i++) {
for (j = i-1; j>=0; j--) {
safe = {
"y": bar[j].position.y + bar[j].height + gap.y,
"x": bar[j].position.x + bar[j].width + gap.x
};
if (bar[i].position.y <= safe.y) { // the rects can overlap
if (bar[i].position.x <= safe.x) { // the rects do overlap
bar[i].position.y = safe.y;
}
}
}
}
免责声明:我尚未测试代码,但它应该可以消除最终的语法错误和故障。
关于javascript - 数组中重叠的矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24029354/