D3.js条形图没有加载条形图

时间:2017-05-29 19:36:20

标签: javascript html csv d3.js visualization

这是我第一次使用D3.js,我从教程中调整了下面的代码并从.csv中提取数据,轴加载得很好,但由于某种原因条形图不能渲染条形图?我确定它非常简单。

真的非常感谢任何帮助!

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.bar {
  fill: steelblue;
}

.bar:hover {
  fill: brown;
}

.axis--x path {
  display: none;
}

</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom;

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

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

d3.csv("/dash/templates/dashing/Features.csv", function(d) {
  d.Used = +d.Used;
  return d;
}, function(error, data) {
  if (error) throw error;

  x.domain(data.map(function(d) { return d.Feature; }));
  y.domain([0, d3.max(data, function(d) { return d.Used; })]);

  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).ticks(10, ))
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", "0.71em")
      .attr("text-anchor", "end")
      .text("Feature");

  g.selectAll(".bar")
    .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.Feature); })
      .attr("y", function(d) { return y(d.Used); })
      .attr("width", x.bandwidth())
      .attr("height", function(d) { return height - y(d.Feature); });
});

</script>

1 个答案:

答案 0 :(得分:0)

你的假设是正确的。用于计算条形高度的代码中的小错误。您必须使用return height - y(d.Used);代替return height - y(d.Feature);

g.selectAll(".bar")
  .data(data)
  .enter().append("rect")
  .attr("class", "bar")
  .attr("x", function(d) {
    return x(d.Feature);
  })
  .attr("y", function(d) {
    return y(d.Used);
  })
  .attr("width", x.bandwidth())
  .attr("height", function(d) {
    return height - y(d.Used); //Updated Line
  });

&#13;
&#13;
var svg = d3.select("svg"),
  margin = {
    top: 20,
    right: 20,
    bottom: 30,
    left: 40
  },
  width = +svg.attr("width") - margin.left - margin.right,
  height = +svg.attr("height") - margin.top - margin.bottom;

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

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

var data = [{
  Feature: "Feature 1",
  Used: 10
}, {
  Feature: "Feature 2",
  Used: 20
}, {
  Feature: "Feature 3",
  Used: 15
}];

x.domain(data.map(function(d) {
  return d.Feature;
}));
y.domain([0, d3.max(data, function(d) {
  return d.Used;
})]);

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).ticks(10, ))
  .append("text")
  .attr("transform", "rotate(-90)")
  .attr("y", 6)
  .attr("dy", "0.71em")
  .attr("text-anchor", "end")
  .text("Feature");

g.selectAll(".bar")
  .data(data)
  .enter().append("rect")
  .attr("class", "bar")
  .attr("x", function(d) {
    return x(d.Feature);
  })
  .attr("y", function(d) {
    return y(d.Used);
  })
  .attr("width", x.bandwidth())
  .attr("height", function(d) {
    return height - y(d.Used);
  });
&#13;
.bar {
  fill: steelblue;
}

.bar:hover {
  fill: brown;
}

.axis--x path {
  display: none;
}
&#13;
<script src="https://d3js.org/d3.v4.min.js"></script>

<svg width="960" height="500"></svg>
&#13;
&#13;
&#13;