javascript-D3中具有HTML输入的动态过滤
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-D3中具有HTML输入的动态过滤,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7410字,纯文字阅读大概需要11分钟。
内容图文
![javascript-D3中具有HTML输入的动态过滤](/upload/InfoBanner/zyjiaocheng/662/1b7a117a938e4a1cae23afdc911dcdf0.jpg)
我远非D3极客,只是从制表符分隔的文件中读取了我的第一个“散点图”.每个点(或圆圈)都有一个日期(x轴)和一个计数(y轴)以及一个句子,该句子在鼠标悬停时显示.包含数据的代码和选项卡文件在这里:
https://gist.github.com/sytpp/3a070a6ed6834169a85b
我添加了一个文本框,我可以在其中输入搜索词(第39-42行),当我点击“告诉我”时,我希望它在第2列(谁)和/或3列中突出显示与搜索词匹配的所有点(什么).我设法在搜索(第193-201行)时更改了所有点(包括图例)的不透明度,但没有弄清楚如何根据文本匹配来动态选择子区域.
function handleClick(event){
console.log(document.getElementById("myVal").value)
draw(document.getElementById("myVal").value)
return false;
}
function draw(val){
d3.select("body").selectAll("circle").style("opacity", .3);
}
我在flowingdata上找到了类似的示例,但无法重现.非常感谢你的帮助!
解决方法:
您的可视化效果真的很棒!我查看了您的代码,您只需要进行一些小的更改:
首先,您当前的函数draw()包含错误的选择.
d3.select("body").selectAll("circle").style("opacity", .3);
它不仅包含图形中的圆圈,还包含来自图例的圆圈!.因此,当您在图形中绘制圆时,请为其指定适当的类名称:
svg.selectAll("circle.dot")
.data(data)
.enter()...
因此,在函数draw中,您可以通过以下方式获得正确的选择:
d3.select("body").selectAll("circle.dot")
要为匹配搜索的圆设置不透明度,您只需在回调函数中编写逻辑.我分离了回调(并将搜索词存储在全局变量中)…
var valOpacity = function(d) {
if ((d.what.search(currentSearchTerm) != -1) || (d.who.search(currentSearchTerm) != -1)) {
return 1;
}
else {
return 0.1;
}
};
因为我们可以首先将其重新用于选择…
function draw(){
d3.select("body").selectAll("circle.dot").style("opacity", valOpacity);
}
…,然后调整您的mouseout事件以正确设置不透明度:
.on("mouseout", function(){
d3.select(this).transition().duration(50).attr("r", 5).style("opacity", valOpacity);
});
这是完整的工作代码:
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
body { font: 16px Arial;}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
div.tooltip {
position: absolute;
text-align: left;
width: 300px;
height: 150px;
padding: 2px;
font: 16px Arial;
background: none;
border: 0px;
border-radius: 2px;
}
</style>
<body>
<form name="myform" onSubmit="return handleClick()">
<input type="text" id="myVal" placeholder="Search WHO should do WHAT">
<input name="Submit" type="submit" value="Tell me!" >
</form>
<!-- load the d3.js library -->
<script type=
"text/javascript" src="http://d3js.org/d3.v3.min.js">
</script>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 60},
width = 800 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
// Parse the date / time
var parseDate = d3.time.format("%Y-%m-%d").parse;
var formatTime = d3.time.format("%d %B %Y");// Format tooltip date / time
var currentSearchTerm = "";
// Set the rangestrann
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var flags = ["LEADERS","LETTERS,BRIEFINGS,ETC...","INTERNATIONAL","ASIA (incl. CHINA)","AMERICAS", "EUROPE (incl. BRITAIN)","BUSINESS"]
var cols = ["#b2182b","#969696","#252525","#08519c","#fec503","#5aae61","#de77ae" ]
var color = d3.scale.ordinal().domain(flags).range(cols);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define 'div' for tooltips
var div = d3.select("body")
.append("div") // declare the tooltip div
.attr("class", "tooltip")
.style("opacity", 0);
// Adds the svg canvas
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Legend
var legend = d3.select("body").append("svg")
.attr("width",200)
.attr("height",200)
.append("g")
.attr("transform","translate(10,14)");
legend.append("circle").attr("cx", 10).attr("cy", 10).style("fill", cols[0]).attr("r", 20).style("opacity", .7);
legend.append("text").attr("x", 40).attr("y",16).style("font-size","15px").text("Click to read article.");
legend.append("circle").attr("cx", 10).attr("cy", 55).style("fill", cols[6]).attr("r", 5);
legend.append("text").attr("x", 25).attr("y",60).style("font-size","12px").text(flags[6]);
legend.append("circle").attr("cx", 10).attr("cy", 70).style("fill", cols[5]).attr("r", 5);
legend.append("text").attr("x", 25).attr("y",75).style("font-size","12px").text(flags[5]);
legend.append("circle").attr("cx", 10).attr("cy", 85).style("fill", cols[4]).attr("r", 5);
legend.append("text").attr("x", 25).attr("y",90).style("font-size","12px").text(flags[4]);
legend.append("circle").attr("cx", 10).attr("cy", 100).style("fill", cols[3]).attr("r", 5);
legend.append("text").attr("x", 25).attr("y",105).style("font-size","12px").text(flags[3]);
legend.append("circle").attr("cx", 10).attr("cy", 115).style("fill", cols[2]).attr("r", 5);
legend.append("text").attr("x", 25).attr("y",120).style("font-size","12px").text(flags[2]);
legend.append("circle").attr("cx", 10).attr("cy", 130).style("fill", cols[1]).attr("r", 5);
legend.append("text").attr("x", 25).attr("y",135).style("font-size","12px").text(flags[1]);
legend.append("circle").attr("cx", 10).attr("cy", 145).style("fill", cols[0]).attr("r", 5);
legend.append("text").attr("x", 25).attr("y",150).style("font-size","12px").text(flags[0]);
// Get the data
d3.tsv("EcoSHOULDS.tab", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0,50]);
// draw the scatterplot
svg.selectAll("circle.dot")
.data(data)
.enter()
.append("a")
.attr("xlink:href", function(d) { return d.link; })
.append("circle")
.attr("class", "dot")
.attr("r", 5)
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.lala); })
.attr("fill", function(d){ return color(d.flag); })
// MOUSEOVER EVENTS - Tooltip stuff after this etc
.on("mouseover", function(d) {
d3.select(this).transition().duration(50).attr("r", 20).style("opacity", .7);
div.transition()
.duration(500)
.style("fill", 0);
div.transition()
.duration(200)
.style("opacity", 1);
div .html(
d.who + "<br/><b><big>SHOULD</big></b><br/>" +
d.what + "..." + "<br/><br/><small>" +
formatTime(d.date) + " [ Section: " +
d.flag + " ]</small><br/>")
.style("left", 810 + "px")
.style("top", 100 + "px");
})
.on("mouseout", function(){
d3.select(this).transition().duration(50).attr("r", 5).style("opacity", valOpacity);
});
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(-30," + 0 + ")")
.call(yAxis);
});
function handleClick(event){
currentSearchTerm = document.getElementById("myVal").value;
console.log(currentSearchTerm);
draw(currentSearchTerm);
return false;
}
var valOpacity = function(d) {
if ((d.what.search(currentSearchTerm) != -1) || (d.who.search(currentSearchTerm) != -1)) {
return 1;
}
else {
return 0.1;
}
};
function draw(){
d3.select("body").selectAll("circle.dot").style("opacity", valOpacity);
}
</script>
</body>
内容总结
以上是互联网集市为您收集整理的javascript-D3中具有HTML输入的动态过滤全部内容,希望文章能够帮你解决javascript-D3中具有HTML输入的动态过滤所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。