【问题标题】:How to add custom colors in D3 nodes of a graph如何在图形的 D3 节点中添加自定义颜色
【发布时间】:2013-12-08 03:23:21
【问题描述】:

我正在将 D3 api 用于从父节点形成几个节点的图我想以每个父节点具有固定颜色而子节点具有不同颜色的方式为整个图的节点着色即根节点始终为红色,左侧子节点为蓝色,右侧节点为绿色,如果只有一个子节点,则为绿色。我正在使用这个 api,,

<!DOCTYPE html>
<html>
 <head>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?1.27.1"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.geom.js?1.27.1"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?1.27.1"></script>
    <style type="text/css">

 line.link {
  stroke: #ccc;
}

circle.node {
 fill: #000;
 stroke: #fff;
 stroke-width: 1.5px;
}

    </style>
  </head>
  <body>
    <script type="text/javascript">

   var w = 960,
  h = 500,
  r = d3.scale.sqrt().domain([0, 20000]).range([0, 20]);

 var force = d3.layout.force()
.gravity(.01)
.charge(-120)
.linkDistance(60)
.size([w, h]);

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

d3.xml("flare.xml", "application/xml", function(xml) {
  var nodes = self.nodes = d3.select(xml).selectAll("*")[0],
    links = self.links = nodes.slice(1).map(function(d) {
      return {source: d, target: d.parentNode};
     });

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

  var link = svg.selectAll("line.link")
  .data(links)
.enter().append("svg:line")
  .attr("class", "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; });

  var node = svg.selectAll("circle.node")
  .data(nodes)
  .enter().append("svg:circle")
  .attr("class", "node")
  .attr("cx", function(d) { return d.x; })
  .attr("cy", function(d) { return d.y; })
  .attr("r", function(d) { return r(d.textContent) || 5; })
  .call(force.drag);

    force.on("tick", function() {
    nodes[0].x = w / 2;
    nodes[0].y = h / 2;

   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>
  </body>
 </html>

谁能帮帮我

【问题讨论】:

    标签: javascript svg d3.js force-layout


    【解决方案1】:

    SVG 有自己的 CSS 属性,其中之一是 fill。它设置 SVG 元素的填充颜色。

    D3 的使用方法如下:

    var node = svg.selectAll("circle.node")
      .data(nodes)
      .enter().append("svg:circle")
      .style("fill", function (d) { return '#1f77b4'; })
      .attr("class", "node")
      .attr("cx", function(d) { return d.x; })
      .attr("cy", function(d) { return d.y; })
      .attr("r", function(d) { return r(d.textContent) || 5; })
      .call(force.drag);
    

    这里所有节点的颜色值 (#1f77b4) 都相同。如果您想使用特定算法为节点着色,D3 附带预定义的categorical color scales

    编辑:这里是a related SO question。它的答案之一has a good custom ordinal color scale example

    【讨论】:

    • 感谢您的回答,它正在工作,我将所有节点都设为蓝色..如果我想在节点中添加自定义颜色,我将不得不写一个条件?
    • 是的。您必须通过更改.style("fill", function (d) { return '#1f77b4'; }) 中的返回值来决定如何为每个节点着色。您可以直接将颜色放入 XML 文件中,或者将颜色映射到 XML 中已有的属性值(参见 example)。
    • 我无法在此处更改数据或无法使用 XML 文件,我必须设置一个条件来决定哪个节点是根节点,哪个节点是子节点
    • var nodes = svg.selectAll("circle.node") .data(force.nodes()) .enter().append("circle") .attr("class", "node ") .attr("r", 8) //.style("fill", function(d,i) { return d3.scale.category20(i); }) .style("fill",function(d, i){ return d3.scale.category20(i); }) .call(force.drag);我正在做这个你能从这里帮忙吗??
    • 这是要走的路,但d3.scale.category20(i) 行不通。应该是d3.scale.category20()(i),因为d3.scale.category20()会返回缩放函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2021-12-07
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多