简体   繁体   English

D3尝试访问json对象中的vis元素

[英]D3 trying to access vis element in json object

I'm trying to access the vis element in the json object below using D3 (see screenshot), but am unable to do so. 我正在尝试使用D3访问下面的json对象中的vis元素(请参见屏幕截图),但无法这样做。 I can access the key and value elements without any problems using code similar to the following: 使用类似于以下内容的代码,我可以毫无问题地访问键和值元素:

console.log(d.key);

but this does not work (I get an undefined error): 但这不起作用(我收到未定义的错误):

console.log(d.vis);

Any ideas what I am doing wrong? 有什么想法我做错了吗? Thank you! 谢谢!

json obj

Here are some code snippets: 以下是一些代码段:

var nested = d3.nest().key(function(d) { return d._id.stream; })
  .entries(data.result);

var stream = main.selectAll(".stream")
  .data(nested)
  .enter().append("g")
  .attr("class", "stream");    

  stream.append("rect")
  .attr("height",10)
  .attr("width", 25)
  .attr("x",width-215)
  .attr("y", function(d,i) { return height-400 + (i*40); })
  .attr("stroke", function(d) { return color(d.key);})
  .attr("fill",function(d) {
      if(d.vis=="1") {
        return color(d.key);
      }
      else {
        return "white";
      }
   })
   .on("click", function(d) {
     console.log(d);
     console.log(d.vis);  // undefined
     if(d.vis=="1") {
        d.vis="0";
      }
      else{
        d.vis="1";
      }
   });

It looks like the following is happening. 看起来正在发生以下情况。 The click handler function is executed. 点击处理程序功能被执行。 Inside, you're logging d.vis , which is undefined. 在内部,您正在记录d.vis ,它是未定义的。 The code immediately after that checks whether d.vis has a particular value, else sets it to "1". 之后的代码将检查d.vis是否具有特定值,否则将其设置为“ 1”。 This "else" includes the undefined case. 该“其他”包括未定义的情况。

So after the handler has been executed, d.vis is set for all d . 因此,在执行处理程序之后, d.vis所有d设置d.vis The debugger you're using shows the value of the variable, which was modified after the console.log() statement . 您正在使用的调试器显示变量的值,该值console.log()语句之后进行了修改 That is, at the time when you printed d , d.vis was indeed undefined. 也就是说,在您打印dd.vis确实未定义。 But you're setting it immediately afterwards, and that's what you get in the console. 但是您要在之后立即进行设置,这就是您在控制台中看到的内容。

The log doesn't take a snapshot of the state of the variable when it's printed, but shows you the current version which, in this case, has d.vis set. 日志在打印时不会d.vis变量状态的快照,但会向您显示当前版本(在本例中已设置d.vis

pay attention to the data structure: see the fetchData.js, other than priceList and name, there should be vis element for determining whether to show the curve or not. 注意数据结构:请参阅fetchData.js,除了priceList和name之外,还应该有vis元素,用于确定是否显示曲线。

//The main data array of the graph
//format:
//data2 (List of fundObj)
//   fundObj (Name, priceList)
//      Name
//      vis
//      priceList (List of priceItem)
//         priceItem (date, price)

so, be clear about what is "d" in the code, it all depends on the data you passed to the call: 因此,请清楚代码中的“ d”是什么,这完全取决于您传递给调用的数据:

var fund = focus.selectAll(".fund")
        .data(data2)
        .enter().append("g")
        .attr("class", "fund");

Here data2 is the raw data, you may use other structure, but need to modify some of the code in places of "d"..... 这里的data2是原始数据,您可以使用其他结构,但是需要在“ d”处修改一些代码。

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

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