Skip to content

Instantly share code, notes, and snippets.

@aaizemberg
Last active August 22, 2019 15:41
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 aaizemberg/f220472c8cadc342cc754868675ccd1c to your computer and use it in GitHub Desktop.
Save aaizemberg/f220472c8cadc342cc754868675ccd1c to your computer and use it in GitHub Desktop.
w34
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>w34</title>
</head>
<body>
<div id="q1"></div>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var url = "https://download.data.world/s/rxdnwucvhs5ugosaslbed6a6uty4wg";
d3.csv(url).then(
function(data) {
data.forEach(function(d) {
d.response = +d.response;
});
visualizar(data);
}
)
var txt2emoji = { "Teens" : "🧒", "Parents" : "👨"}
function rep(n,type) {
var text = ""
for (var i = 0; i < n; i++) {
text = text + txt2emoji[type];
}
return(text)
}
function visualizar(data) {
var q1 = "Q1: % of US teens/parents who say they spend too much time on their cellphone";
d3.select("div#q1").text(q1);
d3.select("div#q1")
.selectAll("p")
.data(data.filter(function(d){return d.frequency == "na";}))
.enter()
.append("p")
.text(function(d) {return rep(100*d.response, d.respondent) + " " + (100*d.response).toString() + "% " + d.respondent; })
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment