Skip to content

Instantly share code, notes, and snippets.

@nsonnad
Last active December 11, 2015 20:18
Show Gist options
  • Save nsonnad/4653951 to your computer and use it in GitHub Desktop.
Save nsonnad/4653951 to your computer and use it in GitHub Desktop.
Legislative Yuan similarity
<!DOCTYPE html>
<html>
<head>
<script src="https://raw.github.com/mbostock/d3/master/d3.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="https://raw.github.com/betamos/Better-Autocomplete/develop/src/jquery.better-autocomplete.js"></script>
<style type="text/css">
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
</head>
<body>
<div id="chart">
<form id="search">
Legislator: <input type="text" />
<input type="submit" />
</form>
</div>
<script type="text/javascript">
var width = 960,
height = 500
color = d3.scale.ordinal().range(["#0B009A", "#009A16", "#FF821A", "#DAAA6E", "#A5A5A5", "#F45399"]);
var link;
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force().size([width, height])
.charge(-50);
d3.json("names.json", function(nameSet) {
d3.json("links.json", function(linkSet) {
console.log(nameSet.Label)
linkSet.forEach(function(d) {
d.source = nameSet[d.sourceID - 1];
d.target = nameSet[d.targetID - 1];
d.relation = +d.relation;
});
force
.nodes(nameSet)
.start();
var node = svg.selectAll(".node")
.data(nameSet)
.enter().append("circle")
.attr("r", 6)
.attr("class", "node")
.attr("fill", function(d) { return color(d.Party); })
.call(force.drag);
force.on("tick", function() {
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
node.append("title")
.text(function(d) { return d.Label; });
node.on("mousedown", function() {
var linkID = d3.select(this).datum().ID;
drawLinks(linkID)
});
$('#search').submit(function() {
var searchTerm = $('input').val();
var searchFilter = nameSet.filter(function(d) { return d.Label === searchTerm; });
var searchID = searchFilter[0].ID;
drawLinks(searchID)
return false;
});
function drawLinks(a) {
svg.selectAll(".link").remove()
var filterLinks = linkSet.filter(function(d) { return d.sourceID === a; });
force
.nodes(nameSet)
.links(filterLinks)
.linkDistance(function(d) { return -d.relation + 120; })
.start();
console.log(a)
console.log(filterLinks)
var link = svg.selectAll("line")
.data(filterLinks)
.enter().append("svg:line")
.attr("class", "link");
force.on("tick", function() {
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
});
};
});
});
</script>
</body>
</html>
[{"ID":1,"Label":"丁守中","Party":"KMT"},
{"ID":2,"Label":"孔文吉","Party":"KMT"},
{"ID":3,"Label":"尤美女","Party":"DPP"},
{"ID":4,"Label":"王廷升","Party":"KMT"},
{"ID":5,"Label":"王育敏","Party":"KMT"},
{"ID":6,"Label":"王金平","Party":"KMT"},
{"ID":7,"Label":"王進士","Party":"KMT"},
{"ID":8,"Label":"王惠美","Party":"KMT"},
{"ID":9,"Label":"田秋堇","Party":"DPP"},
{"ID":10,"Label":"江啟臣","Party":"KMT"},
{"ID":11,"Label":"江惠貞","Party":"KMT"},
{"ID":12,"Label":"何欣純","Party":"DPP"},
{"ID":13,"Label":"吳宜臻","Party":"DPP"},
{"ID":14,"Label":"吳育仁","Party":"KMT"},
{"ID":15,"Label":"吳育昇","Party":"KMT"},
{"ID":16,"Label":"吳秉叡","Party":"DPP"},
{"ID":17,"Label":"呂玉玲","Party":"KMT"},
{"ID":18,"Label":"呂學樟","Party":"KMT"},
{"ID":19,"Label":"李昆澤","Party":"DPP"},
{"ID":20,"Label":"李俊俋","Party":"DPP"},
{"ID":21,"Label":"李桐豪","Party":"PFP"},
{"ID":22,"Label":"李貴敏","Party":"KMT"},
{"ID":23,"Label":"李慶華","Party":"KMT"},
{"ID":24,"Label":"李應元","Party":"DPP"},
{"ID":25,"Label":"李鴻鈞","Party":"KMT"},
{"ID":26,"Label":"邱文彥","Party":"KMT"},
{"ID":27,"Label":"邱志偉","Party":"DPP"},
{"ID":28,"Label":"邱議瑩","Party":"DPP"},
{"ID":29,"Label":"林世嘉","Party":"TSU"},
{"ID":30,"Label":"林正二","Party":"PFP"},
{"ID":31,"Label":"林佳龍","Party":"DPP"},
{"ID":32,"Label":"林岱樺","Party":"DPP"},
{"ID":33,"Label":"林明溱","Party":"KMT"},
{"ID":34,"Label":"林郁方","Party":"KMT"},
{"ID":35,"Label":"林國正","Party":"KMT"},
{"ID":36,"Label":"林淑芬","Party":"DPP"},
{"ID":37,"Label":"林滄敏","Party":"KMT"},
{"ID":38,"Label":"林德福","Party":"KMT"},
{"ID":39,"Label":"林鴻池","Party":"KMT"},
{"ID":40,"Label":"姚文智","Party":"DPP"},
{"ID":41,"Label":"柯建銘","Party":"DPP"},
{"ID":42,"Label":"段宜康","Party":"DPP"},
{"ID":43,"Label":"洪秀柱","Party":"KMT"},
{"ID":44,"Label":"紀國棟","Party":"KMT"},
{"ID":45,"Label":"孫大千","Party":"KMT"},
{"ID":46,"Label":"徐少萍","Party":"KMT"},
{"ID":47,"Label":"徐欣瑩","Party":"KMT"},
{"ID":48,"Label":"徐耀昌","Party":"KMT"},
{"ID":49,"Label":"翁重鈞","Party":"KMT"},
{"ID":50,"Label":"陳其邁","Party":"DPP"},
{"ID":51,"Label":"陳明文","Party":"DPP"},
{"ID":52,"Label":"陳亭妃","Party":"DPP"},
{"ID":53,"Label":"陳唐山","Party":"DPP"},
{"ID":54,"Label":"陳根德","Party":"KMT"},
{"ID":55,"Label":"陳淑慧","Party":"KMT"},
{"ID":56,"Label":"陳雪生","Party":"N/A"},
{"ID":57,"Label":"陳超明","Party":"KMT"},
{"ID":58,"Label":"陳節如","Party":"DPP"},
{"ID":59,"Label":"陳碧涵","Party":"KMT"},
{"ID":60,"Label":"陳歐珀","Party":"DPP"},
{"ID":61,"Label":"陳學聖","Party":"KMT"},
{"ID":62,"Label":"陳鎮湘","Party":"KMT"},
{"ID":63,"Label":"馬文君","Party":"KMT"},
{"ID":64,"Label":"高志鵬","Party":"DPP"},
{"ID":65,"Label":"高金素梅","Party":"NSU"},
{"ID":66,"Label":"張嘉郡","Party":"KMT"},
{"ID":67,"Label":"張慶忠","Party":"KMT"},
{"ID":68,"Label":"張曉風","Party":"PFP"},
{"ID":69,"Label":"許忠信","Party":"TSU"},
{"ID":70,"Label":"許添財","Party":"DPP"},
{"ID":71,"Label":"許智傑","Party":"DPP"},
{"ID":72,"Label":"曾巨威","Party":"KMT"},
{"ID":73,"Label":"葉宜津","Party":"DPP"},
{"ID":74,"Label":"費鴻泰","Party":"KMT"},
{"ID":75,"Label":"黃文玲","Party":"TSU"},
{"ID":76,"Label":"黃志雄","Party":"KMT"},
{"ID":77,"Label":"黃昭順","Party":"KMT"},
{"ID":78,"Label":"黃偉哲","Party":"DPP"},
{"ID":79,"Label":"楊玉欣","Party":"KMT"},
{"ID":80,"Label":"楊應雄","Party":"KMT"},
{"ID":81,"Label":"楊曜","Party":"DPP"},
{"ID":82,"Label":"楊瓊瓔","Party":"KMT"},
{"ID":83,"Label":"楊麗環","Party":"KMT"},
{"ID":84,"Label":"詹凱臣","Party":"KMT"},
{"ID":85,"Label":"廖正井","Party":"KMT"},
{"ID":86,"Label":"廖國棟","Party":"KMT"},
{"ID":87,"Label":"管碧玲","Party":"DPP"},
{"ID":88,"Label":"蔡正元","Party":"KMT"},
{"ID":89,"Label":"蔡其昌","Party":"DPP"},
{"ID":90,"Label":"蔡煌瑯","Party":"DPP"},
{"ID":91,"Label":"蔡錦隆","Party":"KMT"},
{"ID":92,"Label":"蔣乃辛","Party":"KMT"},
{"ID":93,"Label":"趙天麟","Party":"DPP"},
{"ID":94,"Label":"鄭天財","Party":"KMT"},
{"ID":95,"Label":"鄭汝芬","Party":"KMT"},
{"ID":96,"Label":"鄭麗君","Party":"DPP"},
{"ID":97,"Label":"劉建國","Party":"DPP"},
{"ID":98,"Label":"劉櫂豪","Party":"DPP"},
{"ID":99,"Label":"潘孟安","Party":"DPP"},
{"ID":100,"Label":"潘維剛","Party":"KMT"},
{"ID":101,"Label":"盧秀燕","Party":"KMT"},
{"ID":102,"Label":"盧嘉辰","Party":"KMT"},
{"ID":103,"Label":"蕭美琴","Party":"DPP"},
{"ID":104,"Label":"薛凌","Party":"DPP"},
{"ID":105,"Label":"賴士葆","Party":"KMT"},
{"ID":106,"Label":"謝國樑","Party":"KMT"},
{"ID":107,"Label":"魏明谷","Party":"DPP"},
{"ID":108,"Label":"簡東明","Party":"KMT"},
{"ID":109,"Label":"顏清標","Party":"NSU"},
{"ID":110,"Label":"羅明才","Party":"KMT"},
{"ID":111,"Label":"羅淑蕾","Party":"KMT"},
{"ID":112,"Label":"蘇清泉","Party":"KMT"},
{"ID":113,"Label":"蘇震清","Party":"DPP"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment