Worldcup data pulled. Teams parsed.

master
Ben Burlingham 9 years ago
commit 4030198e64
  1. 13
      Datafile
  2. 4
      Gemfile
  3. 40
      teams.hs
  4. 1
      teams.json
  5. BIN
      worldcup.db
  6. 126
      worldcup.html
  7. 17
      worldcup.js

@ -0,0 +1,13 @@
####################################
# Datafile for World Cups
#
# use
# $ sportdb new worldcup
world 'world.db', setup: 'countries'
football 'national-teams'
football 'world-cup'

@ -0,0 +1,4 @@
source 'http://rubygems.org'
gem "activerecord", "= 4.2.7.1"
gem "sportdb"

@ -0,0 +1,40 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
import Data.Aeson as AE
import Data.Aeson.Types as AET
import Data.ByteString.Lazy as BL
import Data.ByteString.Lazy.Char8 as BL8
import Data.HashMap.Strict as HM
import Prelude as P
data Team = Team { _id :: Value, country :: Value } deriving (Show)
instance ToJSON Team where
toJSON Team{..} = object [ "i" .= _id, "c" .= country ]
instance FromJSON Team where
parseJSON = withObject "team" $ \o -> do
_id <- o .: "id"
country <- o .: "title"
return Team{..}
reduce :: [Team] -> Value -> [Team]
reduce acc x = case (parseEither parseJSON x :: Either String Team) of
(Left s) -> error s
(Right v) -> v : acc
parseTeams :: Either String [Value] -> [Team]
parseTeams (Left x) = error x
parseTeams (Right xs) = P.foldl reduce [] xs
parseResult :: [Team] -> [(String, Value)]
parseResult = P.foldl (\acc x -> (extractId x, country x) : acc) []
where
extractId = BL8.unpack . encode . _id
main :: IO ()
main = do
src <- BL.readFile "./data/teams.json"
let teams = parseTeams (AE.eitherDecode src :: Either String [Value])
BL.writeFile "teams.json" $ encode $ HM.fromList $ parseResult teams

@ -0,0 +1 @@
{"112":"Bonaire","149":"Hungary","211":"Brazil","163":"Liechtenstein","105":"Puerto Rico","170":"England","17":"Côte d'Ivoire","71":"Hong Kong","156":"Russia","48":"Malawi","113":"Guadeloupe","148":"Czech Republic","210":"Argentina","162":"Bosnia-Herzegovina","104":"Montserrat","171":"Scotland","16":"Burkina Faso","70":"China","157":"Turkey","49":"Mozambique","28":"Gabon","110":"Turks and Caicos Islands","213":"Paraguay","59":"Bangladesh","161":"Iceland","198":"Fiji","107":"Saint Lucia","172":"Wales","15":"Benin","73":"North Korea","154":"Croatia","29":"São Tomé and Príncipe","111":"United States Virgin Islands","212":"Chile","58":"Afghanistan","160":"Norway","199":"New Caledonia","106":"Saint Kitts and Nevis","173":"Northern Ireland","14":"Sierra Leone","72":"Japan","155":"Serbia","8":"Guinea","116":"Sint Maarten","88":"Vietnam","215":"Colombia","189":"Yemen","167":"Georgia","101":"Grenada","174":"Faroe Islands","13":"Senegal","75":"Macau","138":"Portugal","152":"Belarus","39":"Tanzania","9":"Guinea-Bissau","117":"Honduras","89":"Anguilla","214":"Uruguay","188":"United Arab Emirates","166":"San Marino","100":"Dominican Republic","175":"Gibraltar","12":"Mauritania","74":"South Korea","139":"Slovakia","153":"Switzerland","38":"Sudan","114":"Martinique","99":"Dominica","217":"Peru","165":"Moldova","103":"Jamaica","176":"Israel","208":"Niue","11":"Mali","77":"Taiwan","129":"Spain","68":"Turkmenistan","150":"Andorra","115":"Saint Martin","98":"Curaçao","216":"Ecuador","164":"Montenegro","102":"Haiti","177":"Bahrain","209":"Tuvalu","10":"Liberia","76":"Mongolia","128":"Estonia","69":"Uzbekistan","151":"Albania","22":"Cameroon","4":"Libya","97":"Cuba","84":"Philippines","141":"Bulgaria","219":"Bolivia","53":"Swaziland","185":"Qatar","192":"Canada","178":"Iran","206":"Vanuatu","220":"Guyana","79":"Cambodia","127":"Germany","134":"Italy","66":"Sri Lanka","35":"Rwanda","40":"Uganda","23":"Central African Republic","5":"Tunisia","96":"Cayman Islands","85":"Singapore","140":"Slovenia","218":"Venezuela","52":"South Africa","184":"Palestine","193":"Australia","179":"Iraq","207":"Kiribati","221":"Suriname","78":"Brunei","126":"Cyprus","135":"Luxembourg","67":"Tajikistan","34":"Kenya","41":"Zanzibar","20":"Nigeria","6":"Cape Verde","118":"Costa Rica","95":"British Virgin Islands","86":"Thailand","143":"Latvija","51":"Seychelles","187":"Syria","169":"Azerbaijan","190":"Mexico","204":"Tahiti","222":"French Guiana","125":"Belgium","136":"Malta","64":"Nepal","37":"South Sudan","42":"Angola","21":"Togo","7":"Gambia","119":"El Salvador","94":"Bermuda","87":"Timor-Leste","142":"Denmark","50":"Namibia","186":"Saudi Arabia","168":"Armenia","191":"United States","205":"Tonga","124":"Austria","137":"Netherlands","65":"Pakistan","36":"Somalia","43":"Botswana","26":"Congo DR","93":"Barbados","80":"Indonesia","145":"Poland","57":"Kazakhstan","181":"Kuwait","196":"American Samoa","109":"Trinidad and Tobago","202":"Samoa","123":"Nicaragua","130":"Finland","62":"Kyrgyzstan","31":"Djibouti","44":"Comoros","27":"Equatorial Guinea","1":"Algeria","92":"Bahamas","81":"Laos","144":"Lithuania","56":"Réunion","180":"Jordan","197":"Cook Islands","108":"Saint Vincent and the Grenadines","203":"Solomon Islands","122":"Belize","131":"France","63":"Maldives","30":"Burundi","45":"Lesotho","24":"Chad","2":"Egypt","91":"Aruba","82":"Malaysia","147":"Sweden","55":"Zimbabwe","183":"Oman","194":"Guam","200":"New Zealand","19":"Niger","121":"Guatemala","132":"Greece","60":"Bhutan","158":"Ukraine","33":"Ethiopia","46":"Mauritius","25":"Congo","3":"Morocco","90":"Antigua and Barbuda","83":"Myanmar","146":"Romania","54":"Zambia","182":"Lebanon","195":"Northern Mariana Islands","201":"Papua New Guinea","18":"Ghana","120":"Panama","133":"Ireland","61":"India","159":"Macedonia","32":"Eritrea","47":"Madagascar"}

Binary file not shown.

@ -0,0 +1,126 @@
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.notes {
font-size:14px;
}
.group-tick line {
stroke: #ddd;
}
.groups,
.ribbons {
fill-opacity: 0.8;
}
</style>
<svg width="500" height="500"></svg>
<script src="http://d3js.org/d3.v4.0.0-alpha.50.min.js"></script>
<script src="http://d3js.org/d3-chord.v0.0.min.js"></script>
<script>
// use lookup table to build this array:
// - traverse events, get team A and team B
// - put team A and team B IDs into lookup, assign them matrix array positions
// - for each event, go to lookupA and put in scoreB, then lookupB and scoreA
var matrix = [
[ null, 0, 2, 3, 0, 1],
[ 1, null, 2, null, 1, 3 ],
[ 2, 3, null, 5, 0, 2 ],
[ 0, null, 1, null, 2, 3 ],
[ 2, 4, 2, 5, null, 5 ],
[ 4, 3, 9, 3, 6, null ],
];
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
outerArcThickness = 15,
outerRadius = Math.min(width, height) * 0.5 - 40,
innerRadius = outerRadius - outerArcThickness;
var chord = d3.chord()
.padAngle(0.25)
.sortSubgroups(d3.descending);
var arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var ribbon = d3.ribbon()
.radius(innerRadius);
var color = d3.scaleOrdinal(d3.schemeCategory10);
// d3.scaleOrdinal()
// .range(["#f0f9e8", "#bae4bc", "#7bccc4", "#2b8cbe"]);
var g = svg.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.datum(chord(matrix));
var group = g.append("g")
.attr("class", "groups")
.selectAll("g")
.data(function(chords) { return chords.groups; })
.enter().append("g");
group.append("path")
.style("fill", function(d) { return color(d.index); })
// .style("stroke", function(d) { return d3.rgb(color(d.index)).darker(); })
.attr("d", arc);
var groupTick = group.selectAll(".group-tick")
.data(function(d) { return groupTicks(d, 1); })
.enter().append("g")
.attr("class", "group-tick")
.attr("transform", function(d) { return "rotate(" + (d.angle * 180 / Math.PI - 90) + ") translate(" + outerRadius + ",0)"; });
groupTick.append("line")
.attr("x2", 6);
groupTick
.filter(function(d) { return d.value % 3 === 0; })
.append("text")
.attr("x", 8)
.attr("dy", ".35em")
.attr("transform", function(d) { return d.angle > Math.PI ? "rotate(180) translate(-16)" : null; })
.style("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; })
.text(function(d) { return d.value; });
g.append("g")
.attr("class", "ribbons")
.selectAll("path")
.data(function(chords) { return chords; })
.enter().append("path")
.attr("d", ribbon)
.style("fill", function(d) { return color(d.target.index); })
// .style("stroke", function(d) { return d3.rgb(color(d.target.index)).darker(); });
// Returns an array of tick angles and values for a given group and step.
function groupTicks(d, step) {
var k = (d.endAngle - d.startAngle) / d.value;
return d3.range(0, d.value, step).map(function(value) {
return {value: value, angle: value * k + d.startAngle};
});
}
</script>
<div class="notes">
<h5>Lessons learned</h5>
scaleOrdinal vs scaleLinear (only 2 colors!) vs interpolateCool vs d3.schemeColor20c<br>
ribbon source vs index (change fill them differently)
<a href="https://github.com/d3/d3-scale-chromatic">https://github.com/d3/d3-scale-chromatic</a>
<h5>TODO</h5>
add team name to each arc
build dataset
</div>

@ -0,0 +1,17 @@
const SqliteToJson = require('sqlite-to-json');
const sqlite3 = require('sqlite3');
const exporter = new SqliteToJson({
client: new sqlite3.Database('./worldcup.db')
});
exporter.tables((err, tables) => {
errorHandler("Listing all tables.", err);
tables.forEach((table) => {
exporter.save(table, `./data/${table}.json`, errorHandler.bind(null,`Saving ${table}.`));
});
});
function errorHandler(msg, err) {
err ? console.error(`ERROR ${msg}:\n ${err}`) : console.log(msg);
}
Loading…
Cancel
Save