如何自动调整d3.js图形以包含轴

时间:2017-05-08 18:46:15

标签: d3.js dynamic width bar-chart axis

构建条形图时遇到了一些问题。我第一次学习d3.js并且是一个总是使用PHP / MySQL的人,我还没有学过javascript。结果,我有点挣扎。

我的问题更具概念性。如果,让我们在条形图中说,Y轴包含在ag元素中,并且条形包含在另一个中,我如何确保我的轴根据显示的数据采用动态宽度?

我设法生成条形图并且效果很好,但填充是一个固定的数字(让我们说50px)。它现在很好用,因为我的数字从0到50,所以一切都适合。如果我获得数万亿的话,会发生什么?轴的宽度会改变,但我的填充仍然是50px,这意味着它将剪切我的内容。

什么是"惯例"说到这个?任何技巧?

由于

1 个答案:

答案 0 :(得分:2)

你可能会在这里使用的一个技巧是我喜欢称之为"双渲染"。您基本上首先绘制轴(在绘图的其余部分之前)并获得最大刻度标签的宽度。您可以使用该值作为边距以常规方式绘制绘图。这个技巧对字符串"类别"特别有用。标签,但也适用于数字。

这是一个评论的例子。多次运行以查看它如何重新修改轴:



<!DOCTYPE html>
<meta charset="utf-8">
<style>
  .bar {
    fill: steelblue;
  }
  
  .bar:hover {
    fill: brown;
  }
  
  .axis--x path {
    display: none;
  }
</style>
<svg width="300" height="300"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//chancejs.com/chance.min.js"></script>
<script>


  // set up some random data
  // pick a random max value to render on the yaxis
  var maxVal = chance.integer({
      min: 1,
      max: chance.pickone([1e1, 1e5, 1e10])
    }),
    // generate some fake data
    data = [{
      x: chance.word(),
      y: chance.floating({
        min: 0,
        max: maxVal
      })
    }, {
      x: chance.word(),
      y: chance.floating({
        min: 0,
        max: maxVal
      })
    }, {
      x: chance.word(),
      y: chance.floating({
        min: 0,
        max: maxVal
      })
    }, {
      x: chance.word(),
      y: chance.floating({
        min: 0,
        max: maxVal
      })
    }];

  // create svg and set up a y scale, the height value doesn't matter
  var svg = d3.select("svg"),
    y = d3.scaleLinear().rangeRound([100, 0]);

  // set domain
  y.domain([0, d3.max(data, function(d) {
    return d.y;
  })]);

  // draw fake axis
  var yAxis = svg.append("g")
    .attr("class", "axis axis--y")
    .call(d3.axisLeft(y));

  // determine max width of text label
  var mW = 0;
  yAxis.selectAll(".tick>text").each(function(d) {
    var w = this.getBBox().width;
    if (w > mW) mW = w;
  });

  // remove fake yaxis
  yAxis.remove();

  // draw plot normally
  var margin = {
      top: 20,
      right: 20,
      bottom: 30,
      left: mW + 10 // max with + padding fudge
    },
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom;

  var g = svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

  // reset to actual height
  y.range([height, 0]);

  var x = d3.scaleBand().rangeRound([0, width]).padding(0.1);

  x.domain(data.map(function(d) {
    return d.x;
  }));

  g.append("g")
    .attr("class", "axis axis--x")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x));

  g.append("g")
    .attr("class", "axis axis--y")
    .call(d3.axisLeft(y));

  g.selectAll(".bar")
    .data(data)
    .enter().append("rect")
    .attr("class", "bar")
    .attr("x", function(d) {
      return x(d.x);
    })
    .attr("y", function(d) {
      return y(d.y);
    })
    .attr("width", x.bandwidth())
    .attr("height", function(d) {
      return height - y(d.y);
    });
</script>
&#13;
&#13;
&#13;