Published
Edited
Jun 5, 2021
Importers
Insert cell
Insert cell
Insert cell
Insert cell
import {graph as graphDraw} from '@d3/d3-hierarchy'
Insert cell
Insert cell
Insert cell
import { raw as candidates, lists } from '@taniki/regionales2021-donnees-minint'
Insert cell
graph = {
let nodes = [... new Set(candidates.map(c => c['Nuance candidat']))]

let pairs = (l) => {
let p = []
l.sort((a,b) => a.localeCompare(b))

l.forEach((vi,i)=>{
l.slice(i+1).forEach((vj,j)=>{
p.push([vi, vj])
})
})
return p
}
let links = lists
.map(l => l.candidatesNuances)
.map(pairs)
.flat()
.reduce((acc, p)=>{
let link = acc.find(l => l.source == p[0] && l.target == p[1])

if (!link) acc.push({ source: p[0], target: p[1], count:1 })
else link.count += 1

return acc
}, [])
return {nodes, links}
}
Insert cell
Insert cell
netClustering = require("netclustering")
Insert cell
communities = {
let n = graph.nodes.map(n => ({id: n}))
let l = graph.links.map(l=>({
'source': graph.nodes.indexOf(l.source),
'target': graph.nodes.indexOf(l.target),
'count': l.count,
}))

let clusters = netClustering.cluster(n, l, undefined, 'count')
return ({n, l, clusters})
}
Insert cell
Insert cell
import { candidateColors as nuanceColors } from '@taniki/regionales2021-colors'
Insert cell
color = d3.scaleOrdinal()
.domain(Object.keys(nuanceColors))
.range(Object.values(nuanceColors))
Insert cell
swatches({ color, columns: '180px' })
Insert cell
import { swatches } from '@d3/color-legend'
Insert cell
h = width
Insert cell
m = ({ t:50, b: 50, l: 50, r: 50 })
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more