D3条形图条形图值显示不正确

时间:2015-01-09 05:21:31

标签: javascript css svg d3.js

我想在每个条形图的外部顶部显示每个条形峰值的条形图。但据我所知,是代表酒吧内的每个酒吧价值。但那不是我想要的。正如您在附图中看到的那样,x轴的底部是混乱的。我认为我们必须将svg代码移出数据循环并使用svg.data(someData).enter()来解决此问题,但我尝试了这样做但不成功。请建议我如何修改以下代码。

enter image description here

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

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

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


.chart text {
  fill: black;
  font: 15px sans-serif;
  text-anchor: middle;
}

</style>
<svg class="chart"></svg>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var margin = {top: 30, right: 80, bottom: 40, left: 100},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var color = d3.scale.category10();

var x = d3.scale.ordinal()
    .rangeRoundBands([0, width], .2);

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

function unit (d) {

  count = 0, download = 0;
  num = d.value;
  while((num) >= 1)
  {
      num/=10;             
      ++count;
  }

  switch (count) {
    case 4 : download = ((d.value/1000).toFixed(2)) + " MB"  ;  
      break;
    case 8 : download = ((d.value/1000 * 1000).toFixed(2)) + " GB"  ; 
      break;
    default: download = ((d.value/1000).toFixed(2)) + " MB" ;
  } 
  return download;
}

var chart =  d3.select("body").append("svg")  
 // line = 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 bar;

d3.csv("data.csv", type, function(error, data) {

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


chart.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis)
.append("text")
.attr("x", 790 )
.attr("y", 15 )
.style("text-anchor", "bottom")
      .text("Clients");


chart.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      //.attr("transform", "rotate(-90)")
      .attr("y", 1)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Kilo Bytes");

  bar = chart.selectAll("g")
      .data(data)
    .enter().append("g")
      .attr("transform", function(d) { return "translate(" + x(d.name) + ",0)"; });

  bar.append("rect")
      .style("fill", function(d) { return color(d.name); })
      .attr("y", function(d) { return y(d.value);})
      .attr("height", function(d) { return height - y(d.value); })
      .attr("width", x.rangeBand());

  bar.append("text")
      .attr("x",  x.rangeBand() / 2 )
      .attr("y", function(d) { return y(d.value) + 2 ; })
      .attr("dy", "1em")
      .attr("text-anchor", "middle")
      .attr("font-size", "14px")
      .attr("fill", "black")
      .text(function(d) { return unit(d); });
});

function type(d) {
  d.value = +d.value; // coerce to number
  return d;
}

</script>

,数据采用csv格式,如下所示:

name,timesatmp,value
98.226.148.140,1341677167454,1
98.226.148.140,1341677167461,3
98.226.148.141,1341677167916,4
98.226.148.141,1341677167935,6
98.226.148.142,1341677167944,7
98.226.148.142,1341677167945,32

请建议我如何正确显示显示器。

2 个答案:

答案 0 :(得分:1)

首先修改一下条形图,否则不会显示任何条形图:

<强> HTML:

<svg class="chart"></svg>

<body class="chart"></body>

<强> JS:

bar = chart.selectAll(".bar")
        .data(data)
        .enter().append("g")
        .attr('class', 'bar')
        .attr("transform", function(d) { return "translate(" + x(d.name) + ",0)"; });

并且要调整峰值,只需按以下方式添加翻译:

bar.append("text")
      .attr("x",  x.rangeBand() / 2 )
      .attr("y", function(d) { return y(d.value) + 2 ; })
      .attr("dy", "1em")
      .attr("text-anchor", "middle")
      .attr("font-size", "14px")
      .attr("fill", "black")
      .attr("transform", function(d) { return "translate(0, -20)"; })
      .text(function(d) { return unit(d); });

enter image description here

答案 1 :(得分:1)

RPLACE:

d3.csv("data.csv", type, function(error, data) {

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

使用:

d3.csv("data.csv", type, function(error, data) {

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

var nestedData = d3.nest()
    .key(function(d) { return d.name; }).entries(data);


var newData = [];
nestedData.forEach(function(d){
  newData.push({name:d.key, value: d3.max(d.values, function(d) {return d.value})});
});