Files
tunmnlu/task_2/others-answer/DVA-CSE6242-Spring2021-main/hw2-javascript,D3/HW2-mvegesna3/Q2/graph.html
louiscklaw 9035c1312b update,
2025-02-01 02:09:32 +08:00

199 lines
5.4 KiB
HTML

<!DOCTYPE html>
<meta charset="utf-8">
<script type="text/javascript" src="../lib/d3.v5.min.js"></script>
<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;
pointer-events: none;
}
</style>
<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"><head>
<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:display_urn_x003a_schemas-microsoft-com_x003a_office_x003a_office_x0023_Editor msdt:dt="string">Hull, Matthew D</mso:display_urn_x003a_schemas-microsoft-com_x003a_office_x003a_office_x0023_Editor>
<mso:xd_Signature msdt:dt="string"></mso:xd_Signature>
<mso:Order msdt:dt="string">35500.0000000000</mso:Order>
<mso:ComplianceAssetId msdt:dt="string"></mso:ComplianceAssetId>
<mso:TemplateUrl msdt:dt="string"></mso:TemplateUrl>
<mso:xd_ProgID msdt:dt="string"></mso:xd_ProgID>
<mso:display_urn_x003a_schemas-microsoft-com_x003a_office_x003a_office_x0023_Author msdt:dt="string">Hull, Matthew D</mso:display_urn_x003a_schemas-microsoft-com_x003a_office_x003a_office_x0023_Author>
<mso:ContentTypeId msdt:dt="string">0x010100D48F87729E805A4096AD64C4E51DACBE</mso:ContentTypeId>
<mso:_SourceUrl msdt:dt="string"></mso:_SourceUrl>
<mso:_SharedFileIndex msdt:dt="string"></mso:_SharedFileIndex>
</mso:CustomDocumentProperties>
</xml><![endif]-->
<title></title></head><body>
<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);
svg.append("text")
.attr("text-anchor", "right")
.attr("x", 900)
.attr("y", 20)
.attr("font-weight","bold")
.attr("font-size", "40px")
.text("mvegesna3");
// add the links
var path = svg.append("g")
.selectAll("path")
.data(links)
.enter()
.append("path")
.attr("class", function(d) { return "link " + d.type; });
//styling the edges
path.style("stroke", function(d){
if(d.value == 0)
return "gray";
else return "green"})
.style("stroke-dasharray", function(d) {
if(d.value === 0) {
return 'null'; //solid line
} else return '5, 5';})
.style('stroke-width', function(d) {
if(d.value === 0) {
return 4;
} else return 2; })
// define the nodes
var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
var color = d3.scaleLinear().domain([1,10]).range(["white","violet"])
// add the nodes
node.append("circle")
.attr("r", function(d){
d.weight = path.filter(function(l){return l.source.index == d.index || l.target.index == d.index
}).size();
var min_r = 8;
return min_r + (d.weight*5);})
.style("fill", function(d){ return color(d.weight)})
.on("dblclick",unpin_node);
// adding node labels
node.append("svg:text")
.attr("x",12)
.attr("y", 0.1)
.style("vertical-align","top")
.text(function(d) {return d.name})
// 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 + ")";
});
};
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;
};
function dragended(d) {
if (!d3.event.active) force.alphaTarget(0);
if (d.fixed == true) {
d.fx = d.x;
d.fy = d.y;
d3.select(this)
.select("circle")
.style("fill","orange");
}
else {
d.fx = null;
d.fy = null;
d.fixed = true;
}
};
function unpin_node(d){
if (!d3.event.active) force.alphaTarget(0);
d.fx = null;
d.fy = null;
d3.select(this)
.style("fill", function(d){ return color(d.weight)})
}
}).catch(function(error) {
console.log(error);
});
</script>
</body>
</html>