Skip to content

Instantly share code, notes, and snippets.

@KristinHenry
Last active August 29, 2015 13:56
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 KristinHenry/9334581 to your computer and use it in GitHub Desktop.
Save KristinHenry/9334581 to your computer and use it in GitHub Desktop.
Example of using a csv file generated by responses to a Google Form.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
div.tooltip {
position: absolute;
text-align: center;
width: 140px;
height: 70px;
padding: 4px;
font: 14px sans-serif;
background: #CCCCCC;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// modified from scatterplot example at http://bl.ocks.org/mbostock/3887118
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 860 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
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 + ")");
// declare div for tooltips
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var txt = function(d){
var str = ("Height: " + d.Height + " inches<br>");
str += "Pets: " + d.Pets + "<br>";
str += "Languages: " + d.Coding + "<br>";
str += "Adobe: " + d.Fireworks;
return str;
}
d3.csv("https://docs.google.com/spreadsheet/pub?key=0AiUD8NlSkrhTdDVxVWxwUGItZko5ZThYY1BmWkFPalE&single=true&gid=0&output=csv", function (error, data){
data.forEach(function(d) {
console.log(d);
d.Height = +d.Height;
d.Pets = +d.Pets;
d.Fireworks = +d.Fireworks;
});
x.domain(d3.extent(data, function(d) { return d.Height; })).nice();
y.domain(d3.extent(data, function(d) { return d.Pets; })).nice();
//y.domain(d3.extent(data, function(d) { return d.Fireworks; })).nice();
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("Height in Inches");
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("Number of pets")
//.text("Years using Adobe software");
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
//.attr("r", 3.5)
.attr("r", function(d) {return d.Coding + 6;})
.attr("opacity", .30)
.attr("cx", function(d) { return x(d.Height); })
.attr("cy", function(d) { return y(d.Pets); })
//.attr("cy", function(d) { return y(d.Fireworks); })
.style("fill", function(d) { return color(d.Drink); })
// for user interaction, and showing tooltip
.on("mouseover", function(d){
d3.select(this)
.transition()
.duration(10)
.style("stroke", "white")
.style("stroke-width", 2);
div.transition()
.duration(200)
.style("opacity", .9);
div.html(txt(d))
.style("left", (d3.event.pageX + 30) + "px")
.style("top", (d3.event.pageY - 28) + "px");
console.log("testing");
})
.on("mouseout", function(d){
d3.select(this)
.style("stroke-width", "0.5px")
div.transition()
.duration(500)
.style("opacity", 0);
});
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment