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

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