椭圆弧箭头边缘d3力布局

2024-03-27

我正在使用强制布局来创建有向图。 它渲染在画布上。我的示例位于http://jsbin.com/vuyapibaqa/1/edit?html,输出 http://jsbin.com/vuyapibaqa/1/edit?html,output

现在我的灵感来自于
https://bl.ocks.org/mattkohl/146d301c0fc20d89d85880df537de7b0#index.html https://bl.ocks.org/mattkohl/146d301c0fc20d89d85880df537de7b0#index.html

d3 svg 中的资源很少,我正在尝试在画布中获取类似的资源。

http://jsfiddle.net/zhanghuancs/a2QpA/ http://jsfiddle.net/zhanghuancs/a2QpA/

http://bl.ocks.org/mbostock/1153292 http://bl.ocks.org/mbostock/1153292 https://bl.ocks.org/ramtob/3658a11845a89c4742d62d32afce3160 https://bl.ocks.org/ramtob/3658a11845a89c4742d62d32afce3160
http://bl.ocks.org/thomasdobber/9b78824119136778052f64a967c070e0 http://bl.ocks.org/thomasdobber/9b78824119136778052f64a967c070e0 使用 d3 在两个节点之间绘制多条边 https://stackoverflow.com/questions/11368339/drawing-multiple-edges-between-two-nodes-with-d3.

想要添加椭圆弧与箭头连接边。如何在画布中实现这一点。

我的代码:

<!DOCTYPE html>
<html>
<head>
        <title>Sample Graph Rendring Using Canvas</title>
        <script src="https://rawgit.com/gka/randomgraph.js/master/randomgraph.js"></script>
        <script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
    <script>
        var graph = {}//randomgraph.WattsStrogatz.beta(15, 4, 0.06);

    graph.nodes = [{"label":"x"} , {"label":"y"}];
    graph.edges = [{source:0,target:1},{source:0,target:1},
                   {source:1,target:0}]

        var canvas = null
        var width = window.innerWidth,
            height = window.innerHeight;
        canvas = d3.select("body").append("canvas").attr("width",width).attr("height",height);

        var context = canvas.node().getContext("2d");


        force = d3.forceSimulation()
                .force("link", d3.forceLink().id(function(d) { 
                     return d.index;
                })).force("charge", d3.forceManyBody())
                .force("center", d3.forceCenter(width / 2, height / 2));

        force.nodes(graph.nodes);
        force.force("link").links(graph.edges).distance(200);

        var detachedContainer = document.createElement("custom");
            dataContainer = d3.select(detachedContainer);

        link = dataContainer.selectAll(".link").data(graph.edges)
              .enter().append("line").attr("class", "link")
              .style("stroke-width", 2)

        node = dataContainer.selectAll(".node").data(graph.nodes)
              .enter().append("g");

          var circles = node.append("circle")
              .classed("circle-class", true)
              .attr("class", function (d){ return "node node_" + d.index;})
              .attr("r", 5)
              .attr("fill", 'red')
              .attr("strokeStyle", 'black');

        d3.timer(function(){
            context.clearRect(0, 0, width, height);

            // draw links
            link.each(function(d) {
              context.strokeStyle = "#ccc";
              /***** Elliptical arcs *****/
              context.stroke(new Path2D(linkArc(d)));
              /***** Elliptical arcs *****/
            });

            context.lineWidth = 2;
            node.each(function(d) {

              context.beginPath();
              context.moveTo(d.x, d.y);
              var r = d3.select(this).select("circle").node().getAttribute('r');   

              d.x = Math.max(30, Math.min(width - 30, d.x));
              d.y = Math.max(30, Math.min(height - 30, d.y));         
              context.closePath();
              context.arc(d.x, d.y, r, 0, 2 * Math.PI);

              context.fillStyle = d3.select(this).select("circle").node().getAttribute('fill');
              context.strokeStyle = d3.select(this).select("circle").node().getAttribute('strokeStyle');
              context.stroke();
              context.fill();

              context.beginPath();
              context.arc(d.x + 15, d.y-20, 5, 0, 2 * Math.PI);
              context.fillStyle = "orange";
              context.strokeStyle = "orange";
              var data = d3.select(this).data();
              context.stroke();
              context.fill();
              context.font = "10px Arial";
              context.fillStyle = "black";
              context.strokeStyle = "black";
              context.fillText(parseInt(data[0].index),d.x + 10, d.y-15);
            });

        });

        circles.transition().duration(5000).attr('r', 20).attr('fill', 'orange');

        canvas.node().addEventListener('click',function( event ){
           console.log(event)
            // Its COMING ANY TIME INSIDE ON CLICK OF CANVAS
        });

        /***** Elliptical arcs *****/
        function linkArc(d) {
          var dx = d.target.x - d.source.x,
              dy = d.target.y - d.source.y,
              dr = Math.sqrt(dx * dx + dy * dy);
          return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
        }
        /***** Elliptical arcs *****/
    </script>
</body>
</html>  

用箭头从一个圆到另一个圆画弧

基本问题

这两个点必须是随机的(从任意位置到任意位置)x1,y1 和 x2,y2。您需要控制与点之间的距离无关的弯曲量(即,如果点之间的距离为 100 像素或 10 像素,则弯曲量相同)

因此输入是

x1,y1 // as start
x2,y2 // as end
bend  // as factor of distance between points 
      // negative bends up (to right) 
      // positive bends down (to left of line)
arrowLen  // in pixels
arrowWidth // in pixels,
arrowStart // boolean if arrow at start
arrowEnd   // boolean if arrow at end.

基本方法步骤

  1. 找到两个端点之间的中点。
  2. 获取点之间的距离
  3. 获取从头到尾的归一化向量。
  4. 旋转标准90度
  5. 将距离乘以弯曲乘以旋转法线并添加到中点以找到圆弧上的中点
  6. 用这 3 个点找到适合所有 3 个点的圆的半径。
  7. 使用半径找到圆弧的中心
  8. 从中心找到起点和终点的方向
  9. 使用箭头 len 找到箭头的角长度,现在我们有了半径
  10. 从内部箭头或开始/结束处绘制圆弧(取决于是否显示箭头)
  11. 从平边的点沿着圆弧中心的线绘制箭头

其他问题。

我假设您希望线条从一个圆到下一个圆。因此,您需要指定圆心和圆的半径。这将需要两个附加参数,一个用于起始圆半径,一个用于结束圆半径。

还有一个问题是当两点距离很近(即重叠)时该怎么办。除了不绘制线条和箭头(如果它们不合适)之外,没有真正的解决方案。

解决方案作为演示

该演示具有随时间变化大小的圆,有 6 个弧,其弯曲值分别为 0.1、0.3、0.6 和 -0.1、-0.3、-0.6。移动鼠标来改变结束圆的位置。

完成这一切的函数称为drawBend我在那里添加了很多注释,还有一些注释行可以让您更改当起点和终点之间的距离发生变化时弧线的变化方式。如果取消注释,则设置该变量b1(您将弧上的中点分配给 x3,y3) 您必须注释掉其他分配

求圆弧半径和圆心的解决方案很复杂,由于对称性,很可能有更好的解决方案。该部分会找到一个圆来容纳任意 3 个点(如果不是全部在一条线上),因此可能还有其他用途。

Update我找到了一种更好的方法来找到圆弧半径,从而找到中心点。对称性提供了一组非常方便的相似三角形,因此我可以将函数缩短 9 行。我已经更新了演示。

圆弧被绘制为描边,箭头被绘制为填充。

它相当快,但如果您计划实时绘制许多 100 个数字,您可以通过让弧开始然后返回共享一些计算来进行优化。如果交换起点和终点,从起点到终点的弧线会向另一个方向弯曲,并且有许多值保持不变,因此在绘图 2 的大约 75% CPU 负载下可以获得两条弧线

const ctx = canvas.getContext("2d");

const mouse  = {x : 0, y : 0, button : false}
function mouseEvents(e){
	mouse.x = e.pageX;
	mouse.y = e.pageY;
	mouse.button = e.type === "mousedown" ? true : e.type === "mouseup" ? false : mouse.button;
}
["down","up","move"].forEach(name => document.addEventListener("mouse"+name,mouseEvents));




// x1,y1 location of a circle start
// x2,y2 location of the end circle
// bend factor. negative bends up for, positive bends down. If zero the world will end 
// aLen is Arrow head length in pixels
// aWidth is arrow head width in pixels
// sArrow boolean if true draw start arrow
// eArrow  boolean if true draw end  arrow
// startRadius = radius of a circle if start attached to circle
// endRadius = radius of a circle if end attached to circle
function drawBend(x1, y1, x2, y2, bend, aLen, aWidth, sArrow, eArrow, startRadius, endRadius){
    var mx, my, dist, nx, ny, x3, y3, cx, cy, radius, vx, vy, a1, a2;
    var arrowAng,aa1,aa2,b1;
    // find mid point
    mx = (x1 + x2) / 2;  
    my = (y1 + y2) / 2;
    
    // get vector from start to end
    nx = x2 - x1;
    ny = y2 - y1;
    
    // find dist
    dist = Math.sqrt(nx * nx + ny * ny);
    
    // normalise vector
    nx /= dist;
    ny /= dist;
    
    // The next section has some optional behaviours
    // that set the dist from the line mid point to the arc mid point
    // You should only use one of the following sets
    
    //-- Uncomment for behaviour of arcs
    // This make the lines flatten at distance
    //b1 =  (bend * 300) / Math.pow(dist,1/4);

    //-- Uncomment for behaviour of arcs
    // Arc bending amount close to constant
    // b1 =  bend * dist * 0.5

    b1 = bend * dist

    // Arc amount bend more at dist
    x3 = mx + ny * b1;
    y3 = my - nx * b1;
   
    // get the radius
    radius = (0.5 * ((x1-x3) * (x1-x3) + (y1-y3) * (y1-y3)) / (b1));

    // use radius to get arc center
    cx = x3 - ny * radius;
    cy = y3 + nx * radius;

    // radius needs to be positive for the rest of the code
    radius = Math.abs(radius);

    


    // find angle from center to start and end
    a1 = Math.atan2(y1 - cy, x1 - cx);
    a2 = Math.atan2(y2 - cy, x2 - cx);
    
    // normalise angles
    a1 = (a1 + Math.PI * 2) % (Math.PI * 2);
    a2 = (a2 + Math.PI * 2) % (Math.PI * 2);
    // ensure angles are in correct directions
    if (bend < 0) {
        if (a1 < a2) { a1 += Math.PI * 2 }
    } else {
        if (a2 < a1) { a2 += Math.PI * 2 }
    }
    
    // convert arrow length to angular len
    arrowAng = aLen / radius  * Math.sign(bend);
    // get angular length of start and end circles and move arc start and ends
    
    a1 += startRadius / radius * Math.sign(bend);
    a2 -= endRadius / radius * Math.sign(bend);
    aa1 = a1;
    aa2 = a2;
   
    // check for too close and no room for arc
    if ((bend < 0 && a1 < a2) || (bend > 0 && a2 < a1)) {
        return;
    }
    // is there a start arrow
    if (sArrow) { aa1 += arrowAng } // move arc start to inside arrow
    // is there an end arrow
    if (eArrow) { aa2 -= arrowAng } // move arc end to inside arrow
    
    // check for too close and remove arrows if so
    if ((bend < 0 && aa1 < aa2) || (bend > 0 && aa2 < aa1)) {
        sArrow = false;
        eArrow = false;
        aa1 = a1;
        aa2 = a2;
    }
    // draw arc
    ctx.beginPath();
    ctx.arc(cx, cy, radius, aa1, aa2, bend < 0);
    ctx.stroke();

    ctx.beginPath();

    // draw start arrow if needed
    if(sArrow){
        ctx.moveTo(
            Math.cos(a1) * radius + cx,
            Math.sin(a1) * radius + cy
        );
        ctx.lineTo(
            Math.cos(aa1) * (radius + aWidth / 2) + cx,
            Math.sin(aa1) * (radius + aWidth / 2) + cy
        );
        ctx.lineTo(
            Math.cos(aa1) * (radius - aWidth / 2) + cx,
            Math.sin(aa1) * (radius - aWidth / 2) + cy
        );
        ctx.closePath();
    }
    
    // draw end arrow if needed
    if(eArrow){
        ctx.moveTo(
            Math.cos(a2) * radius + cx,
            Math.sin(a2) * radius + cy
        );
        ctx.lineTo(
            Math.cos(aa2) * (radius - aWidth / 2) + cx,
            Math.sin(aa2) * (radius - aWidth / 2) + cy
        );
        ctx.lineTo(
            Math.cos(aa2) * (radius + aWidth / 2) + cx,
            Math.sin(aa2) * (radius + aWidth / 2) + cy
        );
        ctx.closePath();
    }
    ctx.fill();
}



/** SimpleUpdate.js begin **/
// short cut vars 
var w = canvas.width;
var h = canvas.height;
var cw = w / 2;  // center 
var ch = h / 2;
var globalTime = new Date().valueOf();  // global to this 

// main update function
function update(timer){
    globalTime = timer;
    if(w !== innerWidth || h !== innerHeight){  // resize if needed
      cw = (w = canvas.width = innerWidth) / 2;
      ch = (h = canvas.height = innerHeight) / 2;
    }    
    ctx.setTransform(1,0,0,1,0,0); // reset transform
    ctx.globalAlpha = 1;           // reset alpha
    ctx.clearRect(0,0,w,h);

    var startRad = (Math.sin(timer / 2000) * 0.5 + 0.5) * 20 + 5;
    var endRad = (Math.sin(timer / 7000) * 0.5 + 0.5) * 20 + 5;
    ctx.lineWidth = 2;
    ctx.fillStyle = "white";
    ctx.strokeStyle = "black";
    ctx.beginPath();
    ctx.arc(cw,ch,startRad,0,Math.PI * 2);
    ctx.fill();
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(mouse.x,mouse.y,endRad,0,Math.PI * 2);
    ctx.fill();
    ctx.stroke();

    ctx.lineWidth = 2;
    ctx.fillStyle = "black";
    ctx.strokeStyle = "black";
    
    
    
    drawBend(cw,ch,mouse.x,mouse.y,-0.1,10,10,true,true,startRad + 1,endRad + 1);
    drawBend(cw,ch,mouse.x,mouse.y,-0.3,10,10,true,true,startRad + 1,endRad + 1);
    drawBend(cw,ch,mouse.x,mouse.y,-0.6,10,10,true,true,startRad + 1,endRad + 1);
    drawBend(cw,ch,mouse.x,mouse.y,0.1,10,10,true,true,startRad + 1,endRad + 1);
    drawBend(cw,ch,mouse.x,mouse.y,0.3,10,10,true,true,startRad + 1,endRad + 1);
    drawBend(cw,ch,mouse.x,mouse.y,0.6,10,10,true,true,startRad + 1,endRad + 1);


    requestAnimationFrame(update);
}
requestAnimationFrame(update);
canvas { position : absolute; top : 0px; left : 0px; }
<canvas id="canvas"></canvas>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

椭圆弧箭头边缘d3力布局 的相关文章

随机推荐

  • 使用Python直接向USB发送信号

    如何使用 Python 将 USB 端口连接设置为高或低 这可用于定制 USB 设备 例如 假设我有一个 LED 连接到 USB 端口 数据线 现在通过代码我想闪烁它或控制它 现在 这可以通过使用任何微控制器 Arduino Raspber
  • Android studio 的aspectj - AJDT 插件在哪里

    如何为 android studio 安装 AJDT 我知道 Eclipse 有一个 但我在 android studio 中找不到任何东西 我想做的是在启动之前对我的每个方法调用进行安全检查 我已经从这个网站尝试过AJDT http ec
  • 如何使用 ant build.xml 导入现有的 java web 项目

    我正在尝试使用导入功能将现有的 java web 项目 使用 ant build xml 构建 导入到 Eclipse 中 但 Eclipse 无法将其识别为项目 如何将项目导入到eclipse中 编辑 我所做的是将项目导入为 Java 项
  • 从 GitHub 上已删除的分支恢复作为拉取请求发送的提交

    我做了一件蠢事 I fork在 GitHub 上发布了一个存储库 我做了一些改变 commit把它们放在我的叉子上 我将此提交作为拉取请求回到原来的仓库 愚蠢的部分来了 我deleted 我的叉子 原始存储库的所有者要求在接受拉取请求之前对
  • 图例显示带有 geom_vline 的意外黑线

    在查看了 StackOverflow 上提供的一些答案后 我一直试图添加一个额外的图例 但不知何故我无法完成这项工作 我正在使用以下代码 x breaks lt seq as Date 2010 1 1 as Date 2015 4 1 m
  • 函数隐私和单元测试 Haskell

    你如何处理 Haskell 中的函数可见性和单元测试 如果导出模块中的每个函数以便单元测试可以访问它们 则可能会导致其他人调用不应出现在公共 API 中的函数 我想用 LANGUAGE CPP 然后围绕出口 ifdef LANGUAGE C
  • git svn:svn 的密码未存储

    我正在使用 git svn 对中央远程 svn 存储库使用 git SVN 存储库使用带有自签名证书的 https 一切正常 只有一个令人讨厌的例外 只要我直接使用 svn 密码就会被记住 所以只能在第一个命令中输入密码 当使用git sv
  • window.onload 与

    两者到底有什么区别window onload事件和onload事件的body标签 我什么时候使用哪个以及如何正确完成 window onload myOnloadFunc and 是不同的使用方式同一个事件 Using window onl
  • 如何使用 ScriptTags 为 shopify 开发 Rails 应用程序

    我在 Heroku 中部署了一个 Shopify 应用程序 并使用 Rails 开发 我需要从任何 Shopify 商店的前端调用 JavaScript 函数 我读过这篇文章 http www shopify com technology
  • Appcelerator Titanium:Facebook 图片上传失败

    我的 Titanium 软件中从 Facebook 上传图像时出现错误 每次我想从我的应用程序上传图像时 我都会收到以下信息 失败 v2 1 及更高版本已弃用 REST API 但如果我在 KitchenSink 示例应用程序中尝试相同的代
  • 在设计模式中编辑集合的最简单方法?

    最简单的编辑方法是什么persist像这样的集合decimal or List
  • 在 Python 上分析字符串输入直到达到某个字母

    我需要帮助来尝试编写程序的某个部分 这个想法是 一个人输入一堆乱码 程序会读取它 直到它到达 感叹号 例如 input Type something 人物类型 wolfdo65gtornado salmontiger223 如果我要求程序打
  • `enable_query_strings` 无法正常工作

    我正在尝试使用 CodeIgniter 和 xdebug 当我输入以下 URL 时 http localhost redux index php xdebug 运行良好 当我访问以下网址时 http localhost redux inde
  • QT后台进程进行键盘输入嗅探

    我正在开发一个简单的应用程序 该应用程序将在后台运行并捕获用户的键盘输入 如键盘记录器 但用于 LAN 我正在发送 UDP 数据包来传输击键 但从后台进程捕获键盘输入的问题似乎仍然无法解决 所以需要帮助 如果您想在 Windows 上执行此
  • 子类化 NSNumber

    我想向 NSNumber 类添加一个属性 因此我必须对其进行子类化 文档指出我必须重写所有 NSValue 原始方法 由于 NSValue 文档没有说明哪些方法是原始方法 所以我认为这两个可能是实例化的原始方法 initWithBytes
  • 将文本表转换为 pandas 数据框

    很多时候 当我尝试回答 Stackoverflow 上的问题时 问题包含一个表 我必须将其转换为 pandas 数据框才能进行处理 例如 在这个问题中 http stackoverflow com questions 43172116 pa
  • 使用验证器 jQuery 验证 Click 事件上的表单

    我正在使用 jQuery validate 来验证输入 我的代码 button click function form validate rules phone required true number true rangelength 7
  • 测试 Google Analytics iOS SDK

    有人找到了在 iOS 上测试 Google Analytics 的好方法吗 SDK 非常简单 但文档没有讨论如何测试或验证 该库在模拟器上或运行调试构建配置时的行为是否有所不同 我使用此委托方法设置了委托 GANTrackerDelegat
  • 紧凑框架中的网络浏览器

    我想用一个WebBrowser NET Compact Framework 3 5 项目中的组件 我面临着关于此的相互矛盾的信息 如果我只是尝试使用它 我会得到以下异常 System Threading ThreadStateExcepti
  • 椭圆弧箭头边缘d3力布局

    我正在使用强制布局来创建有向图 它渲染在画布上 我的示例位于http jsbin com vuyapibaqa 1 edit html 输出 http jsbin com vuyapibaqa 1 edit html output 现在我的