Search code examples
javascriptopenlayersd3.js

how to use d3.data().enter().call()


I'm trying to link a d3 project with an open layers project. What I'm trying to do is to use d3 to find out if a given node exists in my DOM.

If it exists, i use transitions. If it does not exist, I need to insert the node via the openlayers API. This is required as the nodes are registered with openlayers. My initial thought was to perform a call on d3.data( ).enter( ).call( myInsertFunction() ) But this does not seem to be working. I hope someone out there can help me or point me in the right direction. I'm fairly new to the 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());

}

Solution

  • Try this jsfiddle: http://jsfiddle.net/Q8b2z/

    I'm not positive why, but you can't call call on the result of text.enter(). Instead it should work to call the similar insertNewFeature(text.enter()).