TopoJSON ‹‹properties›› field, ¿cómo obtener valores con d3.js?
Tengo el archivo TopoJSON, se parece a esto:
{"type":"Topology","transform":{"scale":[0.03600360003702599,0.0040674580654071245],"translate":[-179.9999967702229,41.18684289776669],"objects":{"country":{"type":"GeometryCollection","geometries":[{"type":"Polygon","arcs":[[0]],"properties":{"AREA":29809500,"PERIMETER":21822.4,"region":"XYZ"}},…
Quiero usar valores de propiedades ("ÁREA", "PERÍMETRO", "región") en mi código d3.js. Intenté obtener algunos valores, pero no funcionó:
d3.json("map_topo.json", function(error, map) {
svg.append("path")
.datum(topojson.object(map, map.objects.country).geometries)
.attr("d", path)
.style("fill", function(d) {
if (d.properties.region == "XYZ")
{return "red"}
else {return "gray"}
})
¿Qué estoy haciendo mal?
UPD: el problema se resolvió con la ayuda de @ChrisWilson, el problema estaba en anexaruno camino, en lugar de seleccionarTODOS caminos. Código de trabajo (para topojson.v0.js):
d3.json("map_topo.json", function(error, map) {
svg.selectAll("path")
.data(topojson.object(map, map.objects.country).geometries)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
if (d.properties.region == "XYZ")
{return "red"}
else {return "gray"}
});
Para uso topojson.v1.jstopojson.feature
método (ver comentarios abajo).