条形图中的条形高度问题d3 js

时间:2017-11-01 12:24:54

标签: javascript d3.js

我正在尝试使用D3.js绘制条形图,但它无法正常工作。我正在尝试以下方法:

   var margin = {top:40, right: 40, bottom: 70, left: 40},
    width = 500 - margin.left - margin.right,
    height = 350 - margin.top - margin.bottom;

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')
  .html(function(d) {
    return "<strong>Value:</strong> <span style='color:red'>" + d.ActiveCount + "</span>";
  })

var svg = d3.select("#chart").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);

  var data = [{"CreateMonth":"5","ActiveCount":"6"},
      {"CreateMonth":"6","ActiveCount":"20"},
      {"CreateMonth":"7","ActiveCount":"43"},
      {"CreateMonth":"8","ActiveCount":"125"},
      {"CreateMonth":"9","ActiveCount":"356"},
      {"CreateMonth":"10","ActiveCount":"557"}];


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

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)
            .selectAll("text")  
        .style("text-anchor", "end")
        .attr("dx", "-.8em")
        .attr("dy", ".15em")
        .attr("transform", "rotate(-65)");

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("Active");

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

我已经使用本教程http://bl.ocks.org/d3noob/8952219完成了这个条形图,但是我面临条形(rect)高度问题,数组对象中的ActiveCount数据不均匀。还有一些我无法识别的问题吗?

另外,为什么将y轴如下所示

Bar Chart OutPut

也无法确定工具提示位置的高度,因为我认为是由于某些无序数据点或其他问题所致..

任何建议都应该受到赞赏

1 个答案:

答案 0 :(得分:3)

此处的问题是数据中的ActiveCount字符串,而不是数字。

您必须将其转换为数字。例如,使用一元加运算符:

data.forEach(function(d) {
    d.ActiveCount = +d.ActiveCount;
});

关于轴,您必须删除默认的fill。最简单的方法是使用CSS(让我们也改变滴答声):

line, path {
    fill: none;
    shape-rendering: crispEdges;
    stroke: black;
}

以下是包含这些更改的代码(不含d3.tip):

var margin = {
    top: 40,
    right: 40,
    bottom: 70,
    left: 40
  },
  width = 500 - margin.left - margin.right,
  height = 350 - margin.top - margin.bottom;
  
var format = d3.time.format("%b");
var parser = d3.time.format("%m").parse; 

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")
  .tickFormat(function(d){
    return format(parser(d))
  });

var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left");

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

var data = [{
  "CreateMonth": "5",
  "ActiveCount": "6"
}, {
  "CreateMonth": "6",
  "ActiveCount": "20"
}, {
  "CreateMonth": "7",
  "ActiveCount": "43"
}, {
  "CreateMonth": "8",
  "ActiveCount": "125"
}, {
  "CreateMonth": "9",
  "ActiveCount": "356"
}, {
  "CreateMonth": "10",
  "ActiveCount": "557"
}];

data.forEach(function(d) {
  d.ActiveCount = +d.ActiveCount;
});

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

svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(xAxis)
  .selectAll("text")
  .style("text-anchor", "end")
  .attr("dx", "-.8em")
  .attr("dy", ".15em")
  .attr("transform", "rotate(-65)");

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("Active");

svg.selectAll(".bar")
  .data(data)
  .enter()
  .append("rect")
  .style("fill", "darkorange")
  .attr("class", "bar")
  .attr("x", function(d) {
    return x(d.CreateMonth);
  })
  .attr("width", x.rangeBand())
  .attr("y", function(d) {
    return y(d.ActiveCount);
  })
  .attr("height", function(d) {
    return height - y(d.ActiveCount);
  })
line,
path {
  fill: none;
  shape-rendering: crispEdges;
  stroke: black;
}
<script src="https://d3js.org/d3.v3.min.js"></script>