I am far to be a D3 geek and just made one of my first 'scatter plots' reading from a tab delimited file. Each dot (or circle) has a date (x axis) and a count (y axis) attached to it and a sentence, which shows on mouseover. The code and tab file containing the data are here:
https://gist.github.com/sytpp/3a070a6ed6834169a85b
I added a text box where I can enter a search term (line 39-42), when I hit 'Tell me' I would like it to highlight all dots which match the search term in column 2 (who) and/or 3 (what) of the data. I managed to change the opacity of ALL dots (including legend) upon search (line 193-201) but didn't figure out how to select a subsect dynamically based on text match.
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);
}
I found similar examples on flowingdata but could not reproduce it. Thanks a lot for your help!
Your visualization is really cool! I looked at your code and you just have to make some minor changes:
First of all, your current function draw() contains the wrong selection.
d3.select("body").selectAll("circle").style("opacity", .3);
It contains not just the circles in the graph but also the circles from the legend!. So when you draw the circles in the graph, assign a proper class name to them:
svg.selectAll("circle.dot")
.data(data)
.enter()...
So in the function draw, you can get the right selection with:
d3.select("body").selectAll("circle.dot")
For setting the opacity for the circles matching the search you just write the logic in the callback function. I separated the callback (and stored the search term in a global variable) ...
var valOpacity = function(d) {
if ((d.what.search(currentSearchTerm) != -1) || (d.who.search(currentSearchTerm) != -1)) {
return 1;
}
else {
return 0.1;
}
};
because we can reuse this first for the very selection...
function draw(){
d3.select("body").selectAll("circle.dot").style("opacity", valOpacity);
}
... and second to adapt your mouseout event to set the opacity correctly:
.on("mouseout", function(){
d3.select(this).transition().duration(50).attr("r", 5).style("opacity", valOpacity);
});
Here is the full working code:
<!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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With