Files
tunmnlu/task_2/others-answer/isye_6242-main/HW 2/graph.html
louiscklaw 9035c1312b update,
2025-02-01 02:09:32 +08:00

233 lines
6.0 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);
var name = [{
x: 700,
y: 30,
text: "icanlapan3"
}];
var g = svg.selectAll('.someClass')
.data(name)
.enter()
.append("g")
.attr("class","someClass")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
g.append("text")
.style("fill", "black")
.text(function(d) {
return d.text;
})
// add the links
var path = svg.append("g")
.selectAll("path")
.data(links)
.enter()
.append("path")
.attr("class", function(d) { return "link " + d.type; })
.style("stroke", function(d) {
if (d.value == 1 ) { return ('green'); }
else { return 'grey'; }
} )
.style("stroke-width", function(d) {
if (d.value == 1 ) { return 1; }
else { return 3; }
} )
.style("stroke-dasharray", function(d) {
if (d.value == 1 ) { return '5, 5'; }
else { return null; }
} );
// 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))
.on("dblclick", dblclick);
d3.selectAll('g.node')
.each(function(d) {
d.degree = 0;
});
// Calculate degree
links.forEach(function(d){
d.source.degree += 1;
d.target.degree += 1;
});
var minDegree = d3.min(d3.values(nodes), function(d) {return d.degree;})
var maxDegree = d3.max(d3.values(nodes), function(d) {return d.degree;})
var colorScale = d3.scaleOrdinal(d3.schemeYlGnBu[9]);
// 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 minRadius = 3;
return minRadius + (d.weight * 2);
})
.style("fill", function(d) { return colorScale(d.degree)});
node.append("text")
.attr("dx", 12)
.attr("dy", "-.35em")
.style("font-weight", "bold")
.attr("text-anchor", "beginning")
.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();
d3.select(this).classed("fixed", d.fixed = true);
<!-- 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;
}
else {
d.fx = null;
d.fy = null;
}
d3.select(this)
.select("circle")
.style("stroke-width",4);
};
function dblclick(d) {
delete d.fx;
delete d.fy;
d3.select(this).classed("fixed", d.fixed = false)
.select("circle").style("stroke-width",1.5);
};
}).catch(function(error) {
console.log(error);
});
</script>
</body>
</html>