2016-07-29 5 views
1

私はd3jsで中国の地図を描くことができますが、チャリティーは満たされません。コード:あなたが見ることができるように、私はconsole.log(rateById);が含まれているd3js choroplethは記入されません

<script> 
var margin = {top: 0, right: 0, bottom: 0, left: 0}, 
    width = parseInt(d3.select('#map').style('width')), 
    height = width*0.7; 

var places = <?php echo json_encode($provinces); ?> 

var minimum = d3.min(places, function(d) { return d.rate; }), 
    maximum = d3.max(places, function(d) { return d.rate; }); 

var minimumColor = "#BFD3E6", 
    maximumColor = "#88419D"; 

var color = d3.scale 
    .linear() 
    .domain([minimum, maximum]) 
    .range([minimumColor, maximumColor]); 

var projection = d3.geo.mercator() 
    .center([109, 31]) 
    .scale(width*0.85) 
    .translate([width/1.8, height/1.5]) 

var path = d3.geo.path() 
    .projection(projection); 

var svg = d3.select("#map") 
    .append("svg") 
    .attr("width", width) 
    .attr("height", height) 
    .append("g") 
    .attr("transform", "translate(0,0)"); 

d3.json("data/china_simplify.json", function(error, root) { 

    if (error) 
    return console.error(error); 
    console.log(root.features); 

    var rateById = {}; 
    places.forEach(function(d) { rateById[d.id] = +d.rate; }); 

    console.log(rateById); 

    svg.append("g") 
     .attr("class", "provinces") 
    svg.selectAll("path") 
     .data(root.features) 
     .enter() 
     .append("path") 
     .attr("d", path) 
     .style("fill", function(d) { return color(rateById[d.id]); }); 

}); 
</script> 

、返します。

Object {1: 5829, 2: 2181, 3: 33904, 4: 14182, 5: 23290, 6: 135365, 7: 56757, 8: 21148, 9: 463618, 10: 1895, 11: 8408, 12: 12318, 13: 2990, 14: 25601, 15: 12720, 16: 41816, 17: 27993, 18: 47288, 19: 15422, 20: 24968, 21: 34515, 22: 36199, 23: 44708, 24: 20886, 25: 16609, 26: 70220, 27: 117876, 28: 84413, 29: 14271, 30: 23102, 31: 19010, 32: 16097, 33: 3783, 34: 7616} 

キーは、州のIDを指します。

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":1,"name":"甘肃"},"geometry":{"type":"Polygon","coordinates":[[[104.35851932200904,37.4],[104.46450768428224,37.440247301072134],[104.68950687084538,37.41192861571304],[104.76474775590418,37.25049144112714],[104.92241255059872,37.096754055055584],[105.18017459508144,36.97213633912071], [...] 

ない、それが正しく記入されていない理由を確認してください:ここでchina_simplify.jsonファイルの抜粋です! return color(rateById[d.id]);を削除してredと置き換えると、すべて赤色で塗りつぶされます。これは、問題がcolor機能に関連していることを示しています。

EDIT

私はconsole.log(color(rateById[1]));を実行すると、それは#bfd2e5を返します。これは、color関数自体に何も問題はないが、何らかの理由で16進数コードがマップに書き込まれていないことを示しています。

ここにはおそらく何か問題があります。.style("fill", function(d) { return color(rateById[d.id]); })です。私はそれがthis exampleと一致するので、それを理解することができないようです。私は一緒にjsfiddle置かれている2

EDIT:rateByIdでhttps://jsfiddle.net/o06edvuf/

答えて

2

私はそれを見つけました。

.style("fill", function(d) { 
     return color(rateById[d.properties.id]); }) 
+0

ありがとう、それはそれを修正した。不思議なことに、なぜあなたは「不動産」を必要としますか? –

+0

印刷d、あなたは知っているでしょう。 –

1

は、金利は、IDの上に集約されますが、ドメインは、最小値と最大率を考慮して設定されています。 rateByIdを使用して色のドメインを設定する:

var minimum = d3.min(d3.values(rateById)), 
maximum = d3.max(d3.values(rateById)); 
+0

私はそれが問題ではないと思います。なぜなら、カラー機能がうまく機能するからです。上記の私の編集を参照してください。助けを感謝します! –

+0

あなたはフィドルを作成できますか? –

+0

確かに、ここに行く:https://jsfiddle.net/o06edvuf/ –

関連する問題