Public
Edited
Dec 1, 2023
Insert cell
Insert cell
output = FileAttachment("output.json").json()
Insert cell
simple1 = {
// 指定圖表的尺寸。
const width = 1500;
const height = 1000;

// 計算圖形並啟動力模擬。
const root = d3.hierarchy(output);
const links = root.links();
const nodes = root.descendants();
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(100).strength(1))
.force("charge", d3.forceManyBody().strength(-400))
.force("x", d3.forceX())
.force("y", d3.forceY());

// 創建容器 SVG。
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto;");

// 添加連結。
const link = svg.append("g")
.attr("stroke", "#00f")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line");
const linkForce = d3.forceLink(links)
.id(d => d.id)
.distance(1000) // 增加連結的距離
.strength(1); // 可選:設定連結的強度

// 設定節點的顏色,根據階層關係選擇不同的顏色
const colorScale = d3.scaleOrdinal(d3.schemeCategory10); // 使用 D3 的顏色比例尺
// 添加節點。
const node = svg.append("g")
.selectAll("g")
.data(nodes)
.join("g")
.attr("transform", d => `translate(${d.x},${d.y})`) // 定位節點

// 添加節點外框
const circleRadius = 20; // 調整圓圈半徑大小
node.append("circle")
.attr("r", circleRadius)
.attr("fill", "white") // 內部填充顏色
.attr("stroke", d => colorScale(d.depth)) // 外框顏色根據節點深度
.attr("stroke-width", 3);
// 設定節點初始位置在畫布的中間
nodes.forEach(node => {
node.y = 0; // 將y座標設定在畫布的中間
});
simulation.on("tick", () => {
node.attr("transform", d => `translate(${d.x},${d.y})`); // 更新節點位置
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
});

invalidation.then(() => simulation.stop());

return svg.node();
}

Insert cell
simple2 = {
// 指定圖表的尺寸。
const width = 1500;
const height = 1000;

// 計算圖形並啟動力模擬。
const root = d3.hierarchy(output);
const links = root.links();
const nodes = root.descendants();
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(100).strength(1))
.force("charge", d3.forceManyBody().strength(-400))
.force("x", d3.forceX())
.force("y", d3.forceY());

// 創建容器 SVG。
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto;");

// 添加連結。
const link = svg.append("g")
.attr("stroke", "#00f")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line");
const linkForce = d3.forceLink(links)
.id(d => d.id)
.distance(1000) // 增加連結的距離
.strength(1); // 可選:設定連結的強度

// 設定節點的顏色,根據階層關係選擇不同的顏色
const colorScale = d3.scaleOrdinal(d3.schemeCategory10); // 使用 D3 的顏色比例尺
// 添加節點。
const node = svg.append("g")
.selectAll("g")
.data(nodes)
.join("g")
.attr("transform", d => `translate(${d.x},${d.y})`) // 定位節點
.call(drag(simulation));//拖拉功能開啟

// 添加節點外框
const circleRadius = 20; // 調整圓圈半徑大小
node.append("circle")
.attr("r", circleRadius)
.attr("fill", "white") // 內部填充顏色
.attr("stroke", d => colorScale(d.depth)) // 外框顏色根據節點深度
.attr("stroke-width", 3);
// 設定節點初始位置在畫布的中間
nodes.forEach(node => {
node.y = 0; // 將y座標設定在畫布的中間
});

simulation.on("tick", () => {
node.attr("transform", d => `translate(${d.x},${d.y})`); // 更新節點位置
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
});

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
simple3 = {
// 指定圖表的尺寸。
const width = 1500;
const height = 1000;
// 計算圖形並啟動力模擬。
const root = d3.hierarchy(output);
const links = root.links();
const nodes = root.descendants();
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(100).strength(1))
.force("charge", d3.forceManyBody().strength(-400))
.force("x", d3.forceX())
.force("y", d3.forceY());

// 創建容器 SVG。
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto;");

// 添加連結。
const link = svg.append("g")
.attr("stroke", "#00f")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line");
const linkForce = d3.forceLink(links)
.id(d => d.id)
.distance(1000) // 增加連結的距離
.strength(1); // 可選:設定連結的強度

// 設定節點的顏色,根據階層關係選擇不同的顏色
const colorScale = d3.scaleOrdinal(d3.schemeCategory10); // 使用 D3 的顏色比例尺
// 添加節點。
const node = svg.append("g")
.selectAll("g")
.data(nodes)
.join("g")
.attr("transform", d => `translate(${d.x},${d.depth * 100})`) // 定位節點
.call(drag(simulation));

// 添加節點外框
const circleRadius = 20; // 調整圓圈半徑大小
node.append("circle")
.attr("r", circleRadius)
.attr("fill", "white") // 內部填充顏色
.attr("stroke", d => colorScale(d.depth)) // 外框顏色根據節點深度
.attr("stroke-width", 3);

//設定圖片大小
const size_offset = 1.2;//控制內圖片大小
// 計算偏移量
const offset = size_offset / 2;//控制內圖片放置位置的偏移量
// 添加內圖
node.append("image")
.attr("x", -(circleRadius * offset)) // 將圖片的左上角放在圓圈框的左上角
.attr("y", -(circleRadius * offset)) // 將圖片的左上角放在圓圈框的左上角
.attr("width", circleRadius * size_offset) // 設置圖片寬度為圓圈直徑的兩倍
.attr("height", circleRadius * size_offset) // 設置圖片高度為圓圈直徑的兩倍
.attr("href",d => d.data.image_url);
// 設定節點初始位置在畫布的中間
nodes.forEach(node => {
node.y = 0; // 將y座標設定在畫布的中間
});
simulation.on("tick", () => {
node.attr("transform", d => `translate(${d.x},${d.y})`); // 更新節點位置
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
});

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
drag = simulation => {
function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(event, d) {
d.fx = event.x;
d.fy = event.y;
}
function dragended(event, d) {
if (!event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
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