Bellice
Bellice

Reputation: 322

d3js v5 x-axis bar chart

I tried a lot of datasets and I don't know why, I have a issue with data_histo.csv. The x axis seems reverse and then, bars can't be displayed. With data_histo2.csv or data_histo3.csv, all is good.

An explanation could be nice!

<!DOCTYPE html>
<html>

  <head>
    <script src="https://unpkg.com/[email protected]/dist/d3.min.js"></script>
  </head>

  <body>


  <svg class="histogramme" width="960" height="600"></svg>

  <script>

//select
let svgHisto = d3.select(".histogramme")

//dimension
  let margin = {top: 20, right: 10, bottom: 20, left: 80}
  let width = +svgHisto.attr("width") - margin.left - margin.right
  let height = +svgHisto.attr("height") - margin.top - margin.bottom;

  let g1 = svgHisto.append("g")
      .attr("transform",`translate(${margin.left}, ${margin.top})`);

//data
  d3.csv("data_histo.csv").then(function(data) {
    console.log(data);

//define x and y axis
    let x = d3.scaleLinear();
    let y = d3.scaleBand();

x.domain(d3.extent(data, function(d) { return d.value0; })).nice()
     .rangeRound([0, width]);
    y.domain(data.map(function(d) { return d.libreg; }))
     .rangeRound([0, height]).padding(0.1);

//add x axis
    g1.append("g")
        .attr("class", "axis x_axis")
        .attr("transform",`translate(0,${height})`)
        .call(d3.axisBottom(x));

//add y axis
    g1.append("g")
        .attr("class", "axis y_axis")
        .call(d3.axisLeft(y));

//bar chart
  g1.selectAll(".bar1")
      .data(data)
      .enter().append("rect")
        .attr("class", "bar bar1")
        .attr("x", function(d) {return x(Math.min(0,d.value0)); })
        .attr("y", function(d) { return y(d.libreg) + 10; })
        .attr("width", 0)
        .attr("height", y.bandwidth() - 20);

//animate
      g1.selectAll(".bar1")
      .transition()
      .duration(1000)
      .delay(function(d,i){return i*100})
      .attr("width", function(d) { return x(d.value0); });

  });

  </script>

  </body>

</html>

With data_histo.csv

"codreg","libreg","year0","value0","year1","value1"
"03","Guyane",2009,4,2014,4.6
"04","La Réunion",2009,8.2,2014,9.8
"11","Île-de-France",2009,12.6,2014,13.9
"01","Guadeloupe",2009,13.3,2014,15.8
"32","Hauts-de-France",2009,14.7,2014,16.1
"02","Martinique",2009,14.7,2014,17.6
"44","Grand Est",2009,16.5,2014,18
"84","Auvergne-Rhône-Alpes",2009,16.8,2014,18.3
"52","Pays de la Loire",2009,17.1,2014,18.6
"28","Normandie",2009,17.2,2014,19
"53","Bretagne",2009,18.5,2014,20.2
"24","Centre-Val de Loire",2009,18.7,2014,20.4
"27","Bourgogne-Franche-Comté",2009,18.8,2014,20.7
"76","Occitanie",2009,19.3,2014,20.8
"93","Provence-Alpes-Côte d''Azur",2009,19.5,2014,21.3
"94","Corse",2009,20.2,2014,21.5
"75","Nouvelle-Aquitaine",2009,20.2,2014,21.8

enter image description here With data_histo2.csv

"codreg","libreg","year0","value0","year1","value1"
"84","Auvergne-Rhône-Alpes",2013,39.1,2017,41.7
"27","Bourgogne-Franche-Comté",2013,42.3,2017,44.4
"53","Bretagne",2013,39.6,2017,44.7
"24","Centre-Val de Loire",2013,40.5,2017,46.8
"94","Corse",2013,24.2,2017,30.8
"44","Grand Est",2013,41.3,2017,45.4
"01","Guadeloupe",2013,55.5,2017,56.5
"03","Guyane",2013,33.1,2017,33.2
"32","Hauts-de-France",2013,45.8,2017,47.3
"11","Île-de-France",2013,40.1,2017,42.6
"02","Martinique",2013,52.5,2017,50.2
"28","Normandie",2013,42.6,2017,46.2
"75","Nouvelle-Aquitaine",2013,40,2017,44.4
"76","Occitanie",2013,40.3,2017,43.7
"52","Pays de la Loire",2013,40.6,2017,45.8
"93","Provence-Alpes-Côte d''Azur",2013,38.5,2017,42.6
"04","La Réunion",2013,54.2,2017,54.6
"06","Mayotte",2013,,2017,

enter image description here

Here is my code : https://plnkr.co/edit/B8qEQ4dlUdRHhkQvzjZx?p=preview

Upvotes: 2

Views: 883

Answers (1)

Andrew Reid
Andrew Reid

Reputation: 38161

There are two issues with your code:

D3 parses csv/tsv/dsv entries as text. So when you load your csv, you get rows that look like this:

{
  "codreg": "03",
  "libreg": "Guyane",
  "year0": "2009",
  "value0": "4",
  "year1": "2014",
  "value1": "4.6"
}

When you set your scale with extent, you aren't using the numerical extent. You could coerce your data to a number like so:

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

Secondly, if you do this you'll notice some peculiar behavior in the placement of the bars:

enter image description here

You can see that the bars start to the left of the plot area. The reason is that you are using a linear scale, and plot the start of the bars like so:

.attr("x", function(d) {return x(Math.min(0,d.value0)); })

You want your bars to start at x(4) - which is where the x value that marks the interception with the y axis. Instead you are using x(0), which will naturally be to the left of where you want. This works in your second example, because x(0) also happens to be the x value that intercepts the y axis. Instead, you can simply use:

.attr("x",0)

This marks the left edge of the plot area, which is likely where you want all your bars to be anchored to.

Here's a forked plunkr.

One thing to note, is that your shortest bar will always be non-visible: the start and end points will be the same. This is because the extent of the scale goes from the smallest value to the largest value - and the smallest value, marking the left boundary of the plot area, is the value of the shortest bar. To change this you can modify the scale's domain, perhaps using 0 as the first value and then using d3.max to find the uppermost value.

Upvotes: 2

Related Questions