Here is my D3js code
function ShowGraph(data)
{
var w = 600,
h = 600,
padding = 36,
p = 31,
barwidth = 1;
var bar_height = d3.scale.linear()
.domain([d3.max(data, function(d) { return d.count; }), 0] ) // min max of count
.range([p, h-p]);
var bar_xpos = d3.scale.linear()
.domain([1580, d3.max(data, function(d) { return d.year; })] ) // min max of year
.range([p,w-p]);
var xAxis = d3.svg.axis()
.scale(bar_xpos)
.orient("bottom")
.ticks(5); //Set rough # of ticks
var yAxis = d3.svg.axis()
.scale(bar_height)
.orient("left")
.ticks(5);
var svg = d3.select("#D3line").append("svg")
.attr("width", w)
.attr("height", h);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
.call(yAxis);
svg.selectAll("rect")
.data(data)
.enter().append("rect")
.attr("class","bar")
.attr("x", function(d) {
return bar_xpos(d.year); })
.attr("y", function(d) {
return bar_height(d.count); })
.attr("width", barwidth)
.attr("height", function(d) {return h - bar_height(d.count) - padding; })
.attr("fill", "steelblue")
}
i run the above code at the click of a button.When i click the button once the graph displays but if click it once again it shows another graph.So now i get 2 graphs.If i click once more i get 3.All i want is to update the existing graph instead of duplicating the graphs.
Here is my D3js code
function ShowGraph(data)
{
var w = 600,
h = 600,
padding = 36,
p = 31,
barwidth = 1;
var bar_height = d3.scale.linear()
.domain([d3.max(data, function(d) { return d.count; }), 0] ) // min max of count
.range([p, h-p]);
var bar_xpos = d3.scale.linear()
.domain([1580, d3.max(data, function(d) { return d.year; })] ) // min max of year
.range([p,w-p]);
var xAxis = d3.svg.axis()
.scale(bar_xpos)
.orient("bottom")
.ticks(5); //Set rough # of ticks
var yAxis = d3.svg.axis()
.scale(bar_height)
.orient("left")
.ticks(5);
var svg = d3.select("#D3line").append("svg")
.attr("width", w)
.attr("height", h);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
.call(yAxis);
svg.selectAll("rect")
.data(data)
.enter().append("rect")
.attr("class","bar")
.attr("x", function(d) {
return bar_xpos(d.year); })
.attr("y", function(d) {
return bar_height(d.count); })
.attr("width", barwidth)
.attr("height", function(d) {return h - bar_height(d.count) - padding; })
.attr("fill", "steelblue")
}
i run the above code at the click of a button.When i click the button once the graph displays but if click it once again it shows another graph.So now i get 2 graphs.If i click once more i get 3.All i want is to update the existing graph instead of duplicating the graphs.
Share Improve this question asked Nov 24, 2012 at 19:13 iJadeiJade 23.9k58 gold badges161 silver badges250 bronze badges2 Answers
Reset to default 4Here you always append a new SVG element (.append('svg')
):
var svg = d3.select("#D3line").append("svg")
.attr("width", w)
.attr("height", h);
So instead of using a new SVG element (and thus a new graph), just maintain a link to the first selected graph and override it or select the SVG again:
var svg = d3.select( '#D3line svg' );
if ( !svg ) {
svg = d3.select("#D3line").append("svg")
.attr("width", w)
.attr("height", h);
}
Or you clear all content of you element, where the SVG resides:
document.querySelector( '#D3line' ).innerHTML = '';
You can also remove your old svg before appending the new one..
d3.select("#D3line").selectAll("svg").remove();
var svg = d3.select("#D3line").append("svg")
.attr("width", w)
.attr("height", h);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744864332a4597888.html
评论列表(0条)