Skip to content

Instantly share code, notes, and snippets.

@RaghavSood
Forked from banksJeremy/soElectionGraphs.js
Created March 5, 2013 01:27
Show Gist options
  • Save RaghavSood/5087280 to your computer and use it in GitHub Desktop.
Save RaghavSood/5087280 to your computer and use it in GitHub Desktop.
var candidates = $( ".vote-count-post" ).map(function() {
var score, name, reputation, helpfulFlags,
postEl, repEl;
score = Math.max( +$( this ).text(), 1 );
postEl = $( this ).closest( "tr" );
postHeight = postEl.find( ".post-text" ).height();
name = postEl.find( ".user-details a" ).text();
repEl = postEl.find( ".user-details .reputation-score" );
reputation = +( repEl.attr( "title" ).replace( /[^0-9]/g, "" ) || repEl.text().replace( /[^0-9]/g, "" ) );
helpfulFlags = +postEl.find( ".user-details" ).filter(function() { return this.childNodes[0].textContent === "helpful flags: "; })[0].childNodes[1].textContent;
return { score: score, name: name, reputation: reputation, helpfulFlags: helpfulFlags, postHeight: postHeight };
}).toArray();
candidates.sort(function(a,b){return b.score - a.score;});
console.log($(candidates).map(function(i, c){ return "" + (i + 1) + ". " + c.name + " [" + c.score + "] (" + c.reputation + " rep, " + c.helpfulFlags + " helpful flags)"; }).toArray().join("\n"));
jQuery.getScript("https://www.google.com/jsapi").then(function() {
google.load("visualization", "1", {
packages: [ "corechart" ],
callback: function() {
graph( "Height of Nomination Post (pixels)", "Current Primary Score", "postHeight", "score" );
graph( "log(Reputation × Helpful Flags)", "log(Current Primary Score)", ["helpfulFlags", "reputation"], "score", true, true );
graph( "Helpful Flags", "Current Primary Score", "helpfulFlags", "score" );
graph( "Reputation", "Current Primary Score", "reputation", "score" );
function graph( xLabel, yLabel, xKey, yKey, logX, logY ) {
var data = new google.visualization.DataTable();
data.addColumn( "number", xLabel );
data.addColumn( "number", yLabel );
data.addColumn( { type: "string", role: "tooltip" } );
for( var i = 0, l = candidates.length; i < l; ++i ) {
var candidate = candidates[ i ], xValue;
if (typeof xKey === "string") {
xValue = candidate[ xKey ];
} else {
xValue = 1;
for ( var j in xKey ) {
xValue = xValue * candidate[ xKey[ j ] ];
}
}
if( xValue != xValue ) { throw "foo"; }
data.addRows( [ [ xValue, candidate[ yKey ], candidate.name ] ] );
}
var target = jQuery("<div style=\"width: 650px; height: 400px; display: inline-block;\"></div>").insertAfter( $( ".post-text" ).first() )[0];
var chart = new google.visualization.ScatterChart(target);
chart.draw( data, {
title: yLabel + " / " + xLabel,
chartArea: {
top: "15%",
width: "80%",
left: "15%",
height: "70%"
},
legend: "none",
pointSize: 4,
colors: [ "#123" ],
hAxis: { title: xLabel, gridlines: { count: 10 }, logScale: !!logY },
vAxis: { title: yLabel, gridlines: { count: 10 }, logScale: !!logX }
});
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment