简体   繁体   English

将变量传递到d3.js函数(d)

[英]Passing a variable into d3.js function(d)

Fairly new to d3.js, so there may be something obvious I'm missing. d3.js相当新,因此可能缺少一些明显的东西。 I'm trying to pass a string variable into a function(d) to calculate the "x" position of text labels based on another function. 我正在尝试将字符串变量传递给function(d),以基于另一个函数计算文本标签的“ x”位置。 I've got the following (relevant) code: 我有以下(相关)代码:

function display_labels(category){
  var categories = vis.selectAll("."+category+"s")
                   .data(cat_data);
  console.log(category); // returns 'year' - correct
  categories.enter().append("text")
                    .attr("class", category+"s")
                    .attr("x", function(d) { 
                      console.log(category); // returns undefined - incorrect
                      // more code after this

How can I pass the string into the function so that it will work? 我如何将字符串传递到函数中以便它可以工作?

Further below in the code I had an if statement which incorrectly called the same function again with an undefined category. 在代码的下面,我有一个if语句,该语句再次错误地使用未定义的类别调用同一函数。 I was using: 我正在使用:

if (category2 !== 'undefined') { display_labels(category2); }

Instead of: 代替:

if (category2 !== undefined) { display_labels(category2); } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM