Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="ru">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title></title>
- <link rel="stylesheet" href="css/style.css">
- <script src="https://d3js.org/d3.v7.min.js"></script>
- </head>
- <body>
- <script>
- let data = {
- "id": "14",
- "name": "ANONIMOUS 0",
- "locked": "3",
- "author": "1",
- "children": [
- {
- "id": "1",
- "name": "ANONIMOUS 1",
- "locked": "3",
- "author": "1",
- "children": []
- },
- {
- "id": "2",
- "name": "ANONIMOUS 2",
- "locked": "3",
- "author": "1",
- "children": []
- },
- {
- "id": "3",
- "name": "ANONIMOUS 3",
- "locked": "3",
- "author": "1",
- "children": []
- },
- {
- "id": "4",
- "name": "ANONIMOUS 4",
- "locked": "3",
- "author": "1",
- "children": []
- }
- ]
- };
- let translate = d3.zoomIdentity.translate(innerWidth / 2, innerHeight / 2).scale(1);
- let zoom = d3.zoom().scaleExtent([.25, 4]).on("zoom", function ({ transform }) {
- if (svg) {
- g.attr("transform", transform);
- }
- });
- let duration = 750;
- let diagonal = d3.linkHorizontal()
- .x(d => d.y)
- .y(d => d.x)
- .source(d => ({ x: d.source.x, y: d.source.y + 50 }))
- .target(d => ({ x: d.target.x, y: d.target.y - 50 }));
- let svg = d3.select('body')
- .append('svg')
- .attr("width", innerWidth)
- .attr("height", innerHeight);
- let g = svg.append('g');
- svg.call(zoom);
- svg.call(zoom.transform, translate);
- let root = d3.hierarchy(data, function (d) { return d.children; });
- let treeLayout = d3.tree().nodeSize([46, 180]);
- root.children.forEach(collapse);
- function collapse(d) {
- if (d.children) {
- d._children = d.children
- d._children.forEach(collapse)
- d.children = null
- }
- }
- root.x0 = 80;
- root.y0 = innerHeight / 2;
- update(root);
- function update(source) {
- var treeData = treeLayout(root);
- let nodes = treeData.descendants();
- let links = treeData.links();
- nodes.forEach(function (d) {
- if (d.parent && d.parent.y === 180) {
- d.y = d.parent.y + 180;
- } else {
- d.y = d.depth * 180;
- }
- });
- let node = g.selectAll('g.node')
- .data(nodes, function (d) { return d.id || (d.id = d.data.id); });
- let link = g.selectAll('path.link')
- .data(links, function (d) { return d.id; });
- let nodeEnter = node.enter().append('g')
- .attr('class', 'node')
- .attr("transform", function (d) {
- return "translate(" + source.y0 + "," + source.x0 + ")";
- })
- .style("cursor", "pointer")
- .on('click', click);
- nodeEnter.append("rect")
- .attr("width", 120)
- .attr("height", 36)
- .attr("transform", "translate(-60,-18)")
- .attr("rx", 18)
- .attr("ry", 18)
- .attr("stroke", "darkseagreen")
- .attr("stroke-width", 2)
- .style("fill", function (d) {
- return d._children ? "darkseagreen" : "#fff";
- });
- nodeEnter.append("text")
- .attr("dy", ".35em")
- .style("text-anchor", "middle")
- .style("cursor", "pointer")
- .style('fill-opacity', 1e-6)
- .text(d => d?.data?.name);
- var nodeUpdate = nodeEnter.merge(node);
- nodeUpdate.transition()
- .duration(duration)
- .attr("transform", function (d) {
- return "translate(" + d.y + "," + d.x + ")";
- });
- nodeUpdate.select("text").transition()
- .duration(duration)
- .style('fill-opacity', 1);
- var nodeExit = node.exit().transition()
- .duration(duration)
- .attr("transform", function (d) {
- return "translate(" + source.y + "," + source.x + ")";
- })
- .remove();
- nodeExit.select('text')
- .style('fill-opacity', 1e-6);
- nodes.forEach(function (d) {
- d.x0 = d.x;
- d.y0 = d.y;
- });
- }
- async function click(event, d) {
- if (!d.children && !d._children) {
- const response = await fetch(`admin-ajax.php?id=${d.id}`);
- const childrenData = await response.json(); // []
- d.children = childrenData;
- d.children.forEach(collapse);
- update(d);
- } else {
- if (d.children) {
- d._children = d.children;
- d.children = null;
- } else {
- d.children = d._children;
- d._children = null;
- }
- update(d);
- }
- if (svg) {
- var currentTransform = d3.zoomTransform(svg.node());
- var xOffset = (innerWidth / 2) - d.y * currentTransform.k;
- var yOffset = (innerHeight / 2) - d.x * currentTransform.k;
- translate = d3.zoomIdentity
- .translate(xOffset, yOffset)
- .scale(currentTransform.k);
- svg.transition()
- .duration(750)
- .call(
- zoom.transform,
- translate
- );
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement