带有D3JS V7错误的概率读取条形图:< rect>属性高度:预期长度,“ nan”

发布于 2025-02-05 06:24:35 字数 2721 浏览 3 评论 0原文

亲爱的堆栈溢出社区,

我一直在尝试使用D3V7为我的一个Uni项目创建条形图,遵循_Blocks _示例。但是,我遇到了一个问题标题中提到的错误。显然,我的代码阅读我的数据有问题,但我不知道如何解决此问题。如果您可以在这里帮助我,这真的很有帮助:)

这是我的代码:

var margin = {top: 5, right:10, bottom:5, left:10},
    width = 575 - margin.left - margin.right,
    height = 200 - margin.top - margin.bottom;

// set the ranges
var x = d3.scaleBand()
          .range([0, width])
          .padding(0.1);
var y = d3.scaleLinear()
          .range([height, 0]);
          
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("#bar").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 + ")");

// get the data
d3.csv("canton_pop.csv").then(function(data) {

  // format the data
  data.forEach(function(d) {
    d.Population = +d.Population;
  });

  // Scale the range of the data in the domains
  x.domain(data.map(function(d) { return d.Canton; }));
  y.domain([0, d3.max(data, function(d) { return d.Population; })]);

  // append the rectangles for the bar chart
  svg.selectAll(".bar_chart")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar_chart")
      .attr("x", function(d) { return x(d.Canton); })
      .attr("width", x.bandwidth())
      .attr("y", function(d) { return y(d.Population); })
      .attr("height", function(d) { return height - y(d.Population); });

  // add the x Axis
  svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

  // add the y Axis
  svg.append("g")
      .call(d3.axisLeft(y));

});

这是我的CSV文件的样子:

Canton;Population
Zürich;1553423
Bern;1043132
Luzern;416347
Uri;36819
Schwyz;162157
Obwalden;38108
Nidwalden;43520
Glarus;40851
Zug;128794
Fribourg;325496
Solothurn;277462
Basel-Stadt;196735
Basel-Landschaft;290969
Schaffhausen;83107
Appenzell Ausserrhoden;55309
Appenzell Innerrhoden;16293
St. Gallen;514504
Graubünden;200096
Aargau;694072
Thurgau;282909
Ticino;350986
Vaud;814762
Valais;348503
Neuchâtel;175894
Genève;506343
Jura;73709

运行我的代码时,我能够可视化图形轴,但没有出现栏,并且轴没有任何毕业(以下是我得到的结果的图像以及控制台中出现的错误)。希望有人可以在这里帮助我。

空白条图:

我的控制台中出现的错误消息:

Dear stack overflow community,

I've been trying to create a bar chart using d3v7 for one of my Uni projects, following the _Blocks_ example. However, I am getting an error as mentioned in this question title. Apparently, my code has a problem reading my data but I can't figure out how to fix this. It would be really helpful if you could help me here :)

Here is my code:

var margin = {top: 5, right:10, bottom:5, left:10},
    width = 575 - margin.left - margin.right,
    height = 200 - margin.top - margin.bottom;

// set the ranges
var x = d3.scaleBand()
          .range([0, width])
          .padding(0.1);
var y = d3.scaleLinear()
          .range([height, 0]);
          
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("#bar").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 + ")");

// get the data
d3.csv("canton_pop.csv").then(function(data) {

  // format the data
  data.forEach(function(d) {
    d.Population = +d.Population;
  });

  // Scale the range of the data in the domains
  x.domain(data.map(function(d) { return d.Canton; }));
  y.domain([0, d3.max(data, function(d) { return d.Population; })]);

  // append the rectangles for the bar chart
  svg.selectAll(".bar_chart")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar_chart")
      .attr("x", function(d) { return x(d.Canton); })
      .attr("width", x.bandwidth())
      .attr("y", function(d) { return y(d.Population); })
      .attr("height", function(d) { return height - y(d.Population); });

  // add the x Axis
  svg.append("g")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));

  // add the y Axis
  svg.append("g")
      .call(d3.axisLeft(y));

});

Here is what my CSV file looks like:

Canton;Population
Zürich;1553423
Bern;1043132
Luzern;416347
Uri;36819
Schwyz;162157
Obwalden;38108
Nidwalden;43520
Glarus;40851
Zug;128794
Fribourg;325496
Solothurn;277462
Basel-Stadt;196735
Basel-Landschaft;290969
Schaffhausen;83107
Appenzell Ausserrhoden;55309
Appenzell Innerrhoden;16293
St. Gallen;514504
Graubünden;200096
Aargau;694072
Thurgau;282909
Ticino;350986
Vaud;814762
Valais;348503
Neuchâtel;175894
Genève;506343
Jura;73709

When running my code I am able to visualize the graphics axis but no bar appears and the axis doesn't have any graduation (below is an image of the result I get and of the error that appears in my console). Hopefully, someone can help me here.

Blank bar chart:
blank bar chart

The error message that appears in my console:
error message that appears in my console

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2025-02-12 06:24:36

好的,所以我只是意识到问题来自我的CSV文件,实际上我正在使用“”;当我需要使用“”时作为分离

Okay so I just realized the problem was coming from my CSV file, indeed I was using ";" as separation when I needed to use ","

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文