首页 \ 问答 \ 标记D3中的网络节点(Labeling network nodes in D3)

标记D3中的网络节点(Labeling network nodes in D3)

我需要在D3之外的节点上绘制标签(名称)。 这是我的代码:

<script src="//d3js.org/d3.v3.min.js"></script>
<script>

var width = window.innerWidth,
    height = 400;

var color = d3.scale.category20();

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(40)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .call(d3.behavior.zoom().on("zoom", redraw))
        .append('g');

    function redraw() {
      svg.attr("transform",
          "translate(" + d3.event.translate + ")"
          + " scale(" + d3.event.scale + ")");}

          var drag = force.stop().drag()
          .on("dragstart", function(d) {
            d3.event.sourceEvent.stopPropagation(); 
          });

d3.json("/static/net.json", function(error, graph) {
  if (error) throw error;

  force
      .nodes(graph.nodes)
      .links(graph.links)
      .start();

  var link = svg.selectAll(".link")
      .data(graph.links)
    .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value); });

  var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", function(d) { return d.degree; })
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

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

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });

});

</script>

出于某种原因,只有当我将鼠标悬停在节点上而不是始终被绘制时,才会出现标签。

当我更换:

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

有:

node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });

即使悬停,也不会出现任何标签。

我的代码中是否有一些明显的东西?


I need to draw labels (names) besides nodes with D3. Here's my code:

<script src="//d3js.org/d3.v3.min.js"></script>
<script>

var width = window.innerWidth,
    height = 400;

var color = d3.scale.category20();

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(40)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .call(d3.behavior.zoom().on("zoom", redraw))
        .append('g');

    function redraw() {
      svg.attr("transform",
          "translate(" + d3.event.translate + ")"
          + " scale(" + d3.event.scale + ")");}

          var drag = force.stop().drag()
          .on("dragstart", function(d) {
            d3.event.sourceEvent.stopPropagation(); 
          });

d3.json("/static/net.json", function(error, graph) {
  if (error) throw error;

  force
      .nodes(graph.nodes)
      .links(graph.links)
      .start();

  var link = svg.selectAll(".link")
      .data(graph.links)
    .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value); });

  var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", function(d) { return d.degree; })
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

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

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });

});

</script>

For some reason, labels only appear if I hover over nodes with the mouse, instead of always being drawn.

When I replace:

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

with:

node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });

No label appear at all, even when hovering.

Is there something obvious I am missing in this code?


原文:https://stackoverflow.com/questions/35440823
更新时间:2024-02-04 20:02

最满意答案

我使用Visual Studio作为编译器,并使用emacs作为编辑器。

只需安装Visual Studio C ++ 2010 Express Edition。 然后,我所做的就是编写一个nmake Makefile并从Visual Studio命令提示符调用nmake(可从“程序”菜单访问)。 这适用于较小的项目。

有关更多详细信息,请参阅http://msdn.microsoft.com/en-us/library/f35ctcxw.aspx

对于大型项目,您可以在Visual Studio中创建解决方案,并使用emacs作为编辑器。 您也可以从命令提示符调用msbuild来构建解决方案。

此外,visual studio命令提示符只是调用vcvars.bat(或类似的东西)来设置必要的环境。 我猜你可能可以修改emacs shell来指向在启动时运行此bat文件的cmd实例?


I use Visual Studio as a compiler with emacs as an editor.

Just install Visual Studio C++ 2010 Express Edition. Then what I do is write an nmake Makefile and invoke nmake from the Visual Studio Command Prompt (accessible from the Programs menu). This works fine for smaller projects.

See http://msdn.microsoft.com/en-us/library/f35ctcxw.aspx for more details.

For larger projects you can create a solution in Visual Studio and just use emacs as an editor. You can also invoke msbuild from the command prompt to build the solution.

Also, the visual studio command prompt just invokes vcvars.bat (or something like it) to set up the necessary environment. I guess you might be able to modify the emacs shell to point to an instance of cmd that has run this bat file on startup?

相关问答

更多
  • 您需要在计算机设置的环境变量中设置HOME (如果我没有记错的话),或者将init文件放到默认目录(只需启动Emacs,然后按Cx Cf ~/查看实际目录)。 据我_emacs ,在Windows上,init文件可以被称为_emacs ... You need to set HOME in environment variables in Computer settings (if I remember correctly), or put init file to default directory (j ...
  • 我使用aspell在Windows上使用emacs进行拼写检查。 看看我的.emacs文件,我可以看到这些配置变量。 (custom-set-variables '(ispell-dictionary "british") '(ispell-program-name "H:\\bin\\aspell\\bin\\aspell.exe")) 我使用安装向导安装了aspell。 MS-$ ispell-buffer flyspell-mode等都可以正常工作。 I use aspell for ...
  • 我使用EmacsW32 ,它的效果很好。 编辑:我现在使用常规的GNU Emacs 24,见下文。 有关详细信息,请参阅其EmacsWiki页面 。 对我来说,最大的优点是: 它有一个版本的emacsclient启动Emacs服务器,如果没有服务器正在运行(打开所有的文件在同一个Emacs窗口) 它包括几个有用的包,如Nxml 它有一个Windows安装程序,或者你可以从源代码构建它 关于XEmacs,根据Steve Yegge的这篇文章 : 总而言之,我认为XEmacs的市场份额要低得多,性能更差,更多的 ...
  • 你必须具体说明你的意思是“其余的”。 除了对象检查员(我知道的),emacs很容易做到以上所有: 编辑(明显) 编译器 - 只需运行Mx compile并输入你的编译命令。 从那里,你可以只是Mx compile并使用默认值。 Emacs将捕获C / C ++编译器错误(最好使用GCC),并帮助您导航到带有警告或错误的行。 调试 - 类似地,当你想调试时,键入Mx gdb ,它将创建一个具有特殊绑定的gdb缓冲区 文档查找 - emacs具有出色的CScope绑定用于代码导航。 对于其他文档:Emacs还有 ...
  • 使用Cx o, other-window 。 请参阅http://www.gnu.org/software/emacs/manual/html_node/emacs/Other-Window.html 。 Use C-x o, other-window. See http://www.gnu.org/software/emacs/manual/html_node/emacs/Other-Window.html.
  • 我使用Visual Studio作为编译器,并使用emacs作为编辑器。 只需安装Visual Studio C ++ 2010 Express Edition。 然后,我所做的就是编写一个nmake Makefile并从Visual Studio命令提示符调用nmake(可从“程序”菜单访问)。 这适用于较小的项目。 有关更多详细信息,请参阅http://msdn.microsoft.com/en-us/library/f35ctcxw.aspx 。 对于大型项目,您可以在Visual Studio中创建 ...
  • 可能会调整您的系统路径,将其指向您安装gdb的位置。 probably adjust your system path to point it where you have gdb installed.
  • 对于后人来说,让Emacs从.emacs.d文件夹中读取数据的.emacs.d是首先手动删除.emacs.d文件夹中的所有.emacs文件(对于我来说,rlrner / AppData / Roaming)。 这将迫使Emacs在rlrner / AppData / Roaming / .emacs.d中寻找init.el,就像在OS X中一样。 这里的文档: https : //www.emacswiki.org/emacs/DotEmacsDotD For posterity, the way to h ...
  • (shell-command "ver" t) ;; outputs version in current buffer (shell-command "ver" t) ;; outputs version in current buffer
  • 从w32-pass-lwindow-to-system的文档: 请注意,左侧“Windows”键与其他键的某些组合由Windows在低级别捕获,因此在Emacs中绑定它们将不起作用。 例如, -r总是弹出Windows运行对话框, - 弹出“系统属性”对话框等。但是,请参阅`w32-phantom-key-code'的文档字符串。 从w32-phantom-key-code的文档: 用于生成“幻像”按键的虚拟键码。 值是0到255之间的数字。生成幻像按键是为 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)