根据条件设置节点颜色

时间:2015-05-06 15:50:24

标签: javascript d3.js

我想根据每个节点的状态为每个节点提供一种颜色。例如,如果状态已完成',则节点的颜色应为绿色。如果它正在等待'状态应该是蓝色等等。

为此,我创建了这些css类。类名与状态完全匹配 -



.completed {
    fill: green;
}
.pending {
    fill: blue;
}
.dormant {
    fill: purple;
}




构建节点时,我正在尝试应用名称与状态

匹配的类



 .style("fill", function (d) { return d3.select(this).classed(d.status, true); })




但是,这没有任何影响。

以下是完整的代码



		var links = [
            {source: "Start", target: "Dept Approver", type: "approve", staus: "completed"},
		  {source: "Dept Approver", target: "Amount>20", type: "approve", staus: "completed"},
		  {source: "Amount>20", target: "Div Approver", type: "approve", staus: "completed"},
		  {source: "Amount>20", target: "Section Approver", type: "approve", staus: "completed"},
		  {source: "Amount>20", target: "Dept Approver", type: "reject", staus: "completed"},
		  {source: "Div Approver", target: "End", type: "approve", staus: "dormant"},
		  {source: "Section Approver", target: "End", type: "approve", staus: "pending"}
		];
		
	
		var nodes = {};

		// Compute the distinct nodes from the links.
		links.forEach(function(link) {
		  link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
		  link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
		});

		var width = 960,
			height = 500;

		var force = d3.layout.force()
			.nodes(d3.values(nodes))
			.links(links)
			.size([width, height])
			.linkDistance(80)
			.charge(-300)
			.on("tick", function(d) {
				 path.attr("d", function(d) {
					 var dx = d.target.x - d.source.x,
					 dy = d.target.y - d.source.y,
					 dr = 0;
					 return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
				 });
				 circle.attr("transform", function(d) {
					return "translate(" + d.x + "," + d.y + ")";
				 });
				 text.attr("transform", function(d) {
					return "translate(" + d.x + "," + d.y + ")";
				 });
			})
			.start();

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

		// Per-type markers, as they don't inherit styles.
		svg.append("defs").selectAll("marker")
			.data(["approve", "reject"])
		  .enter().append("marker")
			.attr("id", function(d) { return d; })
			.attr("viewBox", "0 -5 10 10")
			.attr("refX", 15)
			.attr("refY", -1.5)
			.attr("markerWidth", 8)
			.attr("markerHeight", 8)
			.attr("orient", "auto")
		  .append("path")
			.attr("d", "M0,-5L10,0L0,5");

		var path = svg.append("g").selectAll("path")
			.data(force.links())
		  .enter().append("path")
			.attr("class", function(d) { return "link " + d.type; })
			.attr("marker-end", function(d) { return "url(#" + d.type + ")"; });

		var circle = svg.append("g").selectAll("circle")
			.data(force.nodes())
		  .enter().append("circle")
			.attr("r", 8)
            .style("fill", function (d) { return d3.select(this).classed(d.status, true); })
			.call(force.drag);

		var text = svg.append("g").selectAll("text")
			.data(force.nodes())
		  .enter().append("text")
			.attr("x", ".40em")
			.attr("y", 12)
			.text(function(d) { return d.name; });

        var drag = force.drag()
            .on("dragstart", function(d) {
					 d3.select(this).classed("fixed", d.fixed = true);
				 });

 .link {
  fill: none;
  stroke: #666;
  stroke-width: 1.5px;
}

#licensing {
  fill: green;
}

.link.licensing {
  stroke: green;
}

.link.reject {
  stroke-dasharray: 0,2 1;
    stroke: red;
}

circle {
  fill: #ccc;
  stroke: #333;
  stroke-width: 1.5px;
}

text {
  font: 11px sans-serif;
  pointer-events: none;
  text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
}
.fixed {
 /* fill: #00B2EE; */
}
.completed {
    fill: green;
}
.pending {
    fill: blue;
}
.dormant {
    fill: purple;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<body>
    <div id="chart"></div>
</body>
&#13;
&#13;
&#13;

有人可以帮我纠正一下吗?

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题。

  1. 您在示例中的#include"classes.h" int main(){ derivedClass1 example; example.myfunction(1.0); } 中将"status"拼错为"staus"
  2. 您尝试根据状态为节点着色,但由于您提供了来自links的节点数据,因此您将失去force.nodes() 。 (此外,每个status都有link,而不是status。)解决此问题的一种方法是分别存储每个节点的状态:

    node

    然后用它来为节点着色:

    var nodes = {},
    nodeToStatus = {};
    
    // Compute the distinct nodes and node status from the links.
    links.forEach(function(link) {
      link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
      link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
      nodeToStatus[link.source.name] = link.status; <---- unclear which status
      nodeToStatus[link.target.name] = link.status; <---- to use per node
    });
    
  3. 这会给出下面的输出(完整小提琴here)。

    screenshot where nodes are colored by status