public
anonymous / gist:2142346
Created

redraw

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
var data = []
var gChild;
 
data.push([50,50]);
data.push([50,80]);
data.push([50,110]);
data.push([50,140]);
 
drawGraph();
 
 
 
 
 
function drawGraph()
{
chart = d3.select("body")
.append("svg:svg")
.attr("width", 400)
.attr("height", 400)
 
//create a parent g container
var gParent = chart.append("svg:g") //g container
.attr("class", "gParent");
 
 
//create a nested g container
gChild = gParent.append("svg:g") //g container
.attr("class", "gChild");
 
//create a g container
var gBaby = gChild.selectAll("g.gBaby")
.data(data)
.enter().append("svg:g")
.attr("class", "gBaby")
.on("click", function(d, i)
{
///data.splice(i,1)
data.push(50,(i*110))
redraw();
});
 
//append
gBaby.append("svg:rect")
.style("fill", "blue")
.attr("x",function(p){return p[0]})
.attr("y",function(p){return p[1]})
.attr("height", 20)
.attr("width", 100);
 
//append
gBaby.append("text")
.attr("x",function(p){return p[0]})
.attr("y",function(p){return p[1]})
.text(function(d,i) { return "test " + i; });
}
 
function redraw()
{
var container = gChild.selectAll("g")
.data(data)
container.enter().append("g")
.attr("class", "gBaby")
 
container.exit()
.remove();
//redraw textbox
var box = gChild.selectAll("rect")
.data(data)
box.enter().append("rect")
 
box.transition()
.duration(1000)
.attr("x",function(p){return p[0]})
.attr("y",function(p){return p[1]})
box.exit()
.remove();
//redraw text
var text = gChild.selectAll("text")
.data(data)
text.enter().append("text")
 
text.transition()
.duration(1000)
.attr("x",function(p){return p[0]})
.attr("y",function(p){return p[1]})
text.exit()
.remove();
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.