2016-05-14 41 views
2

我是流星和D3的初學者,目前我正在嘗試獲得M. Bostock(適應英國和歐洲地圖)的例子之一來工作。 http://bl.ocks.org/mbostock/9656675 創建顯示地圖作品完美,但點擊或者試圖放大時,會出現以下錯誤: Errors after click or zoom, d3.event is null流星和D3:d3.event爲空

令人驚訝的一切工作以http服務器(HTTP服務器這個例子中,當預期-p 8008 & )。

我知道D3完成事件(d3.event is null inside of debounced function)之後刪除該事件的變量,但我不知道是否這也可能導致流星這種行爲(以及如何解決這裏的話) 誰能給我一個提示,爲什麼在Meteor中d3.event總是爲空,以及如何處理這個問題?

我用下面一起來看流星包:

meteor add d3js:d3 
meteor add garrilla:topojson 

這裏是我的HTML:

<head> 
 
\t <title>Map</title> 
 
</head> 
 
<body> 
 
\t <header> 
 
\t \t <h1 id="header-title">Title</h1> 
 
\t </header> 
 

 

 
\t <div id="vis-and-sidebar"> 
 
\t \t <div id="vis"></div> 
 
\t \t <div id="sidebar"></div> 
 
\t </div> 
 

 
\t <div id="fixed-footer"></div> 
 

 
\t <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> 
 
\t <script src="//d3js.org/topojson.v1.min.js"></script> 
 
\t <script> 
 

 
\t var visElem = d3.select("#vis"); 
 

 
    var width = visElem.node().getBoundingClientRect().width, 
 
    height = visElem.node().getBoundingClientRect().height, 
 
    active = d3.select(null); 
 

 
    var projection = d3.geo.mercator() 
 
    .scale(550) 
 
    .translate([270, 1010]); 
 

 
    var zoom = d3.behavior.zoom() 
 
    .translate([0, 0]) 
 
    .scale(1) 
 
    .scaleExtent([1, 8]) 
 
    .on("zoom", zoomed); 
 

 
    var path = d3.geo.path() 
 
    .projection(projection); 
 

 
    var svg = d3.select("#vis").append("svg") 
 
    .attr("width", width) 
 
    .attr("height", height) 
 
    .on("click", stopped, true); 
 

 
    console.log("SVG-Objekt zum Zeichnen:"); 
 
    console.log(svg); 
 

 
    svg.append("rect") 
 
    .attr("class", "background") 
 
    .attr("width", width) 
 
    .attr("height", height) 
 
    .on("click", reset); 
 

 
    var g = svg.append("g"); 
 

 
    svg 
 
    .call(zoom) // delete this line to disable free zooming 
 
    .call(zoom.event); 
 

 
    d3.json("europe.json", function(error, europe) { 
 
     if (error) throw error; 
 

 
     g.selectAll("path") 
 
     .data(topojson.feature(europe, europe.objects.subunits).features) 
 
     .enter().append("path") 
 
     .attr("d", path) 
 
     .attr("class", "feature") 
 
     .on("click", clicked); 
 

 
     g.append("path") 
 
     .datum(topojson.mesh(europe, europe.objects.subunits, function(a, b) { return a !== b; })) 
 
     .attr("class", "mesh") 
 
     .attr("d", path); 
 
    }); 
 

 
    function clicked(d) { 
 
     if (active.node() === this) return reset(); 
 
     active.classed("active", false); 
 
     active = d3.select(this).classed("active", true); 
 

 
     var bounds = path.bounds(d), 
 
     dx = bounds[1][0] - bounds[0][0], 
 
     dy = bounds[1][1] - bounds[0][1], 
 
     x = (bounds[0][0] + bounds[1][0])/2, 
 
     y = (bounds[0][1] + bounds[1][1])/2, 
 
     scale = Math.max(1, Math.min(8, 0.9/Math.max(dx/width, dy/height))), 
 
     translate = [width/2 - scale * x, height/2 - scale * y]; 
 

 
     svg.transition() 
 
     .duration(750) 
 
     .call(zoom.translate(translate).scale(scale).event); 
 
    } 
 

 
    function reset() { 
 
     active.classed("active", false); 
 
     active = d3.select(null); 
 

 
     svg.transition() 
 
     .duration(750) 
 
     .call(zoom.translate([0, 0]).scale(1).event); 
 
    } 
 

 
    function zoomed() { 
 
    console.log(d3.event); 
 
    g.style("stroke-width", 1.5/d3.event.scale + "px"); 
 
    g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")"); 
 
    } 
 

 
// If the drag behavior prevents the default click, 
 
// also stop propagation so we don’t click-to-zoom. 
 
function stopped() { 
 
    if (d3.event.defaultPrevented) d3.event.stopPropagation(); 
 
} 
 

 
</script> 
 

 
</body>

回答

1

使用D3包以及與腳本標籤引用D3造成了這個問題。刪除腳本標記時,每個事件對象都包含預期值。