简体   繁体   English

d3.js如何在国家/地区地图中为每个州创建单独的元素?

[英]d3.js How to create a separate element for each state in a country map?

I want to apply style to different states in a map of India: for now, a darker shade of some color based on certain data. 我想将样式应用于印度地图中的不同州:目前,基于某些数据,某些颜色的阴影较深。

First I think I need to create a separate element for each state, and then set its fill color to a function of my data. 首先,我认为我需要为每个状态创建一个单独的元素,然后将其填充颜色设置为数据的函数。

I'm really new to this. 我真的是新来的。 So this is probably quite easy to answer. 因此,这可能很容易回答。 I can't figure it out. 我不知道。

I tried to copy Bostock's code to create a separate path element for each country in the UK: 我试图复制Bostock的代码以为英国的每个国家/地区创建一个单独的path元素:

svg.selectAll(".subunit")
.data(topojson.feature(uk, uk.objects.subunits).features)
.enter().append("path")
.attr("class", function(d) { return "subunit " + d.id; })
.attr("d", path);

I have a geojson file called indiastates.geojson. 我有一个名为indiastates.geojson的geojson文件。 The states appear to be labeled like so: "NAME_1": "Andaman and Nicobar". 这些州的标签看起来像这样:“ NAME_1”:“ Andaman and Nicobar”。

What does the .data line need to say in my code? .data行在我的代码中需要说什么? I keep getting errors that say topojson (or geojson, if I change topjson to geojson) is undefined. 我不断收到错误消息,指出topojson(如果将topjson更改为geojson,则为geojson)是未定义的。

I also wonder if this is doing almost the same thing? 我也想知道这是否在做几乎相同的事情?

var areas = group.append("path")
.attr("d", path)
.attr("class", "area")
.attr("stroke", "black")
.style("fill", "rgb(255, 102, 102)");

Sounds like what you want to do is create a "choropleth" map of the US If you have a topojson states file, that's a good starting point, but it might need to be converted to CSV. 听起来您想要做的就是创建美国的“ choropleth”地图。如果您有topojson状态文件,这是一个很好的起点,但是可能需要将其转换为CSV。 Unfortunately, it's been 2+ years since I created a choropleth, and I did it in something other than D3js. 不幸的是,距离我创建一个choropleth已经有2年多了,我是用D3js以外的方法来做的。 Fortunately, there are a variety of tutorials. 幸运的是,这里有各种各样的教程。 Just search in Google for "choropleth map for states in D3" (without the quotes) 只需在Google中搜索“ D3中各州的州地图”(不带引号)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM