2012-04-23 4 views
1

私はd3プロジェクトを開いたレイヤプロジェクトにリンクしようとしています。私がしようとしているのは、d3を使用して、指定されたノードがDOMに存在するかどうかを調べることです。d3.data()の使い方enter()。call()

存在する場合は、トランジションを使用します。 存在しない場合は、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()); 

} 

答えて

2

このjsfiddleをお試しください:http://jsfiddle.net/Q8b2z/

私はなぜ肯定ないんだけど、あなたはtext.enter()の結果にcallを呼び出すことはできません。代わりに、それは同様のinsertNewFeature(text.enter())を呼び出すために働く必要があります。

+0

ありがとうございます!これは機能します。 – Bjarne77

関連する問題