【问题标题】:Why is my y-axis not shown accordingly to the data?为什么我的 y 轴没有根据数据显示?
【发布时间】:2014-09-12 03:14:32
【问题描述】:
<!DOCTYPE html>
<meta charset="utf-8">
<style>

.bar {
  fill: steelblue;
}

.bar:hover {
  fill: brown;
}

.axis {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  display: none;
}

.d3-tip {
  line-height: 1;
  font-weight: bold;
  padding: 12px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  border-radius: 2px;
}

/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
  box-sizing: border-box;
  display: inline;
  font-size: 10px;
  width: 100%;
  line-height: 1;
  color: rgba(0, 0, 0, 0.8);
  content: "\25BC";
  position: absolute;
  text-align: center;
}

/* Style northward tooltips differently */
.d3-tip.n:after {
  margin: -1px 0 0 0;
  top: 100%;
  left: 0;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script>

var margin = {top: 100, right: 20, bottom: 30, left: 50},
    width = 500;
    height = 200;

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")

var tip = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-10, 0])
  .html(function(d) {
    return "<strong>Cases:</strong> <span style='color:red'>" + d.cases + "</span>";
  })

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.call(tip);

d3.json("Cutaneous_leishmaniasis.json", function(error, data) {
  x.domain(data.map(function(d) { return d.year;}));
  y.domain([0, d3.max(data, function(d) { return d.cases; })]);

  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
    .append("text")
      .attr("transform", "0")
      .attr("x", 10)
      .attr("dx", ".71em")
      .style("text-anchor", "right")
      .text("Afghanistan");

  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 100)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Deaths");

  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.year); })
      .attr("width", x.rangeBand(1))
      .attr("y", function(d) { return y(d.deaths); })
          .attr("height", function(d) { return height - y(d.cases); })
          .on('mouseover', tip.show)
          .on('mouseout', tip.hide)

    });

function type(d) {
  d.deaths = +d.deaths;
  return d;
}

</script>

上面的代码将使用我拥有的 JSON 数据创建条形图,但 y 轴数据不是从 y 轴的 0 值开始,我不知道是什么原因导致这种情况。图表中是否有任何属性或方法导致此问题?

下面是我创建的条形图的屏幕截图,y 轴不是从 y 轴的值 0 开始的。 http://i.stack.imgur.com/igQts.jpg

【问题讨论】:

  • SVG 中的 Y 坐标从顶部开始,而不是底部。参见例如this tutorial.
  • @LarsKotthoff 您向我展示的教程的值的 y 轴从 0 值开始,而我上面的屏幕截图显示 y 轴从不相等的值开始到 0。
  • 是的,我正在向您展示一个工作示例。正如我所说,这是因为您假设 0 y 坐标位于 SVG 的底部,而它位于顶部。
  • @LarsKotthoff 是的,我明白你想告诉我的。但是,对于我的代码,哪个是影响 y 轴的代码?

标签: html json d3.js visualization bar-chart


【解决方案1】:

您使用d.cases 作为y 变量在d3.json 函数的开头设置域,但是当您设置高度时,您使用d.deaths. 您的y 函数设置为使用@ 987654326@ 数字,因为它的规模,所以你需要选择一个,我想。

【讨论】:

  • 如何更改我的代码以设置每个条的 y 值?
  • 只需使用 .attr("y", height). 即可将每个条形设置为使用相同的 y 值。
  • 其实我觉得这是错误的。让我再想想。
  • 是的,我也尝试使用 .attr("y", height) 但它仍然无法正常工作。花点时间思考一下,我非常感谢您花时间帮助我。
  • 你设置y坐标的范围吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多