add d3 data formatting & update map view with two links repr
This commit is contained in:
Vendored
+75
@@ -0,0 +1,75 @@
|
||||
import { hierarchy } from 'd3-hierarchy'
|
||||
|
||||
function flatten (arr, accumulator = []) {
|
||||
return arr.reduce((acc, child) => {
|
||||
acc.push(child)
|
||||
return child.children ? flatten(child.children, acc) : acc
|
||||
}, accumulator)
|
||||
}
|
||||
|
||||
function getLinked (text) {
|
||||
const types = ['text_en_rebond', 'text_produits', 'text_de_depart']
|
||||
return types.reduce((acc, type) => {
|
||||
// Handle `null` and `undefined`
|
||||
return text[type] ? [...acc, ...text[type]] : acc
|
||||
}, [])
|
||||
}
|
||||
|
||||
export function toSingleManyData (rawData) {
|
||||
rawData.first = true
|
||||
const h = hierarchy(rawData, d => getLinked(d))
|
||||
h.each(node => {
|
||||
if (node.parent && node.children) {
|
||||
const id = node.parent.data.id
|
||||
node.children = node.children.filter(child => child.data.id !== id)
|
||||
}
|
||||
})
|
||||
|
||||
const nodes = h.descendants()
|
||||
const links = h.links()
|
||||
nodes.forEach(node => {
|
||||
Object.assign(node.data, {
|
||||
type: node.data.__typename.toLowerCase(),
|
||||
class: 'family-' + node.data.familles[0].id
|
||||
})
|
||||
})
|
||||
return { nodes, links }
|
||||
}
|
||||
|
||||
|
||||
export function toManyManyData (rawData) {
|
||||
rawData.first = true
|
||||
const h = hierarchy(rawData, d => getLinked(d))
|
||||
h.each(node => {
|
||||
if (node.parent && node.children) {
|
||||
const id = node.parent.data.id
|
||||
node.children = node.children.filter(child => child.data.id !== id)
|
||||
}
|
||||
})
|
||||
|
||||
const ids = []
|
||||
const nodes = []
|
||||
const links = []
|
||||
flatten(h.descendants()).filter(node => {
|
||||
if (!ids.includes(node.data.id)) {
|
||||
ids.push(node.data.id)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}).forEach(({ data, children, depth }) => {
|
||||
nodes.push({ id: data.id, data })
|
||||
if (children) {
|
||||
children.forEach(child => {
|
||||
links.push({ source: data.id, target: child.data.id })
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
nodes.forEach(node => {
|
||||
Object.assign(node.data, {
|
||||
type: node.data.__typename.toLowerCase(),
|
||||
class: 'family-' + node.data.familles[0].id
|
||||
})
|
||||
})
|
||||
return { nodes, links }
|
||||
}
|
||||
Reference in New Issue
Block a user