- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 D3 api 按顺序显示一些节点,这些节点以相同的颜色进入页面。当我将鼠标悬停在一个节点上时,它会变成其他颜色。但我想要在加载时为节点(不同于其他节点)使用固定颜色。我正在使用这个 api.. 请帮助我...
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node circle {
stroke: white;
stroke-width: 1.5px;
opacity: 1.0;
}
line {
stroke: black;
stroke-width: 1.5px;
stroke-opacity: 1.0;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
data = {
nodes : [ {
size : 10
}, {
size : 5
}, {
size : 2
}, {
size : 3
} ],
links : [ {
source : 0,
target : 1
}, {
source : 0,
target : 2
} ]
}
var mouseOverFunction = function(d,i) {
var circle = d3.select(this);
//$ fill = d3.scale.category20();
node.transition(500).style("opacity", function(o) {
return isConnected(o, d) ? 1.0 : 0.2;
}).style("fill", function(o) {
if (isConnectedAsTarget(o, d) && isConnectedAsSource(o, d)) {
fillcolor = 'green';
} else if (isConnectedAsSource(o, d)) {
fillcolor = 'red';
} else if (isConnectedAsTarget(o, d)) {
fillcolor = 'blue';
} else if (isEqual(o, d)) {
fillcolor = "hotpink";
} else if(isNot(o,d)){
//here the output will shown when no mouse event is occured
}
return fillcolor;
});
link
.transition(500)
.style("stroke-opacity", function(o) {
return o.source === d || o.target === d ? 1 : 0.2;
})
.transition(500)
.attr(
"marker-end",
function(o) {
return o.source === d || o.target === d ? "url(#arrowhead)"
: "url()";
});
circle.transition(500).attr("r", function() {
return 1.4 * node_radius(d)
});
}
var mouseOutFunction = function() {
var circle = d3.select(this);
node.transition(500);
link.transition(500);
circle.transition(500).attr("r", node_radius);
}
function isConnected(a, b) {
return isConnectedAsTarget(a, b) || isConnectedAsSource(a, b)
|| a.index == b.index;
}
function isConnectedAsSource(a, b) {
return linkedByIndex[a.index + "," + b.index];
}
function isConnectedAsTarget(a, b) {
return linkedByIndex[b.index + "," + a.index];
}
function isEqual(a, b) {
return a.index == b.index;
}
//here i have trying to make root node a separate color and the child nodes two separate colors
function isNot(a,b){
return d3.scale.category20()(i);
}
function tick() {
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("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
function node_radius(d) {
return Math.pow(40.0 * d.size, 1 / 3);
}
var width = 1000;
var height = 500;
var nodes = data.nodes
var links = data.links
var force = d3.layout.force().nodes(nodes).links(links).charge(-3000)
.friction(0.6).gravity(0.6).size([ width, height ]).start();
var linkedByIndex = {};
links.forEach(function(d) {
linkedByIndex[d.source.index + "," + d.target.index] = true;
});
var svg = d3.select("body").append("svg").attr("width", width).attr(
"height", height);
var link = svg.selectAll("line")
.data(links)
.enter()
.append("line");
var node = svg.selectAll(".node")
.data(nodes)
.enter()
.append("g")
.style("fill", function (d) {
return '#1f77b4';
})
.attr("class", "node")
.attr("cx",function(d) {
return d.x;
})
.attr("cy",function(d) {
return d.y;
})
//.attr("fill",function(d){return color(d.range);})
.call(force.drag);
node.append("circle")
.attr("r", node_radius)
.on("mouseover", mouseOverFunction)
.on("mouseout", mouseOutFunction);
svg.append("marker")
.attr("id", "arrowhead")
.attr("refX", 6 + 7) // Controls the shift of the arrow head along the path
.attr("refY", 2)
.attr("markerWidth", 6)
.attr("markerHeight", 4)
.attr("orient", "auto")
.append("path")
.attr("d", "M 0,0 V 4 L6,2 Z");
link.attr("marker-end", "url()");
force.on("tick", tick);
</script>
</body>
最佳答案
我认为您的代码已经快完成了。在鼠标悬停时,您应该修改节点的“填充”css 属性。
为此,在 d3 中您必须使用:
node.append("circle")
.attr("r", node_radius)
.on("mouseover",mouseOverFunction)
就像你一样。在 moueOverFunction 中,您有两种选择来获取给定节点的属性:
1) 从节点的属性获取颜色:
function mouseoverFunction(node) {
if(node.size<10) {
return 'blue'
} else {
return 'red'
}
}
2) 从节点在数据数组中的位置获取颜色:
function mouseoverFunction(node,index) {
if(index<2) {
// Will color the two first nodes blue
return 'blue'
} else {
// All others will be red
return 'red'
}
}
最后我会针对你的具体问题做:
function mouseoverFunction(node,index) {
if(index<3) {
return ['red','green','blue'][index]
} else {
// index should be less than 3
return 'black'
}
}
关于javascript - 如何在 D3 force layout on load 中为 3 个节点指定 3 种不同的自定义颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20441421/
我试图理解两者之间的区别 git push --force 和 git push --force-with-lease 我的猜测是后者只推送到远程如果远程提交了本地分支没有? 最佳答案 force 用
我在将项目发布到本地系统时收到此错误 Copying file obj\Debug\build.force to obj\Release\Package\PackageTmp\obj\Debug\bu
这个例子的描述:http://bl.ocks.org/mbostock/4062045 (见下图),声明它是“带电粒子和 Spring 的物理模拟,使相关 Angular 色更接近。” 我只是好奇该代
请不要标记重复的问题。 大家好, 我正在执行 NSURLAuthenticationMethodClientCertificate,我在其中使用以下代码。如果我不使用 swiftlint,哪个代码没问
我似乎无法删除文件/文件夹,而无需为所有人输入 [A]。我错过了什么? Get-Childitem "C:\Users\*\AppData\Local\Temp\*" -ErrorAction S
我一直在尝试编写在 Streams 上运行的程序和它们的属性,但我觉得即使是最简单的事情我也被困住了。当我在标准库的 Codata/Streams 中查看 repeat 的定义时,我发现了一个我在 A
我正在尝试使用 symfony2 创建一个下载文件的链接。它确实下载了一个文件,但它没有用,因为它是零八位字节。我不知道如何让它工作。有人知道怎么做吗? 文件位于web/uploads/documen
我需要为MySQLd打开网络,但是每次这样做,服务器都会被强行淘汰。一些卑鄙的密码猜测脚本开始在服务器上运行,在端口3306上打开连接并永久尝试随 secret 码。 我该如何阻止这种情况的发生? 对
Azure Functions 是否可以强制通过 HTTPS 进行连接? 我没有在应用程序设置中看到它,也没有看到任何对 Azure Functions 的 web.config 的引用。 最佳答案
我正在使用 Firebird 数据库并正在尝试以下 sql,但每次它返回 0,而不是 0.61538(等等)。 SELECT (COUNT(myfield)/26) totalcount FROM m
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我想要一个永远未定义的属性: var foo = {bar:undefined} 如果有人稍后尝试更改属性栏,那么它也应该导致未定义。 foo.bar = 'someValue'// foo.bar/
我有课,Target无法更改,具有通用约束。我想从没有约束的泛型类中构建该类的实例。下面演示了我想要做的事情的意图,但我意识到这段代码将无法编译并且 typeof(T).IsClass是运行时检查,通
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
假设我在包中编写了一个类,名为 mypackage.myclass。我已经为包和类编写了自己的 HTML 文档,并将其包含在 MATLAB 帮助浏览器中,如 the MATLAB documentat
我们有一个多平台项目,它为几个平台生成二进制文件,比如 mac、windows、linux...是否可以强制 git 将所有文件的编码更改为某个特定平台(例如:Linux)。那么如何在每次用户提交或推
我正在使用 MSBuild 自动为标签创建一个文本文件和一个 ZIP 文件。我的 MSBuild 项目由 CruiseControl.NET 调用. 文本文件总是latest.txt,ZIP 文件是(
根据我的一些 API 规范: Force to place an Auth transaction into the current batch (PostAuth) or to place a tr
我正在使用超集 0.20.4 如果我想在我的 URL 中添加一个 token 以自动登录到特定用户超集/仪表板/3?standalone=true&token=123456789 我应该在代码的哪个位
我有一个大问题:我有一个 listview,每个项目都链接到页面 #concorsi。当我单击链接时,URL 会变为 #concorsi?numero=1,因为我正在从 JSON 中获取表单编号 1。
我是一名优秀的程序员,十分优秀!