d3js v5 x轴条形图

时间:2018-06-12 22:31:41

标签: javascript d3.js

我尝试了很多数据集,但我不知道为什么,我有 data_histo.csv 的问题。 x轴似乎相反,然后,条形无法显示。使用 data_histo2.csv data_histo3.csv ,一切都很好。

解释可能很好!

<!DOCTYPE html>
<html>

  <head>
    <script src="https://unpkg.com/d3@5.0.0/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>

使用 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 使用 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

这是我的代码:https://plnkr.co/edit/B8qEQ4dlUdRHhkQvzjZx?p=preview

1 个答案:

答案 0 :(得分:2)

您的代码存在两个问题:

D3将csv / tsv / dsv条目解析为文本。因此,当您加载csv时,会得到如下所示的行:

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

使用范围设置比例时,不使用数字范围。您可以将数据强制转换为如下数字:

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

其次,如果你这样做,你会注意到条形放置中的一些特殊行为:

enter image description here

您可以看到条形图从绘图区域的左侧开始。原因是你使用的是线性刻度,并绘制条形图的起点,如下所示:

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

您希望条形图从x(4)开始 - 这是标记y轴截取的x值。相反,你使用x(0),它自然会在你想要的左边。这适用于您的第二个示例,因为x(0)也恰好是截取y轴的x值。相反,您可以简单地使用:

.attr("x",0)

这标志着绘图区域的左边缘,这可能是您希望所有柱子锚定到的位置。

这是一个分叉的plunkr

有一点需要注意的是,您的最短栏始终是不可见的:起点和终点将是相同的。这是因为比例的范围从最小值到最大值 - 标记绘图区域左边界的最小值是最短柱的值。要更改此设置,您可以修改比例的域,可能使用0作为第一个值,然后使用d3.max查找最高值。