<html>
<head>
<meta name="description" content="Rename tree and get underlying data to push to server." />
<script src="http://d3js.org/d3.v3.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<script>
var money;
</script>
</body>
</html>
.node circle {
fill: red;
}
var data = {
"name": "Blog",
"children": [
{
"name": 'Sylvia',
"children": [
{"name": "Tom"},
{"name": "Jeft"},
{"name": "Buffy"}
]
},
{
"name": "David",
"children": [
{"name": "Jeft"},
{
"name": "Buffy",
"children": [
{'name': "test"}
]
}
]
}
]
};
var canvas = d3.select("body").append('svg')
.attr("width",500)
.attr("height",500)
.append('g')
.attr("transform", "translate(50,50)");
var draw = (function() {
var tree = d3.layout.tree()
.size([400,300]);
// Let D3 convert JSON into d3.node array
var nodes = tree.nodes(data);
var linkedNodes = tree.links(nodes); //Creates a start and end
// Add all the nodes
var node = canvas.selectAll('.node')
.data(nodes);
node
.enter()
.append('g')
.attr('class','node')
.attr('transform', function(d){ return "translate(" + d.y + "," + d.x + ")"; });
// Add circle
node.append("circle")
.attr('r', 5)
.attr("fill", 'green');
// Add text
node.append("text")
.text(function(d){ return d.name; })
.on('click', function(d){
var result = prompt('Change the name of the node',d.name);
if(result) {
d.name = result;
var node1 = canvas.selectAll('.node').data(nodes);
node1.select('text')
.text(function(d){ return d.name; });
console.log(filter(node1.data()[0]));
// This will give the data as put into the function a the top. Now you only need to loop through the array to remove the additional attributes d3 has added.
money = node1.data()[0];
}
});
// Filter funciton
function filter(data) {
for(var i in data){
if(["depth","x","x0","y","y0","parent","size"].indexOf(i) != -1){
delete data[i];
} else if (i === "children") {
for (var j in data.children) {
data.children[j] = filter(data.children[j])
}
}
}
return data;
}
// Setup connection
var diagonal = d3.svg.diagonal()
.projection(function(d){return [d.y, d.x];});
// Create links
var links = canvas.selectAll('.links')
.data(linkedNodes)
.enter()
.append("path")
.attr("class", "links")
.attr("fill", 'none')
.attr("stroke",'grey')
.attr('d', diagonal);
return 'done';
});
draw();
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |