Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<style> | ||
|
||
.link { | ||
stroke: #000; | ||
stroke-width: 1.5px; | ||
} | ||
|
||
.node circle{ | ||
cursor: pointer; | ||
fill: #ccc; | ||
stroke: #000; | ||
stroke-width: 1.5px; | ||
|
||
} | ||
|
||
.node circle.hover { | ||
fill:#DDD | ||
} | ||
.node circle.selected{ | ||
fill:#EEE | ||
} | ||
|
||
.d3-tip { | ||
padding:5px; | ||
border: 1px solid #bfbfbf; | ||
} | ||
|
||
|
||
|
||
</style> | ||
<section class="category-svg"> | ||
|
||
</section> | ||
<script type="text/javascript" charset="utf-8"> | ||
|
||
var width = 250, | ||
height = 250; | ||
|
||
var force = d3.layout.force() | ||
.charge(-150) | ||
.size([width, height]) | ||
.on("tick", tick); | ||
|
||
|
||
var svg = d3.select(".category-svg").append("svg") | ||
.attr("width", width) | ||
.attr("height", height); | ||
|
||
var link = svg.selectAll(".link"), | ||
node = svg.selectAll(".node"); | ||
|
||
function categories(){ | ||
return _(window.site.posts).chain() | ||
.map(function(p){return p.categories}) | ||
.flatten().value() | ||
} | ||
|
||
function nodes(){ | ||
return _(categories()).chain().countBy(_.identity).map(function(v,k){ | ||
return {name:k,count:v} | ||
}).sortBy(function(n){return n.count}).reverse().value() | ||
} | ||
function bothCategories(post,c1,c2){ | ||
return _([c1,c2]).all(function (c){ | ||
return _(post.categories).contains(c) | ||
}) | ||
} | ||
|
||
function relations(){ | ||
var cs=_(categories()).uniq() | ||
var couple=[] | ||
for (a in cs) { | ||
for (b in cs){ | ||
couple.push([cs[a],cs[b]]) | ||
}} | ||
return _(couple).map(function(c){ | ||
return [c,_(window.site.posts).chain().filter(function(p){return bothCategories(p,c[0],c[1])}).size().value()] | ||
}) | ||
} | ||
var ns=nodes() | ||
|
||
var graph={ | ||
nodes:ns | ||
, | ||
links:_(relations()).chain().map(function(p){ | ||
return { | ||
target:_(ns).find(function(n){return n.name==p[0][0]}), | ||
source:_(ns).find(function(n){return n.name==p[0][1]}), | ||
postCount:p[1] | ||
} | ||
}).filter(function(l){ | ||
return l.postCount>0 | ||
}).value() | ||
} | ||
|
||
var max=_(graph.nodes).chain().pluck("count").max().value() | ||
var scale=d3.scale.linear() | ||
.domain([1, max]) | ||
.range([10,30]); | ||
|
||
var relationScale=d3.scale.linear() | ||
.domain([_(graph.links).chain().pluck("postCount").max().value(),1]) | ||
.range([10,40]) | ||
|
||
force | ||
.nodes(graph.nodes) | ||
.linkDistance(function(l){return relationScale(l.postCount)}) | ||
.links(graph.links) | ||
.start(); | ||
|
||
link = link.data(graph.links) | ||
.enter().append("line") | ||
.attr("class", "link"); | ||
var tip = d3.tip() | ||
.attr('class', 'd3-tip') | ||
.offset([0, 0]) | ||
.html(function(d) { | ||
return d.name | ||
}) | ||
node = node.data(graph.nodes) | ||
.enter().append("g") | ||
.attr("class", "node") | ||
.on("mouseover", tip.show | ||
) | ||
.on("mouseout", tip.hide | ||
) | ||
node.append("circle").attr("r", function(n){return scale(n.count)}) | ||
.on("mouseover", function(){ | ||
d3.select(this).classed("selected",true) | ||
}) | ||
.on("mouseout", function(){ | ||
d3.select(this).classed("selected",false) | ||
}) | ||
.on("click",function(c){ | ||
var url="/blog/filter/filter.html" | ||
window.location.href=buildUrl(url,{filterName:"category",filterValue:c.name,filterDes:"Category: "+c.name}) | ||
}) | ||
node.call(tip) | ||
|
||
function tick() { | ||
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; }); | ||
|
||
node | ||
.attr("transform", function(d) { | ||
return "translate(" + d.x + "," + d.y + ")"; }); | ||
} | ||
|
||
|
||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
layout: page | ||
title: Blog Archive | ||
footer: false | ||
--- | ||
|
||
<div id="blog-filtered"> | ||
|
||
</div> | ||
<script type="text/javascript" charset="utf-8"> | ||
function filter(){ | ||
window.filter={ | ||
name:getParameterByName("filterName"), | ||
value:getParameterByName("filterValue"), | ||
des:getParameterByName("filterDes") | ||
} | ||
if(window.filter.name=="category"){ | ||
return _(window.site.posts).filter(function(p){ | ||
return _(p.categories).contains(window.filter.value) | ||
}) | ||
} | ||
|
||
} | ||
function article(post){ | ||
var art=$("<article/>") | ||
art.append($("<h1/>").append($("<a/>").attr("href",post.url).text(post.title))) | ||
return art | ||
} | ||
function update(){ | ||
$("#blog-filtered").empty() | ||
var posts=filter() | ||
_(posts).each(function(p){ | ||
$("#blog-filtered").append(article(p)) | ||
}) | ||
$("h1.entry-title").text(window.filter.des) | ||
} | ||
update() | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function getParameterByName(name) { | ||
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); | ||
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), | ||
results = regex.exec(location.search); | ||
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | ||
} | ||
function buildUrl(url, parameters){ | ||
var qs = ""; | ||
for(var key in parameters) { | ||
var value = parameters[key]; | ||
qs += encodeURIComponent(key) + "=" + encodeURIComponent(value) + "&"; | ||
} | ||
if (qs.length > 0){ | ||
qs = qs.substring(0, qs.length-1); //chop off last "&" | ||
url = url + "?" + qs; | ||
} | ||
return url; | ||
} |
Oops, something went wrong.