Published
Edited
Sep 8, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
graph={
const tags = new Map()
let links = new Map()
allEvents
.filter( event => event.tstamp >= dateRange[0] && event.tstamp <= dateRange[1] )
.forEach( event => {
event.tags.forEach( tag => {
if( ! tags.has(tag.id) ) tags.set( tag.id, {id:tag.id,count:0,name:tag.name} )
tags.get(tag.id).count += 1
} )
event.tags.forEach( tag => { event.tags.forEach( otherTag => {
if( tag.id == otherTag.id ) return;
let lid = [tag.id,otherTag.id].sort((a,b)=>a-b).join('-')
if( ! links.has(lid) ) links.set(lid,{id:lid,count:0,source:tag.id,target:otherTag.id})
links.get(lid).count += 0.5 // because this happens twice for each actual pair
} )
} )
} )
links = [...links.values()].filter( l => l.count >= 2 )
let linkedNodes = new Set(links.map(l=>[l.source,l.target]).flat())
var nodes =([...tags.values()].filter( t => linkedNodes.has(t.id) ))
var nodeData = new Set(nodes.map(l=> l.id))
var community= louvain().nodes(nodeData).edges(links)
// getting count of nodes that belongs to a community. Expected result //(community number : count)
let communityNumbers= Object.values(community())
const communityCount = communityNumbers => {
const counts = {};
for (var i = 0; i < communityNumbers.length; i++) {
counts[communityNumbers[i]] = 1 + (counts[communityNumbers[i]] || 0);
};
return counts;
};
// sorting the object with count of nodes in a community to get the top 10 community
let obj =communityCount(communityNumbers)
let comm_sorted_by_val = Object.entries(obj).sort(([,a],[,b]) => b-a)
let top_comm= (comm_sorted_by_val .map((d)=> +d[0])).slice(0,7)

//adding community tag to nodes
nodes.forEach(function (node) {
node.community = community()[node.id]
})
nodes.forEach(function (node) {
if ( top_comm.includes(node.community) == true){
node.top = 1
} else {
node.top =0
}
})


chart.update({links, nodes: nodes,
})
return {
links, nodes: nodes,
}
}
Insert cell
Insert cell
graph
Insert cell
Insert cell
Insert cell
louvain= import('https://cdn.skypack.dev/louvain@1.2.0?min').then( mod => mod.jLouvain )
Insert cell
color = d3.scaleOrdinal(["#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6","#6a3d9a","#ffff99","#b15928"])
Insert cell
requestOptions = ({
method:'POST',
headers: {'content-type': 'application/json','Authorization': `Bearer ${token}`},
body:JSON.stringify(query)
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more