D3.js 无法从geojson文件检索GM_代码

D3.js 无法从geojson文件检索GM_代码,d3.js,D3.js,在看了(很好的教程!)之后,我决定绘制一张我们省的地图,并为该省的所有直辖市涂上颜色。我做了形状文件的工作,并成功地用黑色投影了整个省。您能否帮助理解svg.selectAll(“.subunit”)函数,以便我可以修改它以从geojson文件中提取GM_代码?如果需要,我可以发送geojson文件 <!DOCTYPE html> <meta charset="utf-8"> <style> .GM_CODE.GM0003 { fill: #ddc; } .

在看了(很好的教程!)之后,我决定绘制一张我们省的地图,并为该省的所有直辖市涂上颜色。我做了形状文件的工作,并成功地用黑色投影了整个省。您能否帮助理解svg.selectAll(“.subunit”)函数,以便我可以修改它以从geojson文件中提取GM_代码?如果需要,我可以发送geojson文件

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

<style>
.GM_CODE.GM0003 { fill: #ddc; }
.GM_CODE.GM0005 { fill: #cdd; }
.GM_CODE.GM0007 { fill: #cdc; }
.GM_CODE.GM0009 { fill: #dcd; }

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>

var width = 960,
    height = 640;

var projection = d3.geo.albers()
    .center([0, 53.2])
    .rotate([-6.5, 0])
    .parallels([50, 60])
    .scale(40000)
    .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("groningen.json", function(error, json) {
  svg.selectAll(".subunit")
        .data(topojson.feature(json, json.objects.gemeente).features)
    .enter.append("path")
      .attr("class", function(d) { return "GM_CODE " + d.GM_CODE; })
      .attr("d", path);
});

</script>

.GM#u CODE.GM0003{fill:#ddc;}
.GM#u CODE.GM0005{fill:#cdd;}
.GM#u CODE.GM0007{fill:#cdc;}
.GM#u CODE.GM0009{fill:#dcd;}
可变宽度=960,
高度=640;
var projection=d3.geo.albers()
.center([0,53.2])
.旋转([-6.5,0])
.平行线([50,60])
.规模(40000)
.翻译([宽度/2,高度/2]);
var path=d3.geo.path()
.投影(投影);
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
json(“groningen.json”,函数(错误,json){
svg.selectAll(“.subunit”)
.data(topojson.feature(json,json.objects.gemeente).features)
.enter.append(“路径”)
.attr(“类”,函数(d){返回“GM_代码”+d.GM_代码;})
.attr(“d”,路径);
});

首先,如果您使用的是GeoJSON文件,那么应该直接使用这些功能

svg.selectAll('.subunits')
    .data(json.features)
    .enter()
    .append('path')
    .attr('class', function(d) { return 'GM_CODE ' + d.properties.GM_CODE; })
    .attr('d', path);
假设您的要素具有属性,属性为
GM\u code
,则可以使用此代码分配类
GM\u code
GM0003
(例如)。如果您添加GeoJSON文件的示例,我可以在这里更新确切的代码