Created
September 18, 2018 18:42
-
-
Save michaeljblum/dd304bdc13bc7593ae289f45bcabf516 to your computer and use it in GitHub Desktop.
Excerpt from Three Visualizations Assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
<link rel="stylesheet" href="style.css"> | |
</head> | |
<body> | |
<div id="vizOne"> | |
</div> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script> | |
<script src="vizOne.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.hovering { | |
stroke: white; | |
} | |
#vizOne { | |
background-color: #fecfd9; | |
margin-top: 20px; | |
} | |
.axis path, | |
.axis line { | |
stroke: blue; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const h = 500, | |
w = 700; | |
const svg = d3.select("#vizOne") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h) | |
d3.json("data.json", function(error, data) { | |
if (error) throw error | |
// rScale = d3.scaleLinear() | |
// .domain([1, d3.max(data, function(d){ return +d.estimate})]) | |
// .range([2.5, 65]); | |
console.log(d3.extent(data, function(d) { return +d.estimate})) | |
var aScale = d3.scaleSqrt() | |
// .domain([0, d3.max(data, function(d){ return +d.estimate })]) | |
.domain(d3.extent(data, function(d){ return +d.estimate})) | |
.range([2,25]) | |
var yScale = d3.scaleLog() | |
.domain(d3.extent(data, function(d){ return +d.marginOfError})) | |
.range([h-10,35]) | |
var xScale = d3.scaleLog() | |
.domain(d3.extent(data, function(d){ return +d.estimate})) | |
.range([10,w-35]) | |
var xAxis = d3.axisTop() | |
.scale(xScale) | |
.ticks(10, ",.1s") | |
// .tickValues([1625,11643298]) | |
var circles = svg.selectAll("circle") | |
.data(data) | |
.enter() | |
.append("circle") | |
// var nestedData = d3.nest().key(d=>d.estimate).key(d=>d.country).entries(data) | |
// console.log(nestedData) | |
var colors = ["green", "orange", "red", "blue"] | |
function randomColor() { | |
return colors[Math.floor(Math.random() * colors.length)] | |
} | |
circles | |
.attr("cx", function (d) { return xScale(+d.estimate) }) | |
// .attr("cx", function (d,i) { return i * 5}) | |
.attr("cy", function (d) { return yScale(+d.marginOfError) }) | |
// .attr("cy", function (d,i) { return (i * 4) - 25}) | |
.attr("r", function (d) { return aScale(+d.estimate) }) | |
.style("fill", randomColor) | |
.on("mouseenter", function () { | |
d3.select(this).classed("hovering", true); | |
}) | |
.on("mouseleave", function () { | |
d3.select(this).classed("hovering", false); | |
}) | |
.append("title") | |
.text(function (d) { | |
return d.country}); | |
svg.append("g") | |
.attr("class", "axis") | |
.attr("transform", "translate(0, " + (h-1) + ")") | |
.call(xAxis) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment