【问题标题】:Dynamically size points on D3 TopoJSON mapD3 TopoJSON 地图上的动态大小点
【发布时间】:2014-01-24 08:32:30
【问题描述】:

我使用 D3 在 TopoJSON 地图上绘制了点,代码如下:

<!DOCTYPE html>
<meta charset="utf-8">

<head>

<script src="js/d3.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>

<style>
path {
  stroke: white;
  stroke-width: 0.25px;
  fill: grey;
}
</style>

    </head>

<body>

<script>
var width = 960,
    height = 500;

var projection = d3.geo.mercator()


var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

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

var g = svg.append("g");

// load and display the World
d3.json("data/world-110m2.json", function(error, topology) {

// load and display the cities
d3.json("data/commodities3.json", function(error, data) {
    g.selectAll("circle")
       .data(data)
       .enter()
       .append("circle")
       .attr("cx", function(d) {
               return projection([d.location_lon, d.location_lat])[0];})
       .attr("cy", function(d) {
               return projection([d.location_lon, d.location_lat])[1];})
       .attr("r", 4)
       .style("fill", "green");







});

    //plot the path

g.selectAll("path")
      .data(topojson.object(topology, topology.objects.countries)
          .geometries)
    .enter()
      .append("path")
      .attr("d", path)
});

// zoom and pan
var zoom = d3.behavior.zoom()
    .on("zoom",function() {
        g.attr("transform","translate("+ 
            d3.event.translate.join(",")+")scale("+d3.event.scale+")");
        g.selectAll("circle")
            .attr("d", path.projection(projection));
        g.selectAll("path")  
            .attr("d", path.projection(projection)); 

  });

svg.call(zoom)

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

我现在希望根据d.commodity_text. 的值来调整这些点的大小。因此,例如,如果商品文本等于“铁”,那么圆圈会更大吗?谢谢。

【问题讨论】:

    标签: javascript d3.js mapping topojson


    【解决方案1】:

    您只需将半径的静态值替换为函数即可:

    .attr("r", function(d) {
      if(d.commodity_text == "Iron") {
        return 6;
      } else {
        return 4;
      }
    });
    

    【讨论】:

    • 完美运行。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-11-11
    • 2016-08-04
    • 2016-11-14
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-23
    • 2013-11-25
    相关资源
    最近更新 更多