【问题标题】:D3: Accessing bound data after using .datum()D3:使用 .datum() 后访问绑定数据
【发布时间】:2017-03-06 15:48:50
【问题描述】:

在使用 .datum() 后,我无法弄清楚如何添加带有绑定数据的标题元素

当 .datum() 被调用时,'d' 包含所有预期的属性,但在调用 .datum() 之后,后续尝试访问属性失败...'d' 仅包含路径:

var oc = og.selectAll('.oc-circle')
   .data(function(d) { return [d]; }, get_key);

oc.enter()
   .append('path')
   .attr({ 'class': 'occ oc-circle' });

oc.exit().remove();

oc
   .datum(function(d) {
      console.log(d);
      // d has all of its properties
      // Object {type: "Feature", properties: Object, geometry: Object, id: "nn00564043"}
      return circle
          .origin([d.geometry.coordinates[0], d.geometry.coordinates[1]])
          .angle(.5)();
   })
   .style({ 
      "fill" : 'red',
      'opacity': 0.75,
   })
   .attr("d", geoPath)
   .append('title')
   .text(function(d) {
      console.log(d);
      // d only contains path data
      // Object {type: "Polygon", coordinates: Array[1]}
      // return 'Magnitue ' + d.properties.mag + ' ' + d.properties.place;
   })

上面的内联 cmets 显示了调用 .datum() 后可以访问的内容

我错过了什么?

谢谢!

【问题讨论】:

    标签: d3.js


    【解决方案1】:

    马克的建议是正确的。在这个小提琴的帮助下:http://jsfiddle.net/Lg9z57g5/ 我想出了以下内容,包括使用 d3.geo.circle()path.pointRadius() 绘制圆圈的函数>

    var pointPath = function(d, r) {
       var coords = [d.geometry.coordinates[0], d.geometry.coordinates[1]];
       var pr = geoPath.pointRadius(globe.scale() / 100 * 1.5);
       return pr({
          type: "Point",
          coordinates: coords
       })
    }
    
    var circlePath = function(d) {
       var circle = d3.geo.circle();
       var coords = [d.geometry.coordinates[0], d.geometry.coordinates[1]];
       var cc = circle.origin(coords).angle(.5)();
       return geoPath(cc);
    }
    
    var oc = og.selectAll('.oc-circle')
        .data(function(d) { return [d]; }, get_key);
    
    oc.enter()
        .append('path')
        .attr({ 'class': 'occ oc-circle' });
    
    oc.exit().remove();
    
    oc
        .attr("d", pointPath)
        .style({ 
             'fill' : 'red',
             'opacity': 0.75,
         })
         .append('title')
         .text(function(d) {
             return 'Magnitue ' + d.properties.mag + ' ' + d.properties.place;
         })
    

    在我的情况下更大的问题是 reDraw() 函数,它基本上是:

     surface.selectAll("path").attr("d", d3.geo.path().projection(projection));
    

    这预计所有绑定到路径的数据都包含 d.geometry.coordinates

    我必须添加这一行:

    surface.selectAll('.occ').attr('d', pointPath)
    

    我了解到,在使用 d3 附加“路径”时,如果绑定的数据已经采用正确的路径格式,则无需添加 .attr('d', path)

    【讨论】:

    • 我在上下文中的解决方案可以在此处的“可重用可更新全局混搭”块中找到:bl.ocks.org/TennisVisuals
    【解决方案2】:

    您的.datum 电话正在返回

    return circle
      .origin([d.geometry.coordinates[0], d.geometry.coordinates[1]])
      .angle(.5)();
    

    这是一条路径,这是数据绑定的,而不是d

    我不确定你是否需要datum 电话:

    oc
       .style({ 
          "fill" : 'red',
          'opacity': 0.75,
       })
       .attr("d", function(d){
         return geopath(circle
          .origin([d.geometry.coordinates[0], d.geometry.coordinates[1]])
          .angle(.5)());
       })
       .append('title')
       .text(function(d) {
          console.log(d);
          // d only contains path data
          // Object {type: "Polygon", coordinates: Array[1]}
          // return 'Magnitue ' + d.properties.mag + ' ' + d.properties.place;
       })
    

    【讨论】:

    • geoPath = d3.geo.path().projection(projection); .datum() 正在修改 'd' 以将适当的多边形传递到 geoPath
    • 感谢您的建议,马克。不幸的是,这也不起作用,但我会探索排列。 .我想我需要弄清楚如何使用 geoPath.pointRadius() 来做到这一点。这是最后一个建议的结果:. attribute d: Expected number, "…9674635624766m0,NaNaNaN,NaN 0 1,…"。>
    • @TennisVisuals,你能在 jsfiddle 或 plunker 上编写一个完整的可重现示例吗?
    • 马克 - 你的建议是正确的。我发现我的项目中的 reDraw() 函数期望 .datum() 调用已经修改了绑定数据,所以我的修复(在实现你的建议之后)是重新编写我的 reDraw() 函数。当我在处理一个相关问题时,我还发现这个小提琴很有帮助:jsfiddle.net/Lg9z57g5
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多