Files
tunmnlu/task_2/hw2_skeleton/Q2/Q2_submit.html
louiscklaw 9035c1312b update,
2025-02-01 02:09:32 +08:00

265 lines
6.6 KiB
HTML

<!DOCTYPE html>
<head>
<style>
path.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
circle {
fill: #ccc;
stroke: #fff;
stroke: black;
stroke-width: 1.5px;
}
text {
fill: #000;
font: 10px sans-serif;
font-weight: bold;
pointer-events: none;
}
.solid {
stroke: solid;
fill: none;
}
.dashed {
stroke-dasharray: 5, 5;
fill: none;
}
</style>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script type="text/javascript" src="../lib/d3.v5.min.js"></script>
<script>
d3.dsv(",", "./board_games.csv", function (d) {
return {
source: d.source,
target: d.target,
value: +d.value,
};
})
.then(function (data) {
var links = data;
var nodes = {};
// compute the distinct nodes from the links.
links.forEach(function (link) {
link.source =
nodes[link.source] ||
(nodes[link.source] = { name: link.source });
link.target =
nodes[link.target] ||
(nodes[link.target] = { name: link.target });
});
var width = 1200,
height = 700;
var force = d3
.forceSimulation()
.nodes(d3.values(nodes))
.force("link", d3.forceLink(links).distance(100))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("x", d3.forceX())
.force("y", d3.forceY())
.force("charge", d3.forceManyBody().strength(-250))
.alphaTarget(1)
.on("tick", tick);
var svg = d3
.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
// #* Add GT username
svg
.append("text")
.attr("id", "credit")
.attr("text-anchor", "end")
.attr("x", width)
.attr("y", 12)
.text("tlou31");
// add the links
var path = svg
.append("g")
.selectAll("path")
.data(links)
.enter()
.append("path")
.attr("class", function (d) {
return "link " + d.type;
});
// define the nodes
var node = svg
.selectAll(".node")
.data(force.nodes())
.enter()
.append("g")
.attr("class", "node")
.on("dblclick", dblClick)
.call(
d3
.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
);
// add the nodes
node
.append("circle")
.attr("id", function (d) {
return d.name.replace(/\s+/g, "").toLowerCase();
})
.attr("r", function (d) {
d.weight = path
.filter(function (l) {
return l.source.index == d.index || l.target.index == d.index;
})
.size();
var minRadius = 5;
return minRadius + d.weight * 2;
})
.style("fill", function (d) {
if (d.weight > 6) {
return "#e34a33";
} else if (d.weight > 3) {
return "#fdbb84";
} else {
return "#fee8c8";
}
});
// .attr("r", 5);
// #* node labels
node
.append("text")
.attr("dx", -5)
.attr("dy", -13)
.text(function (d) {
// console.log('d',d)
// console.log('d.source',d.name)
return d.name;
});
// edgeColor
function edgeColor(d) {
if (d.value == 0) {
return "gray";
} else {
return "green";
}
}
// edgeStyle
function edgeStyle(d) {
if (d.value == 0) {
return "solid";
} else {
return "dashed";
}
}
// edgeWidth
function edgeWidth(d) {
if (d.value == 0) {
return 5;
} else {
return 1;
}
}
// add the curvy lines
function tick() {
path.attr("d", function (d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return (
"M" +
d.source.x +
"," +
d.source.y +
"A" +
dr +
"," +
dr +
" 0 0,1 " +
d.target.x +
"," +
d.target.y
);
});
node.attr("transform", function (d) {
return "translate(" + d.x + "," + d.y + ")";
});
node.select("circle").style("fill", function (d) {
if (d.fixed == true) {
return "cyan";
} else {
if (d.weight > 6) {
return "#e34a33";
} else if (d.weight > 3) {
return "#fdbb84";
} else {
return "#fee8c8";
}
}
});
path.attr("class", edgeStyle);
path.style("stroke", edgeColor);
path.style("stroke-width", edgeWidth);
}
function dragstarted(d) {
if (!d3.event.active) force.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
d.fixed = true;
console.log(d);
}
function dragended(d) {
if (!d3.event.active) force.alphaTarget(0);
if (d.fixed == true) {
d.fx = d.x;
d.fy = d.y;
} else {
d.fx = null;
d.fy = null;
}
}
function dblClick(d) {
console.log(d);
d.fixed = false;
d.fx = null;
d.fy = null;
}
})
.catch(function (error) {
console.log(error);
});
</script>
</body>
</html>