Skip to content

Instantly share code, notes, and snippets.

@SeabassWells
Last active February 6, 2019 06:19
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 SeabassWells/b6e2989c56b41f111db164b4574c3d36 to your computer and use it in GitHub Desktop.
Save SeabassWells/b6e2989c56b41f111db164b4574c3d36 to your computer and use it in GitHub Desktop.
con-map
license: mit
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Scatterplot</title>
<style>
#legend-svg {
vertical-align: top;
}
</style>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<svg id="main-svg"></svg>
<svg id="legend-svg"></svg>
<div id="tooltip"></div>
<script type = "text/javascript">
//width and height
var w = 600;
var h = 400;
var padding = 40;
console.log("d3 executed!")
//create svg element
var svg = d3.select("#main-svg")
.attr("width",w)
.attr("height",h);
// add the legend
var legendHeight = h;
var legendWidth = 50;
// interpolate between blue and red
var color = d3.interpolateRgb("blue", "red");
// load data and draw chart
d3.csv("senateChart.csv")
.then(function(dataset) {
//scale function
var xScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) { return parseFloat(d["wordCount"]); })])
.range([padding, w - padding * 2]);
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) { return parseFloat(d["activityTotal"]); })])
.range([h - padding, padding]);
// sets the number of tick marks
var xAxis = d3.axisBottom().scale(xScale).ticks(10);
var yAxis = d3.axisLeft().scale(yScale).ticks(5);
// intialize tooltip
var tip = d3.select('body')
.append('div')
.attr('class', 'tip')
// .text('I am a tooltip...')
.style('border', '1px solid steelblue')
.style('padding', '5px')
.style('position', 'absolute')
.style('display', 'none')
.on('mouseover', function(d, i) {
tip.transition().duration(0);
})
.on('mouseout', function(d, i) {
tip.style('display', 'none');
});
// add the points
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
// point x and y coordinates
.attr("cx", function(d) {
return xScale(d["wordCount"]);
})
.attr("cy", function(d) {
return h - yScale(d["activityTotal"]);
})
.attr("r", 3)
// tooltip trigger
.on('mouseover', function(d, i) {
tip.transition().duration(0)
console.log(d)
tip.attr("x", this.cx.animVal.value)
tip.attr("y", this.cy.animVal.value)
tip.style('display', 'block')
tip.text(d["nameKey"]);
})
.on('mouseout', function(d, i) {
tip.transition()
.delay(500)
.style('display', 'none');
});
//x axis
svg.append("g")
.attr("class", "xaxis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis);
//y axis
svg.append("g")
.attr("class", "yaxis")
.attr("transform", "translate(" + padding + ", 0)")
.call(yAxis);
// style points
d3.selectAll("circle")
.attr("fill", function(d) {
return color(d["ideology"]);
});
// add legend
var key = d3.select("#legend-svg")
.attr("width", legendWidth)
.attr("height", legendHeight);
var legend = key.append("defs")
.append("svg:linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "100%")
.attr("x2", "100%")
.attr("y2", "100%")
.attr("spreadMethod", "pad");
legend.append("stop")
.attr("offset", "0%")
.attr("stop-color", color(0))
.attr("stop-opacity", 1);
legend.append("stop")
.attr("offset", "33%")
.attr("stop-color", color(.33))
.attr("stop-opacity", 1);
legend.append("stop")
.attr("offset", "66%")
.attr("stop-color", color(.66))
.attr("stop-opacity", 1);
legend.append("stop")
.attr("offset", "100%")
.attr("stop-color", color(1))
.attr("stop-opacity", 1);
key.append("rect")
.attr("width", legendWidth)
.attr("height", legendHeight)
.style("fill", "url(#gradient)")
.attr("transform", "translate(0,10)");
var y = d3.scaleLinear()
.range([300, 0])
.domain([1, 0]);
var yAxis = d3.axisBottom()
.scale(y)
.ticks(5);
key.append("g")
.attr("class", "y axis")
.attr("transform", "translate(0,30)")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("axis title");
// tooltip
var div = d3.select("#tooltip")
.style('opacity', 0);
})
.catch(function(error){
console.log(error);
})
</script>
</body>
</html>
nameKey wordCount activityTotal ideology
Mr. LEE 46 383 0.884275827
Mr. PAUL 16 283 0.763332699
Mr. KING 4274 530 0.508044528
Mr. BURR 1724 190 0.733182254
Mr. REED 1185 464 0.109932678
Mr. CRUZ 123 379 0.958110808
Mr. ENZI 3962 230 0.928238348
Mr. FLAKE 2997 258 0.862824775
Mr. COONS 2426 719 0.38904102
Mr. RUBIO 1189 702 0.880858169
Mr. RISCH 0 287 0.911920808
Mr. CRAPO 3407 272 0.868060061
Mr. YOUNG 10 326 0.793668631
Mr. MORAN 5185 336 0.827819836
Mr. BLUNT 3667 362 0.948639628
Mr. SASSE 2246 163 0.744310257
Mr. UDALL 1099 533 0.329247934
Mr. BROWN 1647 772 0.172670071
Mr. WYDEN 5420 686 0.225523147
Mr. CASEY 6070 569 0.331921455
Mr. SCOTT 194 247 0.896612245
Mr. THUNE 4462 258 0.854590264
Mr. HATCH 775 459 0.884773268
Mr. LEAHY 10617 416 0.232496209
Mr. KAINE 2619 450 0.291971229
Mr. SHELBY 4 79 0.688417605
Mr. MCCAIN 2512 241 0.7406107
Mr. COTTON 1361 320 0.977553067
Ms. HARRIS 41 503 0.142618422
Mr. BENNET 3508 502 0.419948251
Mr. MURPHY 6921 476 0.173129102
Mr. CARPER 19 420 0.293620077
Mr. NELSON 2459 582 0.585852924
Mr. PERDUE 397 328 0.961817068
Ms. HIRONO 151 591 0.178124645
Mr. SCHATZ 5178 371 0.230806405
Mr. DURBIN 18174 675 0.174746097
Mrs. ERNST 60 323 0.867112492
Mr. CARDIN 2394 629 0.282637287
Ms. WARREN 8205 1023 0.225134649
Mr. MARKEY 2239 756 0.082090237
Mr. PETERS 3973 495 0.43544556
Mr. WICKER 0 380 0.905146421
Mr. TESTER 6564 487 0.565438178
Mr. DAINES 5132 406 0.890550365
Mr. HELLER 0 440 0.78947638
Ms. HASSAN 22 554 0.198983434
Mr. BOOKER 7255 748 0.206383987
Mr. TILLIS 63 332 0.896315262
Mr. HOEVEN 756 286 0.791116632
Mr. INHOFE 1822 383 1
Mr. TOOMEY 945 206 0.756144109
Mr. GRAHAM 384 247 0.759514873
Mr. ROUNDS 42 361 0.973247729
Mr. CORKER 7509 121 0.679938549
Mr. CORNYN 6510 448 0.949945839
Mr. WARNER 824 375 0.474427927
Mr. STRANGE 0 113 0.910379248
Mr. BOOZMAN 382 358 0.962462909
Mr. GARDNER 3512 438 0.757153252
Mr. ISAKSON 2293 303 0.812955059
Mr. ROBERTS 405 272 0.987049554
Mr. CASSIDY 5399 358 0.817181191
Mr. KENNEDY 4 322 0.91731001
Ms. COLLINS 5792 505 0.507589729
Mr. FRANKEN 47 414 0
Mr. COCHRAN 168 125 0.759592097
Mr. SCHUMER 19668 299 0.308087673
Mr. PORTMAN 2312 360 0.613019582
Mr. MERKLEY 3317 709 0.065856595
Mr. SANDERS 14182 457 0.06337748
Mrs. MURRAY 551 532 0.143810992
Mr. MANCHIN 1151 398 0.718582524
Mrs. CAPITO 2155 383 0.781830563
Ms. BALDWIN 1872 786 0.281910961
Mr. JOHNSON 13 250 0.859047119
Mr. SULLIVAN 855 315 0.799765416
Mr. DONNELLY 16 345 0.707910412
Mr. GRASSLEY 6929 384 0.812566611
Ms. STABENOW 6201 542 0.314696826
Mrs. FISCHER 436 237 0.88584793
Mrs. SHAHEEN 1344 694 0.336368293
Mr. MENENDEZ 1267 648 0.260774576
Mr. HEINRICH 1811 439 0.338176202
Ms. HEITKAMP 1555 411 0.582656163
Mr. LANKFORD 3761 269 0.92576685
Ms. CANTWELL 3143 386 0.344623571
Mr. BARRASSO 1541 225 0.921333295
Ms. MURKOWSKI 886 407 0.671304324
Ms. DUCKWORTH 39 603 0.212382215
Mr. MCCONNELL 11234 209 0.759455453
Ms. KLOBUCHAR 571 828 0.378270303
Mr. ALEXANDER 3100 182 0.672270703
Mrs. FEINSTEIN 1727 650 0.181127987
Mr. BLUMENTHAL 1915 990 0.160477342
Mr. VAN HOLLEN 2064 652 0.100273791
Mrs. MCCASKILL 8 369 0.497054006
Mr. WHITEHOUSE 7627 622 0.188852339
Mrs. GILLIBRAND 1944 706 0.084502868
Ms. CORTEZ MASTO 21 437 0.277798876
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment