Raghuveer
Raghuveer

Reputation: 3057

D3 bar charts bar values display is improper

I want to display the bar charts each bar peak value at the outer top of each bar. But as far as i could get is to represent each bar value inside the bar. But thats not what i wanted. As you can see in the image attached the bottom of the x axis is messed up. I think we have to move the svg code out of the data loop and use svg.data(someData).enter() to fix this issue but i tried ways to do it but unsuccessful. Kindly suggest how i can modify the follow code.

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>

and the data is in the csv format as below:

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

Kindly suggest how i can show the display properly.

Upvotes: 1

Views: 2512

Answers (2)

huan feng
huan feng

Reputation: 8623

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; })]);

with:

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

Upvotes: 1

huan feng
huan feng

Reputation: 8623

First modify your bar charts a little, otherwise no bars will be shown there:

html:

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

to

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

and for adjusting the peak value, just add a translate as follows:

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

Upvotes: 1

Related Questions