Javascript 美国地图中带箭头的网络线

Javascript 美国地图中带箭头的网络线,javascript,d3.js,Javascript,D3.js,我试图在美国网络地图上给出一个箭头标志来指示链接的方向。我已经跟踪了这个链接和许多其他链接,但无法实现同样的效果。 我已经为此创建了一个小提琴手。请看看这个,并请提供一些解决方案来实现这一点。 提前谢谢 Amit Sah对于创建标记,您应该附加一个,而不是:@GerardoFurtado感谢您的快速回复。您能帮我解决一个我面临的问题吗?即我想将节点拖放到任何位置,但我无法实现。在同一个fiddler中,我已经给出了我正在尝试的代码。请帮助我。如果您有其他问题,请发布另一个问题。@Gerardo

我试图在美国网络地图上给出一个箭头标志来指示链接的方向。我已经跟踪了这个链接和许多其他链接,但无法实现同样的效果。 我已经为此创建了一个小提琴手。请看看这个,并请提供一些解决方案来实现这一点。

提前谢谢


Amit Sah

对于创建标记,您应该附加一个
,而不是
:@GerardoFurtado感谢您的快速回复。您能帮我解决一个我面临的问题吗?即我想将节点拖放到任何位置,但我无法实现。在同一个fiddler中,我已经给出了我正在尝试的代码。请帮助我。如果您有其他问题,请发布另一个问题。@GerardoFurtado我已经发布了这个问题。很抱歉在我之前的回复中没有让您知道这个链接。查看链接,如果您有任何解决方案,请让我知道。
var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height"),
    radius = 10;

/*svg.call(d3.zoom().on("zoom", function () {
   svg.attr("transform", d3.event.transform);
}));*/
var color = d3.scaleOrdinal(d3.schemeCategory20);

var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { return d.node_id; }).strength(1) )
    //.force("charge", d3.forceManyBody())
    .force("collide", d3.forceCollide(radius + 1).iterations(4))
    .force("center", d3.forceCenter(width / 2, height / 2));


// projection definition
var projection = d3.geoAlbers()
    .scale(1280)
    .translate([width / 2, height / 2]);

// path generator definition for major cities, including point radius
var path = d3.geoPath()
    .projection(projection)
    .pointRadius(2);

// draws the states
d3.json("https://raw.githubusercontent.com/LogicalInsightscg/Web-Mobile-Dashboard/master/us_new.json", function(error, us_new) {
  if (error) return console.error(error);
svg.selectAll(".states")
    .data(topojson.feature(us_new, us_new.objects.states).features)
  .enter().append("path")
    .attr("class", function(d) { return "states " + d.id; })
    .attr("d", path);

// adding state boundaries
    svg.append("path")
    .datum(topojson.mesh(us_new, us_new.objects.states))
    .attr("d", path)
    .attr("class", "state-boundary");


d3.json("https://raw.githubusercontent.com/LogicalInsightscg/Web-Mobile-Dashboard/master/nodesandlinks.json", function(error, graph) {
  if (error) throw error;


// build the arrow.
svg.append("svg:defs").selectAll("marker")
    .data(["end"])      // Different link/path types can be defined here
  .enter().append("svg:marker")    // This section adds in the arrows
    .attr("id", String)
    .attr("viewBox", "0 -5 10 10")
    .attr("refX", 15)
    .attr("refY", -1.5)
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("orient", "auto")
  .append("svg:line")
    .attr("d", "M0,-5L10,0L0,5");

   var link = svg.append("g")
      .attr("class", "links")
    .selectAll("line")
    .data(graph.links)
    .enter().append("line")
      .attr("stroke-width", function(d) { return Math.sqrt(d.referrals); })
       .attr("marker-end", "url(#end)");




  var node = svg.append("g")
      .attr("class", "nodes")
    .selectAll("circle")
    .data(graph.nodes)
    .enter().append("circle")
      .attr("r", 5)
      .attr("fill", function(d) { return color(d.group); })
      //.attr("fixed", true)
      .attr("cx", function(d) {
                   return projection([d.longitude, d.latitude])[0];
           })
           .attr("cy", function(d) {
                   return projection([d.longitude, d.latitude])[1];
           })
/*       .call(d3.drag()
          .on("end", dragended)); */
       .call(d3.drag()
          .on("start", dragstarted)
          .on("drag", dragged)
          .on("end", dragended)); 



   simulation
      .nodes(graph.nodes)
      .on("tick", ticked);  

   simulation.force("link")
      .links(graph.links);



     node.attr('fx', function(d) { return projection([d.longitude, d.latitude])[0]; })
        .attr('fy', function(d) { return projection([d.longitude, d.latitude])[1]; });

     link.attr('x1', function(d) { return projection([d.source.longitude, d.source.latitude])[0]; })
        .attr('y1', function(d) { return projection([d.source.longitude, d.source.latitude])[1]; })
        .attr('x2', function(d) { return projection([d.target.longitude, d.target.latitude])[0]; })
        .attr('y2', function(d) { return projection([d.target.longitude, d.target.latitude])[1]; });

  node.append("title")
      .text(function(d) { return d.node_name; });

   function ticked() {
    link
        .attr("distance", 10)
//        .attr("x1", function(d) { return d.source.x; })
//        .attr("y1", function(d) { return d.source.y; })
        .attr('x1', function(d) { return projection([d.source.longitude, d.source.latitude])[0]; })
        .attr('y1', function(d) { return projection([d.source.longitude, d.source.latitude])[1]; })
        .attr('x2', function(d) { return projection([d.target.longitude, d.target.latitude])[0]; })
        .attr('y2', function(d) { return projection([d.target.longitude, d.target.latitude])[1]; });

    node.attr('cx', function(d) {if (d.group > 0) { return projection([d.longitude, d.latitude])[0]; }
                                             else  { return d.x; }})
        .attr('cy', function(d) {if (d.group > 0) { return projection([d.longitude, d.latitude])[1]; }
                                             else  { return d.y; }});

  }


/*   function ticked() {
     link.attr('x1', function(d) { return projection([d.source.longitude, d.source.latitude])[0]; })
        .attr('y1', function(d) { return projection([d.source.longitude, d.source.latitude])[1]; })
        .attr('x2', function(d) { return projection([d.target.longitude, d.target.latitude])[0]; })
        .attr('y2', function(d) { return projection([d.target.longitude, d.target.latitude])[1]; });

     node.attr('r', width/100)
        .attr('cx', function(d) { return projection([d.longitude, d.latitude])[0]; })
        .attr('cy', function(d) { return projection([d.longitude, d.latitude])[1]; });

  } */
 });
});

function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  d.fx = d.x;
  d.fy = d.y;
}

function dragged(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function dragended(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}