如何使用d3.data()。enter()。call()

时间:2012-04-23 13:58:34

标签: javascript openlayers d3.js

我正在尝试将d3项目与开放层项目链接起来。我想要做的是使用d3来确定我的DOM中是否存在给定节点。

如果存在,我使用过渡。 如果它不存在,我需要通过openlayers API插入节点。这是必需的,因为节点已注册openlayers。我最初的想法是在d3.data()。enter()。call(myInsertFunction())上执行调用 但这似乎并没有起作用。 我希望有人可以帮助我或指出我正确的方向。我是d3 lib的新手。

function insertNewFeature(d) {
    var word = d;
    var pixel = new OpenLayers.Pixel(word.x,word.y);
    var lonlat = map.getLonLatFromPixel(pixel);
    var point = new OpenLayers.Geometry.Point(lonlat.lon,lonlat.lat);
    var feature = new OpenLayers.Feature.Vector(point);
    feature.id = word.city+'_'+word.text,
    feature.attributes = {
        text: word.text,
        sentiment: word.sentiment,
        count: word.count
    };
    feature.style = {
        label: word.text,
        labelAlign: 'mm',
        fontColor:  word.color,
        fontFamily: word.font,
        fontSize: word.size,
        rotation: word.rotate,
        strokeColor: '#FFFFFF',
        strokeWidth: 1,
        strokeOpacity: 0.5
    }
    svgLayer.addFeatures([feature]);
}


function update(data)
{
    var text = d3.select("OpenLayers.Layer.Vector_4_troot").selectAll("text").data(data, function(d) { return d.cityCode + "_" + d.text.toLowerCase() });

    text.transition()
        .duration(1000)
        .attr("transform", function(d) { return "translate(" + [ d.x , d.y ] + ")rotate(" + d.rotate + ")"; });
    //text.style("font-size", function(d) { return d.size + "px"; });
    text.enter().call(insertNewFeature());

}

1 个答案:

答案 0 :(得分:2)

试试这个jsfiddle:http://jsfiddle.net/Q8b2z/

我不是肯定为什么,但你无法就call的结果致电text.enter()。相反,它应该可以调用类似的insertNewFeature(text.enter())