Skip to content

Instantly share code, notes, and snippets.

@nadinesk
Last active July 6, 2016 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadinesk/162b437373985175be41927959eafc0a to your computer and use it in GitHub Desktop.
Save nadinesk/162b437373985175be41927959eafc0a to your computer and use it in GitHub Desktop.
school-data-dropdown
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<!-- Example based on http://bl.ocks.org/mbostock/3887118 -->
<!-- Tooltip example from http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html -->
<style>
body {
font: 11px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
}
</style>
<body>
<form name="myform" onSubmit="return handleClickSearch()">
<input name="Submit" type="submit" size="50" style="font-size: 14px;" color="#fff" value="Search" >
<input type="text" id="myVal" size="80" style="font-size: 14px;" placeholder="School Search">
</form>
<select id="select-list">
<option value="d.percentwhites">First option, index 0</option>
<option value="d.percentblacks">Second option, index 1</option>
</select>
<div id="correl-1-title">Median Income
<div id="correl-1-chart"></div>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"></script>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
var alertChange = function() {
//get the data value and index from the event
var selectedValue = d3.event.target.value;
var selectedIndex = d3.event.target.selectedIndex;
var xValue = function(d) { return d.percentwhites;}, // data -> value
xScale = d3.scale.linear().range([0, width]), // value -> display
xMap = function(d) { return xScale(xValue(d));}, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
d3.csv("https://raw.githubusercontent.com/nadinesk/nadinesk.github.io/master/d3Data/schooldata4.csv", function(error, data) {
data.forEach(function(d) {
d.income50perc = +d.income50perc;
d["averagetest"] = +d["averagetest"];
});
xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]);
});
alert("You selected the option at index " + selectedIndex
+ ", with value attribute "
+ selectedValue);
/* //if you need to access more complicated attributes
//or data from the option element, you can use the index
//to select the individual element:
var selectedDOMElement =
d3.event.target.children[selectedIndex];
var selection = d3.select(selectedDOMElement);
alert("The text from that option was: " + selection.text()); */
}
d3.select("#select-list").on("change", alertChange);
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 1000 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var xValue = function(d) { return d.income50perc;}, // data -> value
xScale = d3.scale.linear().range([0, width]), // value -> display
xMap = function(d) { return xScale(xValue(d));}, // data -> display
xAxis = d3.svg.axis().scale(xScale).orient("bottom");
var yValue = function(d) { return d["averagetest"];}, // data -> value
yScale = d3.scale.linear().range([height, 0]), // value -> display
yMap = function(d) { return yScale(yValue(d));}, // data -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");
var svg = d3.select("#correl-1-chart").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 + ")");
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var currentSearchTerm = "";
d3.csv("https://raw.githubusercontent.com/nadinesk/nadinesk.github.io/master/d3Data/schooldata4.csv", function(error, data) {
data.forEach(function(d) {
d.income50perc = +d.income50perc;
d["averagetest"] = +d["averagetest"];
});
xScale.domain([d3.min(data, xValue)-1, d3.max(data, xValue)+1]);
yScale.domain([d3.min(data, yValue)-0.2, d3.max(data, yValue)+0.2]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("median income");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("grades ahead/below");
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", function(d) { if (d.totalenrollment >= 400000)
{
return 25
}
else if (d.totalenrollment < 400000 && d.totalenrollment >= 100000)
{
return 20
}
else if (d.totalenrollment < 100000 && d.totalenrollment >= 50000)
{
return 15
}
else if (d.totalenrollment < 50000 && d.totalenrollment >= 10000)
{
return 10
}
else if (d.totalenrollment < 10000 && d.totalenrollment >= 1000)
{
return 8
}
else
return 4
})
.attr("cx", xMap)
.attr("cy", yMap)
.style("fill", function(d) {
if (d.totalenrollment >= 400000)
{
return "rgba(255, 116, 140,1.0)"
}
else if (d.totalenrollment < 400000 && d.totalenrollment >= 100000)
{
return "rgba(255, 116, 140,0.8)"
}
else if (d.totalenrollment < 100000 && d.totalenrollment >= 50000)
{
return "rgba(255, 116, 140.0.7)"
}
else if (d.totalenrollment < 50000 && d.totalenrollment >= 10000)
{
return "rgba(255, 116, 140, 0.6)"
}
else if (d.totalenrollment < 10000 && d.totalenrollment >= 1000)
{
return "rgba(255, 116, 140, 0.4)"
}
else
return "rgba(255, 116, 140,0.9)"
})
.style("stroke", "white")
.attr("visibility", function(d) {
if (d.income50perc <= 1000)
{
return "hidden"
}
else {
return "visible"
}
})
.on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d["educationagencyname"] + "<br/> (" + xValue(d)
+ ", " + yValue(d) + ")")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});
var all_schools = [];
data.forEach(function(d) {
all_schools.push(d["educationagencyname"]);
});
$( "#myVal" ).autocomplete({
source: all_schools
});
});
function handleClickSearch(event){
currentSearchTerm = document.getElementById("myVal").value;
console.log(currentSearchTerm);
draw(currentSearchTerm);
return false;
}
function draw(){
d3.select("body").selectAll("circle.dot").attr("visibility", valOpacity);
}
var valOpacity = function(d) {
if (d.educationagencyname.search(currentSearchTerm.toUpperCase()) != -1) {
return "visible";
}
else {
return "hidden";
}
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment