我有一个条形图,我一次显示4个条形图。当我按下下一个按钮时,会显示下面的4个条。但x轴上的标签正在被覆盖。
这是我的小提琴:FIDDLE
var data=[
{
"letter":"A",
"frequency":.08167
},
{
"letter":"B",
"frequency":.01492
},
{
"letter":"C",
"frequency":.02780
},
{
"letter":"D",
"frequency":.04253
},
{
"letter":"E",
"frequency":.12702
},
{
"letter":"F",
"frequency":.02288
},
{
"letter":"G",
"frequency":.02022
},
{
"letter":"H",
"frequency":.06094
}
]
var margin = {top: 40, right: 20, bottom: 30, left: 40},
width = 625 - margin.left - margin.right,
height = 455 - margin.top - margin.bottom;
p=0,
totalColumns=0,
barNo=4,
viewdata=[],
initial=0;
var formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .5);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(formatPercent);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>";
})
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom+50)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.call(tip);
viewdata = data.slice(p,p+barNo);
x.domain(viewdata.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
svg.selectAll(".bar")
.data(viewdata)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width",50)
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
var prev=svg.append("svg:image")
.attr("xlink:href","http://www.visitliverpool.com/images/button_highlight_prev.gif")
.attr("id","prev")
.attr("class","pagingButton")
.attr("x", width-555)
.attr("y",height+20)
.attr("dy", "2.90em")
.attr("dx", "1.75em")
.attr("width", 45).attr("height", 25)
.on("click",onPrevClick)
var next=svg.append("svg:image")
.attr("xlink:href","http://www.visitliverpool.com/images/button_highlight_next.gif")
.attr("class","pagingButton")
.attr("id","next")
.attr("x", x.rangeBand()*barNo)
.attr("y",height+20)
.attr("dy", "2.90em")
.attr("dx", "1.75em")
.attr("width", 45).attr("height", 25)
.on("click",onNextClick)
data.forEach(function(d) {
totalColumns++;
});
initial=Math.floor(totalColumns/barNo);
if(totalColumns%barNo==0){
initial--;
}
function onNextClick(){
$("#prev").show();
initial--;
if(initial<=0){
$("#next").hide();
}
p+=barNo;
if(p>=totalColumns){
p-=barNo;
}
else{
viewdata = data.slice(p,p+barNo);
}
redraw();
}
function onPrevClick(){
$("#next").show();
initial++;
p-=barNo;
if(p<=0){
$("#prev").hide();
p=0;
}
viewdata = data.slice(p,p+barNo);
redraw();
}
function redraw()
{
x.domain(viewdata.map(function(d) { return d.letter; }));
//xAxis
//.scale(x)
var newXaxis= svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
var bars=svg.selectAll(".bar")
.data(viewdata);
bars .enter()
.append("rect");
bars.exit()
.remove();
bars
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width",50)
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
}
答案 0 :(得分:1)
最简单的方法是保持对x轴元素的引用并直接更新它:
var xaxisG = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// later...
xaxisG.call(xAxis);
完整示例here。