我正在尝试将 d3 项目与开放层项目链接。我想要做的是使用 d3 来找出给定节点是否存在于我的 DOM 中。
如果存在,我使用过渡。如果它不存在,我需要通过 openlayers API 插入节点。这是必需的,因为节点已向 openlayers 注册。我最初的想法是调用 d3.data( ).enter( ).call( myInsertFunction() ) 但这似乎不起作用。我希望有人可以帮助我或为我指明正确的方向。我对 d3 库相当陌生。
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());
}