[英]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.