radialTreew = {
const svg =
d3.select(DOM.svg(width, width))
.attr("viewbox", "0 0 1200 1200")
.attr("preserveAspectRatio","xMidYMid meet")
.style("box-sizing", "border-box")
.style("cursor", "grab");
svg.append('rect')
.attr('width', width)
.attr('height', width)
.attr('x', 0)
.attr('y', 0)
.attr('fill', 'black')
const root = tree(data)
const svgZoom = svg
.append('g')
const svgViz = svgZoom
.append('g')
.attr('transform', 'translate(' + width / 2 + ',' + width / 2 + ')')
.attr('id', 'viz')
svgViz
.append('g')
.attr('id', 'links')
.selectAll('path')
.data(root.links())
.join('path')
.attr(
'd',
d3
.linkRadial()
.angle((d) => d.x)
.radius((d) => d.y)
)
.attr('fill', 'none')
.attr('stroke', '#fff')
svgViz
.append('g')
.selectAll('circle')
.data(root.descendants())
.join('circle')
.attr(
'transform',
(d) => `
rotate(${(d.x * 180) / Math.PI - 90})
translate(${d.y},0)
`
)
.attr('fill', '#4ecb71')
.attr('r', (d) => {
return d.children ? 5 : sizeScale(d.value)
})
// add labels
const textGroups = svgViz.append('g').attr('id', 'labels')
textGroups
.selectAll('g')
.data(root.descendants())
.join('g')
//.filter((d) => (showHierarchyLabels ? true : !d.children)) // if showHierarchyLabels is false, hide non-leaf nodes
.attr(
'transform',
(d) => `
rotate(${(d.x * 180) / Math.PI - 90})
translate(${d.y},0)
rotate(${d.x >= Math.PI ? 180 : 0})
`
)
.append('text')
.attr('x', 0)
.attr('y', 0)
.attr('text-anchor', 'middle')
.attr('dominant-baseline', 'text-before-edge')
.selectAll('tspan')
.data((d) => {
// if the node has children
// pass just its name in hierarhcy
if (d.children) {
return [
{
string: d.data[0],
x: d.x < Math.PI === !d.children ? 6 : -6,
align: d.x < Math.PI === !d.children ? 'start' : 'end',
//style: styles['labelSecondary'],
hierarchy: true,
},
]
}
// else pass the mapped labels
else {
const xpos = sizeScale(d.value) + 5;
return [d.data[1].hashtag].map((e, i) => ({
string: e,
x: d.x < Math.PI === !d.children ? xpos : -xpos,
align: d.x < Math.PI === !d.children ? 'start' : 'end',
//style: styles[labelStyles[i]],
}))
}
})
.join('tspan')
.attr('x', (d) => d.x)
.attr('y', 0)
.attr('dy', (d, i) => i * 10)
.attr('text-anchor', (d) => d.align)
// .styles((d, i) => styles[labelStyles[i]])
//.styles((d) => d.style)
.attr('fill', 'white')
.style('font-family', 'Arial, sans-serif')
.style('font-size', (d)=>d.hierarchy?'14px':'7px')
.style('font-weight', 'bold')
.text((d) => d.string)
textGroups.selectAll('text').each(function () {
const sel = d3.select(this)
sel.attr('transform', function (d) {
return `translate(0,${d.children?-8:-4})`
})
})
svg.call(d3.zoom()
.extent([[0, 0], [width, width]])
.scaleExtent([1, 8])
.on("zoom", zoomed)
.on("start", ()=> svg.style("cursor", "grabbing"))
.on("end", ()=> svg.style("cursor", "grab")))
function zoomed({transform}) {
svgZoom.attr("transform", transform);
}
return svg.node();
}