d3 を使用してビジュアライゼーションを作成しています。これには、上に 60 個、下に 60 個の長方形があります。各長方形には、約 100 ~ 150 個の円が含まれます。データは WebSocket 経由でストリーミングされます。フィルタリングを行わないと、更新に非常に時間がかかります。選択的な更新によって最適化しようとしています。ただし、.filter() を完全には理解していないようです。これは、長方形を扱うコードの部分への jsFiddle です。
関数 updateRect の js コメントからわかるように、いくつかのアプローチを試しました。1. .data 内のフィルタリング 2. ID を作成し、ID に対して selectAll を実行します。
しかし、どちらのアプローチも機能しません。
私が行方不明または間違っていることについて何か助けはありますか?
var bottom_rects = 1;
var top_rects = 1;
var rects = [];
function generate_rect(){
var created = '';
// 6(*10) top rectangles & 6(*10) bottom rectangles
if(bottom_rects <= top_rects-1) {
new_rect = {"name":"rect_bottom_"+bottom_rects,"location":"bottom","minx":0,"miny":Math.floor((2*h)/3),"maxx":0,"maxy":h};
rects.push(new_rect);
//fix x values
var step = Math.floor(w/bottom_rects);
var x = 0;
for (var i = 0; i < rects.length; i++) {
var item = rects[i];
if (item.location == "bottom"){
item.minx = x;
item.maxx = x+step;
x=x+step;
}
}
bottom_rects++;
created = "bottom";
} else {
new_rect = {"name":"rect_top_"+top_rects,"location":"top","minx":0,"miny":0,"maxx":0,"maxy":Math.floor(h/3)};
rects.push(new_rect);
//fix x values
var step = Math.floor(w/top_rects);
var x = 0;
for (var i = 0; i < rects.length; i++) {
var item = rects[i];
if (item.location == "top"){
item.minx = x;
item.maxx = x+step;
x=x+step;
}
}
top_rects++;
created = "top";
}
updateRect(created);
}
var w = 1200,
h = 760;
var vis = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h);
function updateRect(location) {
//var allrects = vis.selectAll("rect")
var allrects = vis.selectAll(".rect_"+location)
//.data(rects.filter(function(d) { return d.location == location; }))
.data(rects)
//update
.attr("x", function(d) { return d.minx; })
.attr("y", function(d) { return d.miny; })
.attr("width",function(d) { return d.maxx-d.minx; })
.attr("height", function(d) { return d.maxy-d.miny; })
//create
.enter().append("rect")
.attr("x", function(d) { return d.minx; })
.attr("y", function(d) { return d.miny; })
//.attr("id", "rect_"+location)
.attr("width",function(d) { return d.maxx-d.minx; })
.attr("height", function(d) { return d.maxy-d.miny; })
.attr("fill", "blue")
.attr("stroke", "black")
.style("stroke-width", 2)
.style("fill-opacity", 0.2)
.exit().remove();
}
for(var i =0; i < 12; i++) setTimeout(generate_rect,(i+1)*200);