2017-05-27 19 views
1

私はMike BostockのChoropleth(https://bl.ocks.org/mbostock/4060606)に似たマップを作ろうとしています。D3.js JSONでデータを使用するChoropleth

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

.counties { 
    fill: none; 
} 

.states { 
    fill: none; 
    stroke: #fff; 
    stroke-linejoin: round; 
} 

</style> 
<svg width="960" height="600"></svg> 
<script src="https://d3js.org/d3.v4.min.js"></script> 
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script> 
<script src="https://d3js.org/topojson.v2.min.js"></script> 
<script> 

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

var unemployment = d3.map(); 

var path = d3.geoPath(); 

var x = d3.scaleLinear() 
    .domain([1, 10]) 
    .rangeRound([600, 860]); 

var color = d3.scaleThreshold() 
    .domain(d3.range(2, 10)) 
    .range(d3.schemeBlues[9]); 

var g = svg.append("g") 
    .attr("class", "key") 
    .attr("transform", "translate(0,40)"); 

g.selectAll("rect") 
    .data(color.range().map(function(d) { 
     d = color.invertExtent(d); 
     if (d[0] == null) d[0] = x.domain()[0]; 
     if (d[1] == null) d[1] = x.domain()[1]; 
     return d; 
    })) 
    .enter().append("rect") 
    .attr("height", 8) 
    .attr("x", function(d) { return x(d[0]); }) 
    .attr("width", function(d) { return x(d[1]) - x(d[0]); }) 
    .attr("fill", function(d) { return color(d[0]); }); 

g.append("text") 
    .attr("class", "caption") 
    .attr("x", x.range()[0]) 
    .attr("y", -6) 
    .attr("fill", "#000") 
    .attr("text-anchor", "start") 
    .attr("font-weight", "bold") 
    .text("Unemployment rate"); 

g.call(d3.axisBottom(x) 
    .tickSize(13) 
    .tickFormat(function(x, i) { return i ? x : x + "%"; }) 
    .tickValues(color.domain())) 
    .select(".domain") 
    .remove(); 

d3.queue() 
    .defer(d3.json, "https://d3js.org/us-10m.v1.json") 
    .defer(d3.tsv, "unemployment.tsv", function(d) { 
     console.log(d); 
     unemployment.set(d.id, +d.rate); }) 
    .await(ready); 

function ready(error, us) { 
    if (error) throw error; 

    svg.append("g") 
     .attr("class", "counties") 
    .selectAll("path") 
    .data(topojson.feature(us, us.objects.counties).features) 
    .enter().append("path") 
     .attr("fill", function(d) { return color(d.rate = unemployment.get(d.id)); }) 
     .attr("d", path) 
    .append("title") 
     .text(function(d) { return d.rate + "%"; }); 

    svg.append("path") 
     .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })) 
     .attr("class", "states") 
     .attr("d", path); 
} 

</script> 

TSVファイルを使用する代わりに、私のデータにJSONを使用したいと思います。 JSONを使用するようにスクリプトを変換する方法を考えてみましたが、成功しませんでした。現在、私はように構成された例えばJSONを使用しています:

[{"id": "01001", "rate": 5.1}, 
{"id": "01003", "rate": 4.9}, 
{"id": "01005", "rate": 8.6}, 
{"id": "01007", "rate": 6.2}, 
{"id": "01009", "rate": 5.1}, 
{"id": "01011", "rate": 7.1}] 

は最終的に、私が表示され、何年もの範囲をフィルタリングする方法を見つけ出すためにどのような「列」を選択するドロップダウンを持っていると思います。私の最後のJSONは、このようなような構造:

[{ 
    "ID": "02", 
    "Volume": "NULL", 
    "SubCategory": "Other Housing", 
    "Category": "Use", 
    "Type": "PAB", 
    "date": "2013-12-31", 
    "State": "Ohio" 
}, 
{ 
    "ID": "02", 
    "Volume": 24800000, 
    "SubCategory": "Student Loans", 
    "Category": "Use", 
    "Type": "PAB", 
    "date": "2013-12-31", 
    "State": "Ohio" 
}, 
{ 
    "ID": "02", 
    "DateExcelNo": "12/31/14", 
    "Volume": 440200000, 
    "SubCategory": "Mort Rev Bonds", 
    "Category": "Use", 
    "Type": "PAB", 
    "date": "2014-12-31", 
    "State": "Ohio" 
}, 
{ 
    "ID": "02", 
    "Volume": 1205000000, 
    "SubCategory": "Total Cap", 
    "Category": "VolumeCapacity", 
    "Type": "PAB", 
    "date": "2014-12-31", 
    "State": "Ohio" 
}, 
{ "ID": "01", 
    "Volume": "NULL", 
    "SubCategory": "Other Housing", 
    "Category": "Use", 
    "Type": "PAB", 
    "date": "2013-12-31", 
    "State": "Connecticut" 
}, 
{ 
    "ID": "01", 
    "Volume": 24800000, 
    "SubCategory": "Student Loans", 
    "Category": "Use", 
    "Type": "PAB", 
    "date": "2013-12-31", 
    "State": "Connecticut" 
}, 
{ 
    "ID": "01", 
    "DateExcelNo": "12/31/14", 
    "Volume": 440200000, 
    "SubCategory": "Mort Rev Bonds", 
    "Category": "Use", 
    "Type": "PAB", 
    "date": "2014-12-31", 
    "State": "Connecticut" 
}, 
{ 
    "ID": "01", 
    "Volume": 1205000000, 
    "SubCategory": "Total Cap", 
    "Category": "VolumeCapacity", 
    "Type": "PAB", 
    "date": "2014-12-31", 
    "State": "Connecticut" 
}, 
{ 
    "ID": "01", 
    "Volume": 663400000, 
    "SubCategory": "Total Carryforward to Next Year", 
    "Category": "Carryfwd", 
    "Type": "PAB", 
    "date": "2014-12-31", 
    "State": "Connecticut" 
}] 

答えて

3

ここでの問題は非常に簡単です:d3.jsonは行関数を受け入れないありません。あなたはJSONあなたがあなたの質問に共有されていることを考えると、あなたは、単にこの行を変更することができ、あるとしていること

は、私がこれに... ...

.defer(d3.tsv, "unemployment.tsv", function(d) { unemployment.set(d.id, +d.rate); }) 

を信じる:しかし

.defer(d3.json, "unemployment.json", function(d) { unemployment.set(d.id, +d.rate); }) 

これは機能しません。なぜなら、その行関数(2番目のカンマの後のすべてが行関数です)を使用することができないからです。d3.jsonです。

ソリューション

は、あなたのJSONファイルを作成します(ここでは "unemployment.json" という名前)とキューに次の操作を行います。

.defer(d3.json, "unemployment.json") 

その後、ready機能で、負荷をJSONなどということ三番目のパラメータ(ここで私はそれをunを呼んでいる):

function ready(error, us, un) { 

最後に、あなたのを移入しますhttps://bl.ocks.org/anonymous/211bf9775e61c5b07935536ed1926225

PS:::これは、更新bl.ocksある

un.forEach(function(d) { 
    unemployment.set(d.id, +d.rate); 
}); 

(私は唯一の6データポイントを使用してJSONを使用していますので、唯一の6郡は、当然のことながら、色を持っている)、それは良いことですで1つの問題をで尋ねる練習したがって、ドロップダウンの問題について質問する別の質問を投稿することをお勧めします。

関連する問題