Skip to content

Instantly share code, notes, and snippets.

@arm5077
Last active December 10, 2015 21:30
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 arm5077/04e865ef1c0325c26be8 to your computer and use it in GitHub Desktop.
Save arm5077/04e865ef1c0325c26be8 to your computer and use it in GitHub Desktop.
Candidate tracker
// thenBy.js, sorting library
/*** Copyright 2013 Teun Duynstee Licensed under the Apache License, Version 2.0 ***/
var firstBy=function(){function n(n,t){if("function"!=typeof n){var r=n;n=function(n){return n[r]}}if(1===n.length){var u=n;n=function(n,t){return u(n)<u(t)?-1:u(n)>u(t)?1:0}}return-1===t?function(t,r){return-n(t,r)}:n}function t(t,u){return t=n(t,u),t.thenBy=r,t}function r(r,u){var f=this;return r=n(r,u),t(function(n,t){return f(n,t)||r(n,t)})}return t}();
var limit = 3; // Number of places to display
// Get data from endpoint
d3.json("http://traveltracker.nationaljournal.com/api/widget", function(err, data){
// Sort by date and number of trips
data.results.sort(
firstBy(function(a, b){
return b.date - a.date
})
.thenBy("stopCount", -1)
);
var places = d3.select(".places");
data.results.forEach(function(d, i){
if( i < limit){
var place = places.append("div")
.attr("class", "place");
place.append("div")
.attr("class", "state " + d.state)
place.append("div")
.attr("class", "name")
.text(d.candidate);
place.append("div")
.attr("class", "location")
.text(d.stops[0].city + ", " + d.stops[0].state);
place.append("div")
.attr("class", "date")
.text(d.formatted_date);
}
})
});
<!doctype html>
<html>
<head>
<!-- D3.js, replace with local version? -->
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<h3>Candidate Tracker</h3>
<div class="places"></div>
</body>
<!-- Site-specific scripts -->
<script src="app.js"></script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment