我正在尝试制作一个d3散点图,以在网页上提供一个搜索框;当用户在搜索框中输入单词时,d3会根据该单词过滤数据集,然后通过散点图上经过过滤的点创建路径。
我已经设法使代码达到可以将搜索词放入代码中并达到预期效果(jsfiddle)的地步,但是,当然,我不希望我的用户拥有打开文本编辑器以搜索数据集。
我喜欢Gerardo Furtado的method of changing the attribute of the node
d3.select("button").on("click", function() {
var txtName = d3.select("#txtName").node().value;
circles.style("fill", function(d) {
return d.doc === txtName ? "red" : "black";
根据他的回答,我已经尝试过将这段代码附加到图形上的代码放置在代码中。我没有错误,但也没有任何一行:
d3.select("button").on("click", function() {
var txtName = d3.select("#txtName")
chartGroup.selectAll(".line")
.data(nest)
.enter()
.filter(function(d){return d.key == txtName;})
.append("path")
.attr("class","line")
.attr("d",function(d) {return line(d.values)})
.attr("stroke", function(d) {return colors(d.key)})
.attr("stroke-width","2px")
.style("stroke-dasharray", ("3, 3"))
我也看过Amber Thomas's multi-line graph,但是我无法理解过滤函数后面的代码块。
任何帮助表示赞赏
答案 0 :(得分:2)
这里有几种不同的解决方案。您可以绘制所有路径,并且不透明度为零:
var cssLink = document.getElementByTagName('link')[2];
var prtContent = document.getElementById('print_body');
var WinPrint = window.open('','','left=0,top=0,width=1000,height=auto,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.getElementsByTagName('head')[0].appendChild(link);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
然后在单击后更改不透明度:
var lines = chartGroup.selectAll(".line")
.data(nest)
.enter()
.append("path")
.style("opacity", 0)
.attr("class", "line")
.attr("d", function(d) {
return line(d.values)
})
.attr("stroke", function(d) {
return colors(d.key)
})
.attr("stroke-width", "2px")
.style("stroke-dasharray", ("3, 3"));
这是产生的演示:
d3.select("button").on("click", function() {
var txtName = d3.select("#txtName").node().value;
circles.style("fill", function(d) {
return d.doc === txtName ? "red" : "black";
})
lines.style("opacity", function(d) {
return d.key === txtName ? 1 : 0;
})
})
var parseDate = d3.timeParse("%m/%d/%Y");
mycolour = d3.rgb("#f7f7f7");
var doc = `date number creator doc
6/16/2000 3 molly 3 rat
2/25/2002 4 may 2 cat
12/05/2004 3 molly 4 fish
07/06/2006 1 milly 1 dog
09/07/2003 4 may 4 fish
12/10/2001 4 may 3 rat
6/15/2005 2 maggie 3 rat
06/09/2004 1 milly 4 fish
10/05/2005 1 milly 3 rat
10/07/2003 4 may 1 dog
1/19/2009 4 may 2 cat
10/30/2007 1 milly 4 fish
8/13/2009 4 may 2 cat
9/30/2004 3 molly 1 dog
1/17/2006 4 may 3 rat
12/18/2009 3 molly 1 dog
11/02/2007 2 maggie 3 rat
4/17/2007 1 milly 4 fish`;
var data = d3.tsvParse(doc, function(d) {
return {
creator: d.creator,
date: parseDate(d.date),
number: Number(d.number),
doc: d.doc
};
});
var height = 300;
var width = 500;
function sortByDateAscending(a, b) {
return a.date - b.date;
}
data = data.sort(sortByDateAscending);
margin = {
top: 40,
right: 50,
bottom: 0,
left: 50
};
var minDate = new Date(2000, 1, 1);
var maxDate = new Date(2011, 1, 1);
var y = d3.scalePoint()
.domain(['may', 'milly', 'maggie', 'molly'])
.range([height, 0])
.padding(0.2);
var x = d3.scaleTime()
.domain([minDate, maxDate])
.range([0, width]);
var yAxis = d3.axisLeft(y);
var xAxis = d3.axisBottom(x);
var svg = d3.select("body").append("svg").attr("height", height + 100).attr("width", width + 100);
var chartGroup = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")")
var line = d3.line()
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.creator);
});
var redBox = chartGroup.append("rect")
.attr("y", 0)
.attr("width", width)
.attr("height", height)
.attr("fill", mycolour)
.append("g");
var nest = d3.nest()
.key(function(d) {
return d.doc;
})
.entries(data);
var colors = d3.scaleOrdinal()
.domain(function(d) {
return colors(d.key)
})
.range(["#e66101", "#fdb863", "#b2abd2", "#5e3c99"]);
var line = d3.line()
.x(function(d, i) {
return x(d.date);
})
.y(function(d, i) {
return y(d.creator);
});
var lines = chartGroup.selectAll(".line")
.data(nest)
.enter()
.append("path")
.style("opacity", 0)
.attr("class", "line")
.attr("d", function(d) {
return line(d.values)
})
.attr("stroke", function(d) {
return colors(d.key)
})
.attr("stroke-width", "2px")
.style("stroke-dasharray", ("3, 3"));
var circles = chartGroup.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", function(d) {
return x(d.date);
})
.attr("cy", function(d) {
return y(d.creator);
})
.attr("r", 4)
.style("fill", "black");
d3.select("button").on("click", function() {
var txtName = d3.select("#txtName").node().value;
circles.style("fill", function(d) {
return d.doc === txtName ? "red" : "black";
})
lines.style("opacity", function(d) {
return d.key === txtName ? 1 : 0;
})
})
chartGroup.append("g").attr("class", "x axis").attr("transform", "translate(0," + height + ")").call(d3.axisBottom(x).ticks(14));
chartGroup.append("g").attr("class", "y axis").call(d3.axisLeft(y).ticks(5));
path {
fill: none;
}
/* circle {
fill: #FF00FF;
stroke:navy;
stroke-width:2px; */
}
g.tick text y {
font-size: 30px;
font: Garamond;
}
g.tick text x {
font-size: 10px;
font: Garamond;
}
g.tick line {
display: none;
}
另一方面,如果您真的想将该代码块放在侦听器中,那就是问题:
当你做...
<script src="https://d3js.org/d3.v4.min.js"></script>
<textarea id="txtName" name="txt-Name" placeholder="Search for something.."></textarea>
<button>Try it</button>
...您正在选择一个现有元素,因此您的输入选择为空。取而代之的是,选择一些新类,例如:
chartGroup.selectAll(".line")
这是更新的JSFiddle:here
但是,这种方法的问题是,除非您创建正确的输入/更新/退出选择,否则行会堆积。