【问题标题】:How to use .defer with an internal data object d3 topoJSON map?如何将 .defer 与内部数据对象 d3 topoJSON 映射一起使用?
【发布时间】:2018-02-20 05:44:37
【问题描述】:

阅读所有文档后,我不明白如何将内部 json 对象加载到 .defer 中以用于 d3 topoJSON 地图。我的数据对象从后端 API 调用传递到前端 JS 文件。一旦进入前端,它就位于一个类似于下面的变量 dataSet 的数据对象中:

var dataSet = [
{county: "021", county_name: "Franklin", county_TR_code: "53021", year: "2015", value: 7.5},
{county: "023", county_name: "Garfield", county_TR_code: "53023", year: "2015", value: 6.1},
{county: "025", county_name: "Grant", county_TR_code: "53025", year: "2015", value: 7.1},
{county: "027", county_name: "Grays Harbor", county_TR_code: "53027", year: "2015", value: 8.9},
{county: "029", county_name: "Island", county_TR_code: "53029", year: "2015", value: 6},
{county: "031", county_name: "Jefferson", county_TR_code: "53031", year: "2015", value: 7.1}]

我已经能够只获得华盛顿县的 topoJSON 地图来渲染,但我对如何从我的 rawData 变量设置就业数据有点困惑。任何帮助都会很棒,谢谢!

D3 代码如下:

// Retrieve data from API
var rawData = dataSet

// Define funcitons to find min and max of data set
var minValue = getMin(rawData)
var maxValue = getMax(rawData)
var step = (maxValue-minValue)/7


// create color range based on Min,Max and Step Value
var employment_domain = [minValue,0,0,0,0,0,0,0];
for(var i=1; i< employment_domain.length; i++){
  var tmp = (employment_domain[i-1] + step)
  employment_domain[i]= tmp
}
console.log('domain', employment_domain)

var employment_color = d3.scaleThreshold()
  .domain(employment_domain)
  .range(d3.schemeBlues[7])


var svg = d3.select("svg");
var path = d3.geoPath();

// Create variable to hold data 
// Dictionary of key value pairs {id: value} --> {censusBlockID: value}
var employmentData = d3.map();

// used to asynchronously load topojson maps and data
d3.queue()
  .defer(d3.json, "../maps/wa_counties.json") // load in topoJSON map data
  //
  .defer(rawData, function(d){
    console.log('d',d)
    employmentData.set(d.county_TR_code, +d.value) // (first refers to county code, second refers to employment value)
  } ) // load in data
  .await(ready) // create callback function

// Callback function
function ready(error, data){
  if (error) throw error;

  // if no error returned retrieve data from topojson file
  // used to refer to the features of the county data
  var county_data = topojson.feature(data, {
    type:"GeometryCollection",
    geometries: data.objects.tl_2016_53_tract.geometries
  });

  // identify projection and path
  var projection = d3.geoAlbersUsa()
    .fitExtent([[20,20], [460, 580]], county_data) // assigns ([padding], [width and height], dataObject)

    // define path
    var geoPath = d3.geoPath()
      .projection(projection)

    // draw map
    d3.select("svg.main_data_point").selectAll("path")
      .data(county_data.features) //pass in data
      .enter()
      .append("path")
      .attr("d", geoPath) // pass in geoPath object created
      .attr("fill", function(d){
        return employment_color(d.value = employmentData.get(d.properties.GEOID)); // pass in employement value and ID from topoJSON map
      }) // fill in the data


}

【问题讨论】:

    标签: javascript json d3.js


    【解决方案1】:
    var data = d3.map();
    
    yourinternaldata.forEach(function(d){
        console.log('d',d)
        data.set(d.code, +d.total) // (first refers to county code, second refers to employment value)
            })
    

    这样使用

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 2018-03-01
      • 2019-06-08
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 2016-10-10
      相关资源
      最近更新 更多