这一整天我都尝试为我的用例做这个(https://bl.ocks.org/mbostock/7607535)工作。在前面提到的页面中,作者将数据保存为JSON,但我希望允许用户对JSON进行更改。
我正在构建一个工具,允许用户将他或她感兴趣的国家/地区输入到表单中。我发出ajax请求以获取此表单的信息,然后将其传递给Flask,以便Flask可以查询我的Cassandra数据库。一旦Flask查询我的Cassandra数据库,它就会将信息发送回我的AJAX请求。一旦我掌握了AJAX中的信息,我就会尝试更新当前的字典,以便用户选择的国家/地区显示另一个气泡,但这不起作用。我能够为第一个条目制作正确的图表,但是一旦用户输入第二个条目,我的图表就会搞砸了。要查看发生的情况,请在此处查看我的网站:http://ec2-52-27-239-181.us-west-2.compute.amazonaws.com/overview
这是我目前的代码:
function add_simple(json,dict_country){ //updates the dictionary
function event_children(event_dict){ //returns a list with event children
var time_lst = []
var event_names = Object.keys(event_dict)
for (var i = 0; i<event_names.length; i++){
var event_type = {}
event_type["name"] = event_names[i]
event_type["children"] = []
event_info = event_dict[event_names[i]]
for (var key in event_info){
key_obj = {}
key_obj["name"] = key + " => " + event_info[key]
key_obj["size"] = 2000
event_type["children"].push(key_obj)
}
time_lst.push(event_type)
}
return time_lst
}
var country = dict_country["Country"]
var timeframe = dict_country["Timeframe"]
var date = dict_country["Date"]
var total = dict_country["Total"]
var info = dict_country["Overview"]
json["children"].push({"name":country,"children":event_children(info)})
return json
}
var poot = {"name":"flare","children":[]}
$( "#target" ).click(function() {
var data = {};
data.country = $("#country").val();
data.timeframe = $("#timeframe").val();
data.eventdate = $("#eventdate").val();
$.ajax({
type : "POST",
url : "/overview",
data: JSON.stringify(data, null, '\t'),
contentType: 'application/json;charset=UTF-8',
success: function(result) {
poot = add_simple(poot,result["result"])
console.log(poot)
createCircles(poot);
}
});
})
var svg = d3.select("svg"),
margin = 20,
diameter = +svg.attr("width"),
g = svg.append("g").attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
var color = d3.scaleLinear()
.domain([-1, 5])
.range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
.interpolate(d3.interpolateHcl);
var pack = d3.pack()
.size([diameter - margin, diameter - margin])
.padding(2);
function createCircles(root){
root = d3.hierarchy(root)
.sum(function(d) { return d.size; })
.sort(function(a, b) { return b.value - a.value; });
var focus = root,
nodes = pack(root).descendants(),
view;
var circle = g.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
.style("fill", function(d) { return d.children ? color(d.depth) : null; })
.on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });
var text = g.selectAll("text")
.data(nodes)
.enter().append("text")
.attr("class", "label")
.style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; })
.style("display", function(d) { return d.parent === root ? "inline" : "none"; })
.text(function(d) { return d.data.name; });
var node = g.selectAll("circle,text");
svg
.style("background", color(-1))
.on("click", function() { zoom(root); });
zoomTo([root.x, root.y, root.r * 2 + margin]);
function zoom(d) {
var focus0 = focus; focus = d;
var transition = d3.transition()
.duration(d3.event.altKey ? 7500 : 750)
.tween("zoom", function(d) {
var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
return function(t) { zoomTo(i(t)); };
});
transition.selectAll("text")
.filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
.style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
.on("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
.on("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
}
function zoomTo(v) {
var k = diameter / v[2]; view = v;
node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
circle.attr("r", function(d) { return d.r * k; });
}
}
//});
我非常感谢任何帮助。