Skip to content

Instantly share code, notes, and snippets.

@ianthomas
Created July 11, 2015 15:38
Show Gist options
  • Save ianthomas/2791af0fdca8942d8e99 to your computer and use it in GitHub Desktop.
Save ianthomas/2791af0fdca8942d8e99 to your computer and use it in GitHub Desktop.
Crossfilter test array
{"description":"Crossfilter test array","endpoint":"","display":"svg","public":true,"require":[{"name":"crossfilter min","url":"https://rawgit.com/square/crossfilter/master/crossfilter.min.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"data.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"data2.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/jcGulOS.png"}
[
{
"keyword":["@horaciorlarreta","@gabimichetti"],
"date_tweet":"2015-04-16 01:01:44",
"sent":"N"
},
{
"keyword":["@bonelliok"],
"date_tweet":"2015-04-16 01:01:53",
"sent":"P+"
},
{
"keyword":["@horaciorlarreta","@gabimichetti"],
"date_tweet":"2015-04-16 01:02:25",
"sent":"P"
},
{
"keyword":["@horaciorlarreta","@adosvoces_tn"],
"date_tweet":"2015-04-16 01:01:12",
"sent":"P"
},
{
"keyword":["@horaciorlarreta","@gabimichetti","@adosvoces_tn"],
"date_tweet":"2015-04-16 01:04:32",
"sent":"N+"
},
{
"keyword":["@horaciorlarreta"],
"date_tweet":"2015-04-16 01:04:33",
"sent":"N"
},
{
"keyword":["@horaciorlarreta","@gabimichetti"],
"date_tweet":"2015-04-16 01:04:53",
"sent":"NEU"
},
{
"keyword":["@horaciorlarreta"],
"date_tweet":"2015-04-16 01:05:00",
"sent":"P"
},
{
"keyword":["@horaciorlarreta","@gabimichetti","@adosvoces_tn"],
"date_tweet":"2015-04-16 01:05:11",
"sent":"N"
}
]
// Thanks Enjalot
// http://tributary.io/inlet/4700608
// https://www.youtube.com/watch?v=6CQmRCOtBQ0
// Array of keywords : http://stackoverflow.com/questions/17524627/is-there-a-way-to-tell-crossfilter-to-treat-elements-of-array-as-separate-record
var svg = d3.select("svg");
var xf = crossfilter();
tributary.inlets = tributary.data2;
xf.add(tributary.inlets); //load data
//console.log(tributary.inlets[1]);
// Define dimensions
var keywords = xf.dimension(function(d) { return d.keyword; });
var time = xf.dimension(function(d) { return new Date(d.date_tweet) });
var sent = xf.dimension(function(d) { return d.sent });
//Time filter
// Change hour or minute to see filtered data
var hour1=0;
var min1=0;
var hour2=5;
var min2=0;
var start = d3.time.format("%Y-%m-%d %H:%M").parse("2015-04-16 "+hour1+":"+min1);
var end = d3.time.format("%Y-%m-%d %H:%M").parse("2015-04-17 "+hour2+":"+min2);
// time.filter([start, end]);
//Sentiment filter (N+,N,NEU,P,P+)
//sent.filter(["P","P+"]);
//sent.filter(["N","N+"]);
//sent.filter("NEU");
// Group by keyword
var keyGroup = keywords.groupAll().reduce(reduceAdd, reduceRemove, reduceInitial).value();
// hack to make dc.js charts work
keyGroup.all = function() {
var newObject = [];
for (var key in this) {
if (this.hasOwnProperty(key) && key != "all") {
newObject.push({
key: key,
value: this[key]
});
}
}
return newObject;
}
//console.log(keyGroup);
var group = keyGroup.all().sort(function(a,b) {
return a.value < b.value ? 1 : -1
} );
var keySel = svg.selectAll("g.keyword")
.data(group);
var keyEnter = keySel.enter()
.append("g")
.classed("keyword",true);
keyEnter.append("text").classed("name", true);
keyEnter.append("rect");
keyEnter.append("text").classed("number", true);
var barWidth = d3.scale.linear()
.domain(d3.extent(group, function(d) { return d.value }))
.range([1, 98]);
// make keywords visible
keySel.attr({
"transform": function(d,i) {
var x = 178;
var y = 36 + i * 22;
return "translate(" + [x,y] + ")";
}
})
// show keyword
keySel.select("text.name")
.text(function(d){
return d.key ;
})
.attr("alignment-baseline", "hanging")
.attr("y", 3);
// show number
keySel.select("text.number")
.text(function(d) { return d.value })
.attr("alignment-baseline", "hanging")
.attr("x", function(d) { return 9 + barWidth(d.value) })
.attr("y", 3)
// show rectangle
keySel.select("rect")
.attr({
width: function(d) { return barWidth(d.value) },
height: 20,
x: 5
})
function reduceAdd(p, v) {
v.keyword.forEach (function(val, idx) {
p[val] = (p[val] || 0) + 1; //increment counts
});
return p;
}
function reduceRemove(p, v) {
v.keyword.forEach (function(val, idx) {
p[val] = (p[val] || 0) - 1; //decrement counts
});
return p;
}
function reduceInitial() {
return {};
}
.keyword text {
alignment-baseline: hanging;
}
text.name {
text-anchor: end;
}
.keyword rect {
fill: #915ac8;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment