我正在使用d3气泡图。
http://bl.ocks.org/mbostock/4063269
在此插件中,当我点击圆圈时,需要为特定圆圈内容渲染数据。
我在下面添加了mycode:
DATA1:
var data1 = '{ "name": "flare", "children": [ { "name": "rcccccccccc", "children": [ { "name": "rrrrrrrrrrrrrrrR", "size": 1000 } ] }, { "name": "crcccccccccc", "children": [ { "name": "crrrrrrrrrrrrrrrR", "size": 4116 } ] }, { "name": "c", "children": [ { "name": "rrrrrrrrrrrrrrrRc", "children": [ { "name": "CC", "size": 6703 } ] } ] }, { "name": "rccccccccccz", "children": [ { "name": "zrrrrrrrrrrrrrrrR", "size": 6703 } ] } ] }';
点击圆圈渲染数据2:
var data2 = '{ "name": "flare", "children": [ { "name": "1111111",
"children": [ { "name": "11111111", "size": 1000 } ] }, { "name": "1111111", "children": [ { "name": "zzzzzzzzzzz", "size": 4116 } ] }, { "name": "c", "children": [ { "name": "zzzzzzzzzzz", "children": [ { "name": "CC", "size": 6703 } ] } ] }, { "name": "zzzzzzzzzzz", "children": [ { "name": "zzzzzzzzzzz", "size": 6703 } ] } ] }';
代码:
var data1 = JSON.parse(data1);
var data2 = JSON.parse(data2);
console.log(data2)
var diameter = 960,
format = d3.format(",d"),
color = d3.scale.category20c();
var diameter = 960, format = d3.format(",d"), color = d3.scale.category20c();
var bubble = d3.layout.pack().sort(null).size([diameter, diameter]).padding(1.5);
var svg = d3.select("body").append("svg").attr("width", diameter).attr("height", diameter).attr("class", "bubble");
update(data1);
// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
var classes = [];
function recurse(name, node) {
if (node.children)
node.children.forEach(function(child) {
recurse(node.name, child);
});
else
classes.push({
packageName : name,
className : node.name,
value : node.size
});
}
recurse(null, root);
return {
children : classes
};
}
d3.select(self.frameElement).style("height", diameter + "px");
function update(root) {
var node = svg.selectAll(".node").data(bubble.nodes(classes(root)).filter(function(d) {
return !d.children;
})).enter().append("g").attr("class", "node").attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
node.append("title").text(function(d) {
return d.className + ": " + format(d.value);
});
node.append("circle").attr("r", function(d) {
return d.r;
}).style("fill", function(d) {
return color(d.packageName);
}).on("click", function(d) {
alert('rl');
console.log(data2);
update(data2);
});
;
node.append("text").attr("dy", ".3em").style("text-anchor", "middle")
//.text(function(d) { return d.className.substring(0, d.r / 3); });
.text(function(d) {
return d.className.substring(0, d.r / 3);
});
}
当我点击圆圈时,新数据(data2)需要在现有圆圈中更新。
我在Jsfiddle中添加了我的代码。 http://jsfiddle.net/uXn2p/150/
答案 0 :(得分:0)
在插入新数据时,代码中没有删除旧数据,强制更新。
这是一个快速工作的代码
var data1json = '{ "name": "flare", "children": [ { "name": "rcccccccccc", "children": [ { "name": "rrrrrrrrrrrrrrrR", "size": 1000 } ] }, { "name": "crcccccccccc", "children": [ { "name": "crrrrrrrrrrrrrrrR", "size": 4116 } ] }, { "name": "c", "children": [ { "name": "rrrrrrrrrrrrrrrRc", "children": [ { "name": "CC", "size": 6703 } ] } ] }, { "name": "rccccccccccz", "children": [ { "name": "zrrrrrrrrrrrrrrrR", "size": 6703 } ] } ] }';
var data1 = JSON.parse(data1json);
var data2json = '{ "name": "flare2", "children": [ { "name": "test", "children": [ { "name": "11111111", "size": 1000 } ] }, { "name": "test1", "children": [ { "name": "test2", "size": 4116 } ] }, { "name": "test3", "children": [ { "name": "zzzzzzzzzzz", "children": [ { "name": "test4", "size": 6703 } ] } ] }, { "name": "test5", "children": [ { "name": "test6", "size": 6703 } ] } ] }';
var data2 = JSON.parse(data2json);
console.log(data2)
var diameter = 960,
format = d3.format(",d"),
color = d3.scale.category20c();
var bubble = d3.layout.pack()
.sort(null)
.size([diameter, diameter])
.padding(1.5);
var svg = d3.select("body").append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "bubble");
update(data1);
// Returns a flattened hierarchy containing all leaf nodes under the root.
function classes(root) {
var classes = [];
function recurse(name, node) {
if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
else classes.push({packageName: name, className: node.name, value: node.size});
}
recurse(null, root);
return {children: classes};
}
d3.select(self.frameElement).style("height", diameter + "px");
function update (data){
svg.selectAll(".node").remove();
var node = svg.selectAll(".node")
.data(bubble.nodes(classes(data))
.filter(function(d) { return !d.children; }))
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
node.append("title")
.text(function(d) { return d.className + ": " + format(d.value); });
node.append("circle")
.attr("r", function(d) { return d.r; })
.style("fill", function(d) { return color(d.packageName); })
.on("click", function(d) {
alert('update');
console.log(data2);
update(data2);
});
node.append("text")
.attr("dy", ".1em")
.style("text-anchor", "middle")
//.text(function(d) { return d.className.substring(0, d.r / 3); });
.text(function(d) { return d.className.substring(0, d.r / 3); });
}
这是相应的小提琴:http://jsfiddle.net/uXn2p/151/