访问D3的var格式数据
16lz
2021-01-22
I am having trouble understanding why my code won't work.
我无法理解为什么我的代码不起作用。
I am getting an error on .attr('r', function(d)
我在.attr('r',函数(d)上收到错误
I am assigning a number to year
and creating a variable called category
by adding d.p
and year
. The name of each data category in my csv file is p1900, p1901, p1902, p1903, etc. And I try to access it in .attr('r', function(d)
but it doesn't seem to register.
我正在为年份分配一个数字,并通过添加d.p和year创建一个名为category的变量。我的csv文件中每个数据类别的名称是p1900,p1901,p1902,p1903等。我尝试在.attr('r',函数(d)中访问它但它似乎没有注册。
cityPoints.attr('cx', function(d) {return projection([d.lon, d.lat])[0]})
.attr('cy', function(d) {return projection([d.lon, d.lat])[1]})
.attr('r', function(d) {
var category = "d.p"+year;
if (scale(category) != null) {return scale(category) + 'px'}
else {return '0px'};
;})
.attr('stroke', 'white');
1 个解决方案
#1
0
You're not actually accessing the d
object, just working on a string called d.p1900
instead.
你实际上并没有访问d对象,只是处理一个名为d.p1900的字符串。
Try doing this:
试着这样做:
cityPoints.attr('cx', function(d) {return projection([d.lon, d.lat])[0]})
.attr('cy', function(d) {return projection([d.lon, d.lat])[1]})
.attr('r', function(d) {
var category = "p" + year;
if (scale(d[category]) != null) {
return scale(d[category]) + 'px';
} else { return '0px' };
}).attr('stroke', 'white');
更多相关文章
- 求助 急 js替换字符串问题
- 如何判断字符串是一个字符串化的JSON对象
- 在传递给google.setOnLoadCallback()的函数中使用参数;
- 另一个iframe中的iframe的onload函数
- JavaScript相当于Ruby的字符串#扫描
- JavaScript 中的函数介绍
- 11、javascript中字符串常用操作总结、JS字符串操作大全
- 从占用转义字符的字符数组创建字符串
- javascript如何取字符串中的数字