简体   繁体   English

d3 svg矩形高度变为NaN

[英]d3 svg rect height becomes NaN

I am unable to debug the reason why the rect height becomes NaN , with the embedded code below. 我无法使用以下嵌入式代码调试rect height变为NaN的原因。

Instead , if I just have .attr("height",yScale.bandwidth()) , then I would see proper height in HTML elements. 相反,如果我只有.attr(“ height”,yScale.bandwidth()),那么我会在HTML元素中看到适当的高度。

The csv file is loaded properly , as I'm able to access the country and year properly. csv文件已正确加载,因为我能够正确访问国家和年份。

If needed , I can upload the full code. 如果需要,我可以上传完整的代码。

 var rHeight = function(d) { return d.ratio;}; //// data is of CSV format : //country,ratio,year //United States,0.33,1924 //United States,0.33,1928 //United States,0.33,1932 //United States,0.0,1936 //United States,0.17,1948 //United States,0.2,1952 //:: //:: // // draw rectangles svg.selectAll(".bar") .data(rpc_data) .enter() .append("rect") .attr("class","bar") .attr("x",xMap) .attr("y",yMap) .attr("width",xScale.bandwidth()) .attr("height",Math.floor((yScale.bandwidth())*rHeight) ); 

As we can see from your snippet rHeight it is a function, so when you run: 正如您从代码段rHeight看到的rHeight它是一个函数,因此在运行时:

Math.floor((yScale.bandwidth())*rHeight

it is the same as number * function and you get NaN as result (check demo). 它与number * function相同,结果为NaN (请检查演示)。

 console.log(1 * function() {}); 

You should calculate the height this way: 您应该这样计算高度:

...
.attr("height", function(d){
  return Math.floor((yScale.bandwidth()) * rHeight(d)
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 d3堆栈似乎正在工作,但是当将值推入svg rect时得到NaN - d3 stack appears to be working but getting NaN when values are pushed to svg rect 无法使用d3将rect附加到SVG - Unable to append a rect to a SVG using d3 错误: <rect> 属性y:预期长度“ NaN”。 D3 JS - Error: <rect> attribute y: Expected length, “NaN”. D3 JS 在d3中将&#39;rect&#39;元素添加到d3中的空白svg画布失败。 - Adding 'rect' elements to blank svg canvas in d3 is failing on .data() 使用D3强制布局在SVG矩形内添加节点 - Adding nodes inside svg rect using d3 force layout 使用 d3 在 svg 中的 rect 组件顶部插入文本 - Inserting a text on top of the rect component in a svg using d3 d3.v3.min.js:1 错误:<svg> 属性高度:在模式弹出窗口中加载 d3 图表时的预期长度,“NaN” - d3.v3.min.js:1 Error: <svg> attribute height: Expected length, "NaN" while loading a d3 chart in a modal popup D3.js 使用 d3 append 将“rect”元素添加到 svg 时不可见的 rect 元素 - D3.js Invisible rect element when using d3 append to add “rect” elements to an svg 使用 D3 和 javascript 在 SVG 矩形和 SVG 文本之间垂直对齐 - Vertical alignment between an SVG rect and SVG text using D3 and javascript d3 v4画笔范围错误: <rect> 属性x:预期长度,“ NaN” - d3 v4 brush extent Error: <rect> attribute x: Expected length, “NaN”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM