javascript - 将变量文件名传递给 topojson

标签 javascript d3.js

在下面的函数中,我这样做是有效的:

    var land = topojson.feature(europe, europe.objects.nuts1);

但如果我这样做,它就会崩溃:

var europe_path = "europe.objects.nuts1";

var land = topojson.feature(europe, europe_path);

出现此错误 Uncaught TypeError: Cannot read property 'length' of undefined

如何将变量传递给topojson?


代码如下:

.border {
  stroke: #000;
  fill: none;

}
.graticule {
  fill: none;
  stroke: #777;
  stroke-width: .5px;
  stroke-opacity: .5;
}

div.tooltip {
  position: absolute;
  text-align: center;
  width: 84px;
  height: 64px;
  padding: 2px;
  font: 12px sans-serif;
  background: lightgrey;
  border: 0px;
  border-radius: 8px;
  pointer-events: none;
}
</style>
<body>

<h1>Administrative Sub-Regions of Europe</h1>

<select id="json_sources" name="json_sources">
    <option value ="nuts1" selected>Source 1</option>
    <option value ="nuts2">Source 2</option>
<!--     <option value ="source3.json">Source 3</option> -->
</select>​


<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/colorbrewer.v1.min.js"></script>
<script src="https://cdn.rawgit.com/rveciana/d3-composite-projections/v0.2.0/composite-projections.min.js"></script>
<script>

var div = d3.select("body").append("div")
        .attr("class", "tooltip")
        .style("opacity", 0);

var width = 600,
    height = 500;

var projection = d3.geo.conicConformalEurope();
var graticule = d3.geo.graticule();

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


// Find new colours here: http://colorbrewer2.org/
var scale =  d3.scale.quantize().domain([10,60]).range(colorbrewer.PuRd[3]);
var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);


    svg.append("path")
        .datum(graticule)
        .attr("class", "graticule")
        .attr("d", path);



var dropdown = d3.select("#json_sources")
var change = function() {
  var source = dropdown.node().options[dropdown.node().selectedIndex].value;


var str1 = source;
var str2 = ".json";
var file = str1.concat(str2);
console.log(file);

var str_a = "europe.objects.";
var str_b = source;
var europe_path = str_a.concat(str_b);
console.log(europe_path);


  d3.json(file, function(error, europe) {

  d3.csv("povertry_rate.csv", function(error, povrate) {

    var europe_path = "europe.objects.nuts1";

    var land = topojson.feature(europe, europe_path);

    data = {};
    povrate.forEach(function(d) {
      data[d.GEO] = d['2013'];
    });


    console.info(data);
    svg.selectAll("path")
      .data(land.features)
      .enter()
      .append("path")
      .attr("d", path)
      .style("stroke","#000")
      .style("stroke-width",".5px")
      .style("fill",function(d){
            var value = data[d.id];
            if (isNaN(value)){
              value = data[d.id.substring(0,2)];
            }
            if (isNaN(value)){
              return "#fff";
            }

            return scale(value);
            })
      .on("mouseover", function(d,i) {
            var value = data[d.id];
            if (isNaN(value)){
              value = data[d.id.substring(0,2)];
            }
            div.transition()
                .duration(200)
                .style("opacity", 0.9);
            div.html("<b>"+d.properties.name+"</b><br/>" + value + "%")
                .style("left", (d3.event.pageX) + "px")
                .style("top", (d3.event.pageY - 28) + "px");
        })
      .on("mouseout", function(d,i) {
          div.transition()
              .duration(500)
              .style("opacity", 0);
        });

        svg
          .append("path")
            .style("fill","none")
            .style("stroke","#000")
            .attr("d", projection.getCompositionBorders());


  });




  })
}

dropdown.on("change", change)
change(); //trigger json on load



</script>






</body>

<!-- // d3.json("nuts2.json", function(error, europe) {

// }); -->

最佳答案

您将 europe_path 定义为字符串“europe.object.nuts1”而不是对象 europe.object.nuts1。删除引号并尝试。

编辑开始****

如果目标是允许用户在两​​层之间切换,您可以使用变量名动态加载 topojson,例如:

Toggle Topojson with file reload

采用与所提供代码类似的方法。

但是,如果目标是允许在两个或更多层之间切换,加载它们一次并切换可见性可能会更容易,如:

Toggle Topojson with visibility change

这通常会更顺畅和更快,因为不需要从重新加载的 topojson 文件重新计算路径。

编辑结束****

关于javascript - 将变量文件名传递给 topojson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40454843/

相关文章:

javascript - 动态更改 react-native-maps 中 Circle 组件的半径并相应地更新 latitudeDelta 和 longitudeDelta 值

javascript - 无法创建堆叠条形图?

javascript - D3 TreeMap - 使用直线而不是对 Angular 线时如何转换链接

javascript - 如何通过js小写所有字符和大写第一个字符?

javascript - 如何通过类比curl请求从JavaScript发出请求

javascript - 将点击事件附加到 Google map

javascript - D3 图表工具 : How to add label at right of target line (additional horizontal line) in column chart

javascript - 为什么一些 jQuery 程序员使用 jQuery 函数,如 $.get、$.getJSON 和 $.when 而不是 $.ajax

d3.js - 用新集替换数据

algorithm - 在包含点的 Voronoi 图中有效地找到多边形