D3 Js条形图标签放置错误

时间:2015-04-18 03:09:03

标签: json d3.js charts

我试图使用在线找到的一个示例(http://jsfiddle.net/chrisJamesC/uzmur5kb/)生成条形图。该图表根据需要出现,但我无法将数据作为标签放在条形图的顶部。请帮忙。 jsfiddle链接是http://jsfiddle.net/info2scs/yr2n3efw/ 代码段如下所示

var data = [
   {
         "Activities" : [
            {"attr" :"Received", "value": 1275},
            {"attr" :"Created", "value": 875},
            {"attr" :"Error", "value": 400}  
         ]
   }
];

var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 550 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;

var x0 = d3.scale
           .ordinal()
		   .rangeRoundBands([0, width], .09);
var y = d3.scale
          .linear()
		  .range([height, 0]);

var color = d3.scale.ordinal()

.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

var xAxis = d3.svg.axis()
                  .scale(x0)
                  .orient("bottom");
var yAxis = d3.svg.axis()
                  .scale(y)
                  .orient("left");
                //.tickFormat(d3.format(".2s"));

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

  console.log('Data : ' + data);

  var attr = [];
  data[0]['Activities'].forEach(function(d){return attr.push(d.attr)})

  //console.log('Attributes are : ' + attr)

  x0.domain(attr.map(function(d){return d}));
  y.domain([0, d3.max(data, function(d) { return d3.max(d["Activities"], function(d) { return d.value; }); })]);

  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", -40)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Counts");

  var state = svg.selectAll(".state")
      .data(data)
      .enter().append("g")
      .attr("class", "g")
      .attr("transform", function(d) { return "translate(" + x0(+d['Activities']['attr']) + ",0)"; });

  state.selectAll("rect")
        .data(function(d) { return d['Activities']; })
        .enter().append("rect")
        .attr("class","bars")
        .attr("width", x0.rangeBand())
        .attr("x", function(d) { return x0(d.attr); })
        .attr("y", function(d) { return y(d.value); })
        .attr("height", function(d) { return height - y(d['value']); })
        .style("fill", function(d) { return color(d.attr);});

  state.selectAll("text")
        .data(function(d) { return d['Activities']; })
        .enter()
        .append("text")
        .text(function(d) {return d.value;})
        .attr("x", function(d, i) {return i * (width / 3) + 70;})
        .attr("y", function(d) {return height - (d.value/4); });
body {font: 10px sans-serif;}
.axis path,.axis line {fill: none; stroke: #000; shape-rendering: crispEdges;}
.bar {fill: steelblue;}
.x.axis path { display: none;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.10/d3.min.js"></script>
<div id="chart"></div>

1 个答案:

答案 0 :(得分:0)

您忘了将y比例也应用于文本的y值,如下所示:

state.selectAll("text")
    .data(function(d) { return d['Activities']; })
    .enter()
    .append("text")
    .text(function(d) {return d.value;})
    .attr("x", function(d, i) {return i * (width / 3) + 70;})
    .attr("y", function(d) {return y(d.value) - 2; });

jsfiddle with working example