'use strict' | |
var topojson = require('topojson'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var pollster = require('pollster'); | |
var topojsonFilename = path.join(__dirname, 'us_states.topojson'); | |
var outputFile = path.join(__dirname, 'us_states-enriched-2016-president.topojson'); | |
function loadPollData () { | |
return new Promise((resolve, reject) => { | |
pollster.charts({topic: '2016-president'}, data => { | |
var dataMap = {}; | |
data.forEach(object => { | |
// console.log(object); | |
dataMap[object.state] = {}; | |
object.estimates.forEach(estimate => { | |
dataMap[object.state][estimate.choice] = estimate.value; | |
}); | |
}); | |
console.log(dataMap); | |
resolve(dataMap); | |
}); | |
}); | |
} | |
function loadTopojsonFile (topojsonFilename) { | |
return new Promise((resolve, reject) => { | |
fs.readFile(topojsonFilename, 'utf8', function (err, data) { | |
if (err) { | |
return reject(err); | |
} | |
try { | |
var norge = JSON.parse(data); | |
} catch (error) { | |
return reject(error); | |
} | |
resolve(norge); | |
}); | |
}); | |
} | |
function enrichTopojson(dataSets) { | |
return new Promise((resolve, reject) => { | |
var electionData = dataSets[0]; | |
var topojsonData = dataSets[1]; | |
console.log(topojsonData); | |
var states = topojson.feature(topojsonData, topojsonData.objects.state); | |
// console.log(states); | |
states.features.forEach((stateObject, i) => { | |
var state = stateObject.properties.STUSPS10; | |
console.log(JSON.stringify(stateObject)); | |
// console.log(JSON.stringify(state)); | |
// Remove unwanted chars. | |
if (typeof electionData[state] === 'object') { | |
stateObject.properties.clinton = electionData[state].Clinton; | |
stateObject.properties.trump = electionData[state].Trump; | |
// Format the maps | |
var fillColor = '#c0c0c0'; | |
if (stateObject.properties.clinton > stateObject.properties.trump) { | |
fillColor = '#0000ff'; | |
} else if (stateObject.properties.trump > 0) { | |
fillColor = '#ff0000'; | |
} | |
stateObject.properties.fill = fillColor; | |
stateObject.properties.stroke = '#000000'; | |
var diff = Math.abs(stateObject.properties.clinton - stateObject.properties.trump) / 40; | |
stateObject.properties['fill-opacity'] = diff; | |
// console.log(municipality, 'opacity:', stateObject.properties['fill-opacity']); | |
stateObject.properties['stroke-opacity'] = .2; | |
stateObject.properties['stroke-width'] = 1; | |
} else { | |
console.log('NOT FOUND!', state); | |
} | |
}); | |
resolve(topojsonData); | |
}); | |
} | |
// { id: 624, | |
// title: '2016 General Election: Trump vs. Clinton', | |
// slug: '2016-general-election-trump-vs-clinton', | |
// topic: '2016-president', | |
// state: 'US', | |
// short_title: '2016 President: Trump vs. Clinton', | |
// election_date: '2016-11-08', | |
// poll_count: 352, | |
// last_updated: '2016-10-28T07:01:21.000Z', | |
// url: 'http://elections.huffingtonpost.com/pollster/2016-general-election-trump-vs-clinton', | |
// estimates: [ [Object], [Object], [Object] ] } | |
// [ { choice: 'Trump', | |
// value: '54.7', | |
// lead_confidence: 100, | |
// first_name: 'Donald', | |
// last_name: 'Trump', | |
// party: 'Rep', | |
// incumbent: false }, | |
// { choice: 'Clinton', | |
// value: '30.9', | |
// lead_confidence: 0, | |
// first_name: 'Hillary', | |
// last_name: 'Clinton', | |
// party: 'Dem', | |
// incumbent: false }, | |
// { choice: 'Other', | |
// value: '8.9', | |
// lead_confidence: null, | |
// first_name: '', | |
// last_name: 'Other', | |
// party: null, | |
// incumbent: false } ] | |
function writeTopojson(topojsonData, topojsonFilename) { | |
return new Promise((resolve, reject) => { | |
var wstream = fs.createWriteStream(topojsonFilename); | |
wstream.write(JSON.stringify(topojsonData)); | |
wstream.end(); | |
resolve('File saved as: ' + topojsonFilename); | |
}); | |
} | |
function calcOpacity (value) { | |
if (value > 1) { | |
value = 0; | |
} | |
value = 1 - value; | |
if (value > .9) { | |
value = .9; | |
} | |
return value; | |
} | |
Promise.all([loadPollData(), loadTopojsonFile(topojsonFilename)]) | |
.then(enrichTopojson) | |
.then(topojsonData => { | |
return writeTopojson(topojsonData, outputFile); | |
}) | |
.then(console.log) | |
.catch(console.error); | |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment