我正在尝试修改Mike Bostok的可缩放圈子包装https://bl.ocks.org/mbostock/7607535 所以它使用CSV文件而不是json文件;不可缩放版本的s: https://bl.ocks.org/mbostock/ca5b03a33affa4160321
从高速数据库(SAP HANA)返回数据。返回格式化的json要比返回模仿CSV的格式复杂得多,因此我将D3 Packed Circles(使用CSV格式数据)的D3示例与D3 Zoomable Packed Circle(使用json格式数据)合并。我的问题开始了。我是D3的新手。
在调用zoomTo()之后,我无法合并这两个版本以使zoom()和zoomTo()函数正常工作。这与root或node的定义有关 - 但是我无法弄清楚“node”zoomTo()的定义是如何定义的
尝试合并的jsfiddle在这里: https://jsfiddle.net/LbLpcjr4/17/
我注释掉了zoomTo() - 取消注释这一行以查看我正在尝试解决的问题:
//zoomTo(nodecircles,[root.x, root.y, root.r * 2 + margin]);
Thsi使用GDELT的数据显示2017年跨国公司的新闻文章数量,按国家/地区分组。
任何建议表示赞赏!
答案 0 :(得分:0)
到目前为止,我设法修复了缩放,但我打破了居中:
脏了
public static void main(String[] args) throws SQLException, IOException {
String url = "jdbc:sqlite:C:/test/sms2.db";
Connection conn = DriverManager.getConnection(url);
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT handle_id,is_from_me,text FROM message");
File f = new File("C:\\Users\\theda\\Documents\\Messages\\convo\\+1**********.html");
if(!f.exists())
f.createNewFile();
FileWriter w = new FileWriter(f);
WriterOutputStream write = new WriterOutputStream(w, Charset.forName("utf8"));
w.append("<link rel='stylesheet' type='text/css' href='message.css'>");
w.append("<body>");
while(rs.next()){
if(rs.getInt("handle_id") == 3){
String st = " ";
String fromMe = " ";
if(rs.getInt("is_from_me") == 1)
fromMe = "true";
else
fromMe = "false";
st = "<div class='message' data-outgoing='" + fromMe + "'>" + rs.getString("text") + "</div><br>";
System.out.println(st);
w.append(st);
}
}
w.append("</body>");
w.close();
}
第114行它“修复”它
https://jsfiddle.net/LbLpcjr4/39/
我宣布svg.select("g").attr("transform","translate("+(width/2)+","+(height/2)+")")
和nodecircles
并将circle
更改为:
zoomTo
答案 1 :(得分:0)
我对此有一些经验。 我留下我的项目代码(在xampp中执行)
<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
text-anchor: middle;
}
.node--hover circle {
stroke: #000;
stroke-width: 1.2px;
}
.node {
cursor: pointer;
}
.node:hover {
/* stroke: #000;
stroke-width: 0.5px; */
opacity: 0.3;
}
.node--leaf {
fill: #fff;
}
.label {
font: 12px "Maison Neue", Helvetica, Arial, sans-serif;
text-anchor: middle;
/* text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff; */
}
.label,
.node--root,
.node--leaf {
pointer-events: none;
}
</style>
<svg width="960" height="960"><g transform="translate(1,1)"></g></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
margin = 1,
diameter = +svg.attr("width"),
g = svg.append("g").attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
var format = d3.format(",d");
var color = d3.scaleLinear()
.domain([-1, 10])
.range(["rgb(200,80,77)", "rgb(245,255,255)"])
.interpolate(d3.interpolateHcl);
var stratify = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
var pack = d3.pack()
.size([width - margin, height - margin])
.padding(1);
d3.csv("./Visualise.csv", function(error, data) {
if (error) throw error;
var root = stratify(data)
.sum(function(d) { return d.value; })
.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) {
if(d.data.value == 0){
return color(d.depth-1);
}
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) {
if(d.data.value == 0){return 0;}
return d.parent === root ? 1 : 0; })
.style("display", function(d) {
if( d.data.value == 0 || d.data.value == "" ) {return "none";}
return d.parent === root ? "inline" : "none"; })
.text(function(d) {
if(d.data.value == 0){return;};
return d.data.id.split(".").pop(-1) + "\n:\n"+ format(d.data.value); });//////
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; });
}
});
</script>
//////visualise.csv
level,CategoryID,CategoryParentID,value,CategoryCount,id
0,-1,0,10000000,1000000,Cat
1,1,-1,263752,173805,Cat.egory_1
2,12,20081,0,0,Cat.egory_29.Leaf_1
3,13,50693,2470,337,Cat.egory_13.Leaf_2.Leaf_1
4,14,13,0,0,Cat.egory_13.Leaf_2.Leaf_1.Leaf_1
5,18,2735,0,0,Cat.egory_13.Leaf_2.Leaf_1.Leaf_11.Leaf_1
5,22,2705,0,0,Cat.egory_13.Leaf_2.Leaf_1.Leaf_8.Leaf_1
5,23,1020,0,0,Cat.egory_13.Leaf_2.Leaf_1.Leaf_5.Leaf_1
3,24,18875,3017,440,Cat.egory_13.Leaf_1.Leaf_1
4,25,24,0,0,Cat.egory_13.Leaf_1.Leaf_1.Leaf_1
4,26,24,0,0,Cat.egory_13.Leaf_1.Leaf_1.Leaf_2
3,27,18875,1173,227,Cat.egory_13.Leaf_1.Leaf_2
3,28,1430,0,0,Cat.egory_1.Leaf_21.Leaf_1
3,30,717,0,0,Cat.egory_3.Leaf_5.Leaf_1
3,31,386,5,5,Cat.egory_4.Leaf_2.Leaf_1
4,32,326,0,0,Cat.egory_4.Leaf_1.Leaf_1.Leaf_1
3,33,940,0,0,Cat.egory_1.Leaf_16.Leaf_1
2,34,1,3527,811,Cat.egory_1.Leaf_1
4,35,13623,0,0,Cat.egory_1.Leaf_1.Leaf_13.Leaf_1
3,36,34,616,84,Cat.egory_1.Leaf_1.Leaf_1
5,37,35692,0,0,Cat.egory_1.Leaf_1.Leaf_1.Leaf_16.Leaf_1
5,38,13600,0,0,Cat.egory_1.Leaf_1.Leaf_1.Leaf_13.Leaf_1
3,39,34,677,221,Cat.egory_1.Leaf_1.Leaf_2
4,40,39,0,0,Cat.egory_1.Leaf_1.Leaf_2.Leaf_1
5,41,156204,0,0,Cat.egory_1.Leaf_1.Leaf_2.Leaf_2.Leaf_1
6,42,35672,0,0,Cat.egory_1.Leaf_1.Leaf_2.Leaf_2.Leaf_20.Leaf_1
3,44,593,64,64,Cat.egory_1.Leaf_11.Leaf_1
4,48,14281,0,0,Cat.egory_1.Leaf_35.Leaf_2.Leaf_1
2,51,64482,2797,323,Cat.egory_33.Leaf_1
3,52,51,0,0,Cat.egory_33.Leaf_1.Leaf_1
4,53,27259,0,0,Cat.egory_33.Leaf_1.Leaf_10.Leaf_1
3,56,51,0,0,Cat.egory_33.Leaf_1.Leaf_2
2,57,45100,388,132,Cat.egory_31.Leaf_1
3,58,57,0,0,Cat.egory_31.Leaf_1.Leaf_1
4,59,32987,0,0,Cat.egory_31.Leaf_1.Leaf_5.Leaf_1
3,60,57,1,1,Cat.egory_31.Leaf_1.Leaf_2
5,61,108726,0,0,Cat.egory_31.Leaf_1.Leaf_4.Leaf_8.Leaf_1
2,63,1,15791,5506,Cat.egory_1.Leaf_2
3,64,63,0,0,Cat.egory_1.Leaf_2.Leaf_1
3,65,63,0,0,Cat.egory_1.Leaf_2.Leaf_2
3,66,63,175,52,Cat.egory_1.Leaf_2.Leaf_3
4,67,66,0,0,Cat.egory_1.Leaf_2.Leaf_3.Leaf_1
4,68,66,78,78,Cat.egory_1.Leaf_2.Leaf_3.Leaf_2
4,69,66,0,0,Cat.egory_1.Leaf_2.Leaf_3.Leaf_3
4,70,66,34,34,Cat.egory_1.Leaf_2.Leaf_3.Leaf_4
4,71,66,0,0,Cat.egory_1.Leaf_2.Leaf_3.Leaf_5
3,73,63,2002,144,Cat.egory_1.Leaf_2.Leaf_4
4,74,73,0,0,Cat.egory_1.Leaf_2.Leaf_4.Leaf_1
5,75,32723,0,0,Cat.egory_1.Leaf_2.Leaf_4.Leaf_8.Leaf_1
4,76,73,1,1,Cat.egory_1.Leaf_2.Leaf_4.Leaf_2
4,77,900,0,0,Cat.egory_1.Leaf_2.Leaf_7.Leaf_1
3,79,63,6,6,Cat.egory_1.Leaf_2.Leaf_5
3,80,63,447,447,Cat.egory_1.Leaf_2.Leaf_6
3,81,13905,2115,145,Cat.egory_1.Leaf_32.Leaf_1
4,82,81,0,0,Cat.egory_1.Leaf_32.Leaf_1.Leaf_1
6,83,38140,0,0,Cat.egory_1.Leaf_32.Leaf_1.Leaf_4.Leaf_3.Leaf_1
5,84,4049,0,0,Cat.egory_1.Leaf_32.Leaf_1.Leaf_5.Leaf_1
4,86,27,0,0,Cat.egory_13.Leaf_1.Leaf_2.Leaf_1
4,87,24,34,34,Cat.egory_13.Leaf_1.Leaf_1.Leaf_3
4,88,24,0,0,Cat.egory_13.Leaf_1.Leaf_1.Leaf_4
4,89,27,64,64,Cat.egory_13.Leaf_1.Leaf_2.Leaf_2
4,90,27,21,21,Cat.egory_13.Leaf_1.Leaf_2.Leaf_3
4,91,24,1,1,Cat.egory_13.Leaf_1.Leaf_1.Leaf_5
4,92,24,139,139,Cat.egory_13.Leaf_1.Leaf_1.Leaf_6
4,93,24,12,12,Cat.egory_13.Leaf_1.Leaf_1.Leaf_7
4,95,24,10,10,Cat.egory_13.Leaf_1.Leaf_1.Leaf_8
4,96,27,155,155,Cat.egory_13.Leaf_1.Leaf_2.Leaf_4
5,97,13779,0,0,Cat.egory_1.Leaf_29.Leaf_37.Leaf_29.Leaf_1