Published
Edited
Oct 14, 2020
3 stars
Insert cell
Insert cell
chart = {
const root = tree(d3.hierarchy(data))
const svg = d3.create("svg")
.style("background-color", "#132031");;

// ветки
svg.append("g")
.attr("fill", "none")
.attr("stroke", '#999')
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.join("path")
.attr("d", d3.linkRadial()
.angle(d => d.x)
.radius(d => d.y))
.attr("stroke", (d, i) => d.source.data.children[0].color);
// кружки
svg.append("g")
.selectAll("circle")
.data(root.descendants())
.join("circle")
.attr("transform", d => `
rotate(${d.x * 180 / Math.PI - 90})
translate(${d.y},0)
`)
.attr("stroke", '#fff')
.attr("fill", d => d.data.color ? d.data.color : '#fff')
.attr("opacity", '0.6')
.attr("r", d => d.data.children.length ? d.data.children.length * 1.2 : 2);

return svg.attr("viewBox", autoBox).node();
}
Insert cell
function autoBox() {
document.body.appendChild(this);
const {x, y, width, height} = this.getBBox();
document.body.removeChild(this);
return [x, y, width, height];
}
Insert cell
data = generateTree();
Insert cell
generateTree = () => {
const generateChildren = (count, color) => {
return new Array(randomInteger(2, 7))
.fill({children: []})
.map((item, i) => {
if (count === generationCount) {
return {...item, color: i < 7 ? dataColors[i] : dataColors[i % 7]}
}
return {...item, color}
})
.map((item) => ({...item, children: count ? generateChildren(count - 1, item.color) : []}))
}
return {children: generateChildren(generationCount, '')};
}
Insert cell
tree = d3.cluster()
.size([2 * Math.PI, radius])
Insert cell
width = 954
Insert cell
radius = width / 2
Insert cell
generationCount = 4

Insert cell
dataColors = ['#9a5d9a', '#7b69e7', '#6ecf83', '#ed8674', '#e0dc7b', '#ff85bb', '#b75fe1']
Insert cell
d3 = require("d3@5")
Insert cell
randomInteger = (min, max) => {
// случайное число от min до (max+1)
let rand = min + Math.random() * (max + 1 - min);
return Math.floor(rand);
}
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