亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 d3.geoPath(projection) 似乎沒有將正確的屬性“d”添加到“路徑”

使用 d3.geoPath(projection) 似乎沒有將正確的屬性“d”添加到“路徑”

HUX布斯 2023-04-27 16:29:27
我正在嘗試為一個項目創建一張美國地圖。為此,我使用d3.geoAlbersUsa()as 投影和d3.geoPath(projection)as 路徑函數。但是,當將“路徑”附加到我的 svg 時,似乎沒有設置“d”屬性。// Projection from geoAlbersUsalet projection = d3.geoAlbersUsa()                    .translate(width/2,height/2)                    .scale(10000);// Path from geoPath with the previous projectionlet path = d3.geoPath().projection(projection);let svg = d3.select("#svg");// Load in csv and json datad3.csv("state_results.csv", data => {  d3.json("states.json", json => {    // Process data from csv file and json file    ...    ...    ...    // Selecting 'g' element in svg and appending 'path'    d3.select("#map").selectAll("path")                .data(json.features)                .enter()                .append("path")                .attr("d", path)                .style("fill", d => {                    return d.properties.party;                });當我啟動我的服務器時,這是我檢查 svg 元素時顯示的內容:關于為什么"d"沒有在每個路徑中正確設置屬性的任何想法?下面是數組中的一個元素json.features: {"type":"Feature","id":"49","properties":{"name":"Utah","density":34.30},"geometry":{"type":"Polygon","coordinates":[[[-112.164359,41.995232],[-111.047063,42.000709],[-111.047063,40.998429],[-109.04798,40.998429],[-109.053457,39.125316],[-109.058934,38.27639],[-109.042503,38.166851],[-109.042503,37.000263],[-110.499369,37.00574],[-114.048427,37.000263],[-114.04295,41.995232],[-112.164359,41.995232]]]}},javascript網頁格式svgd3.js小路
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

我已經使它與您提供的示例數據一起使用。您需要將 中的數字括起來.translate([width / 2, height / 2])。除此之外,我只需要將比例縮小一點以使其適合屏幕。


const jsonFeatures = [{

  "type": "Feature",

  "id": "49",

  "properties": {

    "name": "Utah",

    "density": 34.30

  },

  "geometry": {

    "type": "Polygon",

    "coordinates": [

      [

        [-112.164359, 41.995232],

        [-111.047063, 42.000709],

        [-111.047063, 40.998429],

        [-109.04798, 40.998429],

        [-109.053457, 39.125316],

        [-109.058934, 38.27639],

        [-109.042503, 38.166851],

        [-109.042503, 37.000263],

        [-110.499369, 37.00574],

        [-114.048427, 37.000263],

        [-114.04295, 41.995232],

        [-112.164359, 41.995232]

      ]

    ]

  }

}];


let width = 500,

  height = 300;


// Projection from geoAlbersUsa

let projection = d3.geoAlbersUsa()

  .translate([width / 2, height / 2])

  .scale(1000);


// Path from geoPath with the previous projection

let path = d3.geoPath().projection(projection);


let svg = d3.select("svg").attr("width", width).attr("height", height);


svg.selectAll("path")

  .data(jsonFeatures)

  .enter()

  .append("path")

  .attr("d", path)

  .style("fill", d => {

    return d.properties.party;

  });

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.js"></script>

<svg></svg>


查看完整回答
反對 回復 2023-04-27
  • 1 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號