Javascript 将变量文件名传递给topojson

Javascript 将变量文件名传递给topojson,javascript,d3.js,Javascript,D3.js,在以下功能中,我这样做是可行的: var land = topojson.feature(europe, europe.objects.nuts1); .border { stroke: #000; fill: none; } .graticule { fill: none; stroke: #777; stroke-width: .5px; stroke-opacity: .5; } div.tooltip { position: absolute;

在以下功能中,我这样做是可行的:

    var land = topojson.feature(europe, europe.objects.nuts1);
.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) {

// }); -->
但如果我这样做,它会破坏:

var europe_path = "europe.objects.nuts1";

var land = topojson.feature(europe, europe_path);
.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) {

// }); -->
出现此错误时
uncaughttypeerror:无法读取未定义的属性“length”

.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) {

// }); -->
如何将变量传递给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) {

// }); -->

代码如下所示:

.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) {

// }); -->

.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) {

// }); -->
.border{
行程:#000;
填充:无;
}
.分划{
填充:无;
行程:777;
笔划宽度:.5px;
笔画不透明度:.5;
}
分区工具提示{
位置:绝对位置;
文本对齐:居中;
宽度:84px;
高度:64px;
填充:2px;
字体:12px无衬线;
背景:浅灰色;
边界:0px;
边界半径:8px;
指针事件:无;
}
欧洲行政分区
资料来源1
资料来源2
​
var div=d3.选择(“主体”).追加(“div”)
.attr(“类”、“工具提示”)
.样式(“不透明度”,0);
可变宽度=600,
高度=500;
var projection=d3.geo.conicformaleurope();
var graticule=d3.geo.graticule();
var path=d3.geo.path()
.投影(投影);
//在此处查找新颜色:http://colorbrewer2.org/
var scale=d3.scale.quantize().domain([10,60]).range(colorbrewer.PuRd[3]);
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
追加(“路径”)
.基准面(分划)
.attr(“类别”、“分划”)
.attr(“d”,路径);
var dropdown=d3.选择(“#json_sources”)
var change=function(){
var source=dropdown.node().options[dropdown.node().selectedIndex].value;
var str1=来源;
var str2=“.json”;
var file=str1.concat(str2);
console.log(文件);
var str_a=“europe.objects。”;
var str_b=来源;
var europe_path=str_a.concat(str_b);
控制台日志(欧洲路径);
json(文件,函数(错误,欧洲){
d3.csv(“povertry_rate.csv”),函数(错误,povrate){
var europe_path=“europe.objects.nuts1”;
var land=topojson.feature(欧洲,欧洲路径);
数据={};
povrate.forEach(功能(d){
数据[d.GEO]=d['2013'];
});
控制台信息(数据);
svg.selectAll(“路径”)
.数据(土地、地貌)
.输入()
.append(“路径”)
.attr(“d”,路径)
.style(“笔划”,“#000”)
.style(“笔划宽度”,“0.5px”)
.样式(“填充”,功能(d){
var值=数据[d.id];
如果(isNaN(值)){
值=数据[d.id.子字符串(0,2)];
}
如果(isNaN(值)){
返回“#fff”;
}
返回量表(值);
})
.on(“鼠标悬停”,功能(d,i){
var值=数据[d.id];
如果(isNaN(值)){
值=数据[d.id.子字符串(0,2)];
}
过渡部()
.持续时间(200)
.样式(“不透明度”,0.9);
div.html(“+d.properties.name+”
“+value+“%”) .style(“左”,“d3.event.pageX)+“px”) .style(“top”,(d3.event.pageY-28)+“px”); }) .on(“mouseout”,函数(d,i){ 过渡部() .持续时间(500) .样式(“不透明度”,0); }); svg .append(“路径”) .style(“填充”、“无”) .style(“笔划”,“#000”) .attr(“d”,projection.getCompositionBorders()); }); }) } 下拉列表。打开(“更改”,更改) 改变()//加载时触发json
您正在将europe\u路径定义为字符串“europe.object.nuts1”,而不是对象europe.object.nuts1。删除引用并尝试它

.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) {

// }); -->
编辑开始****

.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) {

// }); -->
如果目标是允许用户在两个层之间切换,则可以使用变量名动态加载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) {

// }); -->

.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) {

// }); -->
它采用的方法与所提供的代码类似

.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) {

// }); -->
但是,如果目标是允许在两个或多个层之间切换,则可能更容易加载它们一次,并切换可见性,如中所示:

.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) {

// }); -->

.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) {

// }); -->
由于不需要从重新加载的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) {

// }); -->

编辑结束****

但是我需要把它作为一个字符串读入,你知道吗?有时它是坚果,有时它是坚果2,所以我想把它作为变量传递。我回答得太快了,没看透。在我点击entre后马上考虑一下,我只是把代码放进去——也许它会让你更好地了解我在做什么。基本上,我希望人们能够在两个地图视图之间切换:/what you mean?
.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) {

// }); -->