2017-02-26 2 views
0

나는 두 가지 문제가 있지만, 그들이 관련이있을 수 있습니다 : 나는에있는 두 개의 JSON 파일 중 하나를로드하는 동안 나는 콜백 함수를 호출하는 경우 작업을 d3.queue를 얻을 수D3의 choropleth 큐 및 채우기 문제

  1. 같은 시간. d3.queue를 사용하지 않고 다른 하나의 d3.json을 중첩 시키면지도가 나타나지만 채우기 문제 (아래 설명 참조)는 여전히 해결되지 않습니다.
  2. d3.queue가 있는지 여부에 관계없이 미국 각 주마다 고유 한 채우기 색상을 표시 할 수 없습니다. 나는 이것이 각 주마다 별도의 "컨테이너"를 만드는 데 실패하고 있기 때문에 이것이라고 생각합니다. 브라우저 속성에서 "경로 클래스 = 상태"컨테이너 위로 마우스를 가져 가면 전체지도가 강조 표시되고 개별 상태는 표시되지 않습니다. 따라서 모든 색상이 서로 위에 적용되지만 가장 어두운 그늘이 지배적이며 그 중 하나만 보입니다.

이은 d3.queue

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title>Choropleth Map</title> 
 
<style> 
 

 
.counties { 
 
    fill: none; 
 
} 
 

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

 
</style> 
 
</head> 
 
<body> 
 
<script src="https://d3js.org/d3.v3.min.js"></script> 
 
<script src="https://d3js.org/topojson.v1.min.js"></script> 
 
<script src="https://d3js.org/d3-queue.v3.min.js"></script> 
 

 
<script> 
 
var margin = {top: 10, right: 200, bottom: 30, left: 50}, 
 
    width = 960 - margin.left - margin.right, 
 
    height = 600 - margin.top - margin.bottom;  
 
    
 
var svg = d3.select("body").append("svg") 
 
    .attr("width", width + margin.left + margin.right) 
 
    .attr("height", height + margin.top + margin.bottom) 
 
    .append("g") 
 
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");  
 
    
 
var earnings = d3.map(); 
 

 
var path = d3.geo.path(); 
 

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

 
var color = d3.scale.threshold() 
 
    .domain(d3.range(2, 10)) 
 
.range(['#f7fcfd','#e5f5f9','#ccece6','#99d8c9','#66c2a4','#41ae76','#238b45','#006d2c','#00441b']); 
 

 
// Load data 
 
d3.queue() 
 
    .defer(d3.json, "us.json") 
 
    .defer(d3.json, "median_earnings.json", loadjson) //removing loadjson from here brings up the map of U.S. states without any fill. Keeping it shows a blank page 
 
    .await(ready); 
 

 
function loadjson (error, earnings) { 
 
    if (error) throw error; 
 
    earnings.forEach(function(d) { 
 
      d.id = +d.id; 
 
      d.median = +d.median_earnings; 
 
    });  
 
} 
 
    
 
function ready(error, us) { 
 
    if (error) throw error; 
 

 
    svg.append("path") 
 
     .attr("class", "states")  
 
     .datum(topojson.feature(us, us.objects.states), function(a, b) { return a !== b; }) 
 
     .attr("d", path); 
 
    
 
     svg.selectAll("path") 
 
     .attr("class", "states")  
 
     .datum(earnings)  
 
     .attr("fill", function(d,i) { 
 
      return color(d[i].median); 
 
      }); 
 
} 
 

 
     
 
</script> 
 
</body> 
 
</html>

내가 사용 us.json 파일 here과 median_earnings here입니다과 버전입니다.

는 그리고이 d3.queue 파일에 ID를 일치하지 않기 때문에 그것은이다

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title>Choropleth Map of College Data</title> 
 
<style> 
 

 
.counties { 
 
    fill: none; 
 
} 
 

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

 
</style> 
 
</head> 
 
<body> 
 
<script src="https://d3js.org/d3.v3.min.js"></script> 
 
<script src="https://d3js.org/topojson.v1.min.js"></script> 
 
<script src="https://d3js.org/d3-queue.v3.min.js"></script> 
 
<script> 
 
var margin = {top: 10, right: 200, bottom: 30, left: 50}, 
 
    width = 960 - margin.left - margin.right, 
 
    height = 600 - margin.top - margin.bottom;  
 
    
 
var svg = d3.select("body").append("svg") 
 
    .attr("width", width + margin.left + margin.right) 
 
    .attr("height", height + margin.top + margin.bottom); 
 

 
var earnings = d3.map(); 
 

 
var path = d3.geo.path(); 
 

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

 
var color = d3.scale.threshold() 
 
    .domain([15000, 18000, 21000, 24000, 27000, 30000, 33000]) .range(['#f7fcfd','#e5f5f9','#ccece6','#99d8c9','#66c2a4','#41ae76','#238b45','#005824']); 
 

 
d3.json("median_earnings.json", function loadjson(error, earnings) { 
 
    if (error) throw error; 
 
    earnings.forEach(function(d) { 
 
      d.id = +d.id; 
 
      d.median = +d.median_earnings; 
 
      
 
    d3.json("us.json", function ready(error, us) { 
 
     if (error) throw error; 
 
    
 
    svg.append("path") 
 
     .attr("class", "states")  
 
     .datum(topojson.feature(us, us.objects.states, function(a, b) { return a !== b; })) 
 
     .attr("d", path) 
 
     .style("stroke", "grey"); 
 
     
 
    svg.selectAll("path") 
 
     .attr("class", "states")  
 
     .datum(earnings)  
 
     .attr("fill", function(d,i) { 
 
      return color(d[i].median); 
 
      }); 
 
    });  
 
}) 
 
}); 
 

 
</script> 
 
</body> 
 
</html>

답변

0

와 버전입니다.

+0

당신은 그것에 대해 자세히 설명해 주시겠습니까? 나는 네가 의미하는 것을 이해하지 못한다. – PythonGuy