在D3條形圖中覆蓋標簽

[英]Overwriting of labels in D3 bar chart


I am having a bar chart in which I am displaying 4 bars at a time. When I press the next button the next 4 bars are shown. But the labels on x axis are being overwritten.

我有一個條形圖,每次顯示4條。當我按下下一個按鈕時,接下來的4條會顯示出來。但是x軸上的標簽被覆蓋了。

Here is my fiddle: 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)
}

1 个解决方案

#1


1  

The easiest way to do this is to keep a reference to the x axis element and update it directly:

最簡單的方法是保存x軸元素的引用並直接更新:

var xaxisG = svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis);

// later...

xaxisG.call(xAxis);

Complete example here.

完整的例子。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/01/28/720c0491511cc77c304d355472c76f.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com