getNodeLinks = () => {
const loveActuallyHashMap = {
'abdul_salis' : {name: "Abdul Salis", title: "Production assistant", group : " London A", groupid: 1},
'keira_knightley' : {name: "Keira Knightley", title: "Newlywed", group: "London A", groupid: 1},
'colin_firth' : {name : "Colin Firth", title: "Newly Single Writer", group: "London A", groupid: 1},
'kris_marshall' : {name: "Kris Marshall", title: "Server", group: "London A", groupid: 1},
'laura_linney' : {name: 'Laura Linny', title: 'Office Worker', group: "Go-betweeners", groupid: 2},
'andrew_lincoln' : {name: 'Andrew Lincoln', title: 'Artist', group: "Go-betweeners", groupid: 2},
'bill_nighy' : {name: 'Bill Nighy', title: 'Aging Pop Star', group: "Go-betweeners", groupid: 2},
'martin_freeman' : {name: 'Martin Freeman/Joanna Page', title: 'Body Doubles', group: "Go-betweeners", groupid: 2},
'heike_makatsch' : {name: 'Heike Makatsch', title: 'Secretary', group: "London B", groupid: 3},
'rowan_atkinson' : {name: 'Rowan Atkinson', title: 'Salesman', group: "London B", groupid: 3},
'liam_neeson' : {name: 'Liam Neeson', title: 'Bereaved stepfather', group: "London B", groupid: 3},
'emma_thompson' : {name: 'Emma Thompson', title: "PM's sister", group: "London B", groupid: 3},
'hugh_grant' : {name: 'Hugh Grant', title: "Prime Minister", group: "London B", groupid: 3},
'alan_rickman' : {name: 'Alan Rickman', title: "Office Manager", group: "London B", groupid: 3}
}
return Promise.all([
d3.csv("https://raw.githubusercontent.com/ahoak/data-visualization-workshop/master/data/laEdges.csv"),
d3.csv("https://raw.githubusercontent.com/ahoak/data-visualization-workshop/master/data/set_appearances.csv")
]).then((result) => {
console.log('len: ', result.length)
const edges = result[0]
const places = result[1]
const multipleScenes = places.reduce((accumulator, x) => {
const keys = Object.keys(x)
let scene = ''
const list = keys.reduce((accum, name) => {
if(name !== 'scenes') {
if(x[name].length){
accum.push(name)
}
} else {
scene = x[name]
}
return accum
}, [])
if(list.length > 1){
accumulator.push({scene, names: list})
}
return accumulator
}, [])
const links = edges.reduce((accumulator, e) => {
const source = e.actors
const keys = Object.keys(e)
let links = keys.reduce((accum, key) => {
if(key !== 'actors' && parseInt(e[key], 10) > 0 && key !== source) {
const o = {}
o.source = source
o.target = key
o.weight = e[key]
accum.push(o)
}
return accum
}, [])
if(links.length) {
links = [...links].map((curr) => {
curr.scene = ''
multipleScenes.forEach((s, i) => {
const found = s.names.filter((name)=> name === curr.source || name === curr.target)
if(found.length === 2) { // both source and target found
curr.scene = s.scene
curr.sceneid = i
}
})
return curr
})
links.forEach((link) => accumulator.push(link))
}
return accumulator
}, [])
const keys = Object.keys(edges[0])
console.log('keys', keys)
const nodes = keys.reduce((accum, key) => {
if(key !== 'actors'){
const node = loveActuallyHashMap[key]
node.id = key
accum.push(node)
}
return accum
}, [])
const forceLayout = { nodes, links}
return forceLayout
})
}