2016-06-12 85 views
2

我遵循標準的「Let’s Make a Map」教程 - 但爲了讓事情變好,我將它與one about Germany合併 - 所以我正在處理稍微不同的數據。莫名其妙地無法在d3.js地圖上渲染城市名稱標籤:超出範圍NAME?

事情到目前爲止工作 - barring this minor hiccup - 但現在我已經到了「#Displaying Places」部分,這是你應該在地圖上顯示城市名稱的地方。

的問題是發生在下面一行:

.text(function(d) { 
     if (d.properties.name!=="Berlin" && 
      d.properties.name!=="Bremen"){ 

       //for some reason this is undefined 
       console.log(d.properties.name); 
       return d.properties.name; 
     } 
    }) 

console.log(d.properties.name);的值總是不確定的,我想不通爲什麼!

我想這是因爲name超出了範圍d - 但我不知道如何解決它。 是嗎?如果是的話 - 如何解決它?如果不是 - 真正的問題是什麼?

這裏是我的代碼如下喜歡 - 這是很簡潔:

<!DOCTYPE html> 
<meta charset="utf-8"> 
<style> 

.subunit{fill:#fff;} 
.subunit.Nordrhein-Westfalen{ fill: #aba; } 
.subunit.Baden-Württemberg{ fill: #bab; } 
.subunit.Hessen{ fill: #bcb; } 
.subunit.Niedersachsen{ fill: #cbc; } 
.subunit.Thüringen{ fill: #cdc; } 
.subunit.Hamburg{ fill: #dcd; } 
.subunit.Schleswig-Holstein{ fill: #ded; } 
.subunit.Rheinland-Pfalz{ fill: #ede; } 
.subunit.Saarland{ fill: #efe; } 
.subunit.Sachsen-Anhalt{ fill: #fef; } 
.subunit.Brandenburg{ fill: #aaa; } 
.subunit.Mecklenburg-Vorpommern{ fill: #bbb; } 
.subunit.Bayern { fill: #ccc; } 
.subunit.Sachsen { fill: #ddd; } 
.subunit.Bremen { fill: #eee; } 
.subunit.Berlin { fill: #fff; } 

.subunit-boundary { 
    fill: none; 
    stroke: #777; 
    stroke-dasharray: 2,2; 
    stroke-linejoin: round; 
} 


.place, 
.place-label { 
    fill: #444; 
    font-size:14px; 
} 

text { 
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
    font-size: 20px; 
    pointer-events: none; 
} 


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

var width = 960, 
    height = 1160; 

var projection = d3.geo.mercator() 
    .center([10.5, 51.35]) 
    .scale(3000) 
    .translate([width/2, height/2]); 

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

var svg = d3.select("body").append("svg") 
    .attr("width", width) 
    .attr("height", height); 


d3.json("de.json", function(error, de) { 

    //colouring the different subunits 
    svg.selectAll(".subunit") 
     .data(topojson.feature(de, de.objects.subunits).features) 
     .enter().append("path") 
     .attr("class", function(d) { 
     // console.log(d.properties.name); 
     return "subunit " + d.properties.name; 
     }) 
     .attr("d", path); 

    //adding a border to the states 
    svg.append("path") 
     .datum(topojson.mesh(de, de.objects.subunits, function(a,b) { 
      if (a!==b || 
       a.properties.name === "Berlin"|| 
       a.properties.name === "Bremen"){ 
        var ret = a; 
       } 
        return ret; 
       })) 
     .attr("d", path) 
     .attr("class", "subunit-boundary"); 

    // add small black dots for populated places 
    svg.append("path") 
     .datum(topojson.feature(de, de.objects.places)) 
     .attr("d", path) 
     .attr("class", "place"); 



    //trying to display names of cities 
    svg.selectAll(".place-label") 
     .data(topojson.feature(de, de.objects.places).features) 
     .enter().append("text") 
     .attr("class", "place-label") 
     .attr("transform", function(d) { 

      //small test 
      //console.log("translate(" + projection(d.geometry.coordinates) + ")"); 

      return "translate(" + projection(d.geometry.coordinates) + ")"; 
     }) 
     .attr("dy", ".35em") 
     .text(function(d) { 
      if (d.properties.name!=="Berlin" && 
       d.properties.name!=="Bremen"){ 

        //for some reason this is undefined 
        console.log(d.properties.name); 
        return d.properties.name; 
      } 
     }) 
     .attr("x", function(d) { 
      return d.geometry.coordinates[0] > -1 ? 6 : -6; 
     }) 
     .style("text-anchor", function(d) { 
      return d.geometry.coordinates[0] > -1 ? "start" : "end"; 
     }); 



}); 

</script> 

Here is the data file

編輯

預計

enter image description here

實際

enter image description here

+0

看看這個答案:http://stackoverflow.com/questions/37635547/interactive-graph-of-europe-using-d3/37644652 – Klaujesi

+0

@Klaujesi這不是真的非常有幫助 - 對這個問題的答案很長,看起來很普遍 - 是否有一個特定的部分,你認爲我應該看看? –

+0

你的topojson'places'沒有名字,只是點的座標。 topojson.feature(de,de.objects.places).features < - 不給你一個。名稱只是點的座標 – Klaujesi

回答

4

裏面你.T opojson你有兩個部分:

  • 屬性:你的縣名稱和多邊形
  • 地方:

    de.objects.subunits 
    

    和:座標點

您訪問的第一個集合第二次收集通過:

de.subunits.places 

文件後加載到sepearte兩個不同的變量使用它:

d3.json("de.json", function(error, de) { 
    var counti = topojson.feature(de, de.objects.subunits) 
    var places = topojson.feature(de, de.objects.places) 

然後referenciate內容加入。功能

.data(counti.features) // <-- to draw your paths and get the .name: München 

.data(places.features) // <-- to draw the circles for the cities: "coordinates": [11.573039376427117, 48.131688134368815] 

麥克的topojson有:

{ 
    "type": "Feature", 
    "properties": { 
    "name": "Ayr" 
    }, 
    "geometry": { 
    "type": "Point", 
    "coordinates": [ 
     -4.617021378468872, 
     55.44930882146421 
    ] 
    } 

,你有:

{ 
    "type": "Feature", 
    "properties": {}, 
    "geometry": { 
    "type": "Point", 
    "coordinates": [ 
     11.573039376427117, 
     48.131688134368815 
    ] 
    } 

邁克的點屬性看起來像日是:

enter image description here

和點座標

enter image description here

你點屬性:

enter image description here

解決方案:

正道

  • 打開你的地理信息系統軟件(ArcGIS的支付,Q-GIS免費)編輯和正確的路徑圖,並指出屬性和出口爲TopoJSON again.-

易方式

  • 轉到:geojson.io加載JSON和添加propertie名稱要貴點(16分,易餅)和TopoJSON保存again.-

enter image description here

現在,你有正確的countie信息刪除一列(你有重複的信息)

enter image description here

+0

我仍然不明白如何將這種見解應用於標有'//嘗試顯示城市名稱'的函數,是否應該更改'return d.properties.name;'行? –

+0

你怎麼看[這個問題](http://stackoverflow.com/questions/37785089/binding-d3-elements-to-json-objects-lost-in-space) –

+0

問題刪除 – Klaujesi

1

@Klaujesi解釋的理由相當不錯。

我只是想通過解決問題來解決問題。

由於功能內沒有任何屬性,因此您可以從de.objects.subunits獲取屬性,如下所示。

.text(function(d, i) { 
//here i is the index of the place. 
//de.objects.subunits.geometries[i].properties will give you the name that you are looking for. 
      d.properties = de.objects.subunits.geometries[i].properties; 
      if (d.properties.name!=="Berlin" && 
       d.properties.name!=="Bremen"){ 

        //for some reason this is undefined 
        console.log(d); 
        return d.properties.name; 
      } 
     }) 

工作代碼here

+0

嗯,那麼 - 你只是改變了'd'代表的含義?這當然是一個聰明的解決方案 - 但我的名字實際上是由於某些原因而不正確的 - 它們顯示 - 但不在正確的位置 –

+0

城市的名稱與「多邊形」相關聯,而與相關聯的狀態相對與'MultiPolygon',現在國家的名字出現在以一種隨機的方式代表首都城市的圓圈附近 –

+0

不是真的d.properties是空白我只是從'de.objects.subunits'設置它的值的原因功能內部的屬性是空白的。也許你可以把你所期望的一個屏幕截圖視爲我沒有看到隨機出現的任何東西。 – Cyril

相關問題