James Poly
James Poly

Reputation: 13

Average line on a bar chart

I have a problem with a bar chart and its line average.

The average line doesn't cover all the bars. The last bar is without average line.

I used d3js code.

var data = [{
    "session": "1",
    "score": 70
  },
  {
    "session": "2",
    "score": 30
  },
  {
    "session": "3",
    "score": 50
  },
  {
    "session": "4",
    "score": 60
  },
  {
    "session": "5",
    "score": 40
  }
];

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



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

var x2 = d3.scale.ordinal()
  .rangeBands([0, width], 0);


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


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


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

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

svg.selectAll(".bar")
  .data(data)
  .enter().append("rect")
  .attr("class", "bar")
  .attr("x", function(d) {
    return x(d.session);
  })
  .attr("width", x.rangeBand())
  .attr("y", function(d) {
    return y(d.score);
  })
  .attr("height", function(d) {
    return height - y(d.score);
  });

var dataSum = d3.sum(data, function(d) {
  return d.score;
});

var line = d3.svg.line()
  .x(function(d, i) {
    return x2(d.session) + i;
  })
  .y(function(d, i) {
    return y(dataSum / data.length);
  });

svg.append("path")
  .datum(data)
  .attr("class", "line")
  .attr("stroke-width", "2")
  .attr("d", line);
<!DOCTYPE html>
<meta name="robots" content="noindex">
<html>

<head>
  <meta charset=utf-8 />
  <title>JS Bin</title>
  <style>
    rect.bar {
      fill: cyan;
    }
    path.line {
      stroke: black;
    }
  </style>
</head>

<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
</body>

</html>

Upvotes: 1

Views: 1274

Answers (1)

Jason Aller
Jason Aller

Reputation: 3652

The existing code was appending a multi-segment path that stopped short because it was going from the start point of each entry in data to the next start point. This example replaces it with a single line that goes from the left most element's start position to the far right side of the graph.

var data = [{
    "session": "1",
    "score": 70
  },
  {
    "session": "2",
    "score": 30
  },
  {
    "session": "3",
    "score": 50
  },
  {
    "session": "4",
    "score": 60
  },
  {
    "session": "5",
    "score": 40
  }
];

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



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

var x2 = d3.scale.ordinal()
  .rangeBands([0, width], 0);


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


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


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

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

svg.selectAll(".bar")
  .data(data)
  .enter().append("rect")
  .attr("class", "bar")
  .attr("x", function(d) {
    return x(d.session);
  })
  .attr("width", x.rangeBand())
  .attr("y", function(d) {
    return y(d.score);
  })
  .attr("height", function(d) {
    return height - y(d.score);
  });

var dataSum = d3.sum(data, function(d) {
  return d.score;
});

svg.append("line")
    .attr("x1", x2(1))
    .attr("x2", width)
    .attr("y1", y(dataSum / data.length))
    .attr("y2", y(dataSum / data.length));
<!DOCTYPE html>
<meta name="robots" content="noindex">
<html>

<head>
  <meta charset=utf-8 />
  <title>JS Bin</title>
  <style>
    rect.bar {
      fill: cyan;
    }
    line {
      stroke: black;
    }
  </style>
</head>

<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
</body>

</html>

Upvotes: 2

Related Questions