Skip to content

Instantly share code, notes, and snippets.

@davekinkead
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davekinkead/11177796 to your computer and use it in GitHub Desktop.
Save davekinkead/11177796 to your computer and use it in GitHub Desktop.
Epistemic 0.51 Full Clustering
<html>
<head>
<title>Montecarlo simulation for epistemic justifications of democracy</title>
<script src="http://mbostock.github.com/d3/d3.v2.js"></script>
<style>
/* tell the SVG path to be a thin blue line without any area fill */
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
.axis {
shape-rendering: crispEdges;
}
.x.axis line {
stroke: lightgrey;
}
.x.axis .minor {
stroke-opacity: .5;
}
.x.axis path {
display: none;
}
.y.axis line, .y.axis path {
fill: none;
stroke: #000;
}
</style>
</head>
<body>
<div id="graph" class="aGraph" style="position:absolute;top:0px;left:0; float:left;"></div>
<script>
/* implementation heavily influenced by http://bl.ocks.org/1166403 */
// define dimensions of graph
var m = [80, 80, 80, 80]; // margins
var w = 1000 - m[1] - m[3]; // width
var h = 600 - m[0] - m[2]; // height
// create a simple data array that we'll plot with a line (this array represents only the Y values, X will just be the index location)
var data = [1.000000000000000, 0.513666666666667, 0.504000000000000, 0.519900000000000, 0.508266666666667, 0.520428571428571, 0.509571428571429, 0.507750000000000, 0.511422222222221, 0.517109090909091, 0.511257575757575, 0.511487179487180, 0.508439560439560, 0.514152380952382, 0.512191666666666, 0.512566176470588, 0.512117647058824, 0.511064327485380, 0.508884210526315, 0.512047619047619, 0.514523809523810, 0.514498023715414, 0.511550724637681, 0.513286666666667, 0.513236923076924, 0.511575498575499, 0.512960317460316, 0.511995073891626, 0.509034482758621, 0.518630107526881, 0.511604838709679, 0.508842803030303, 0.511670231729053, 0.509793277310925, 0.512228571428572, 0.514444444444444, 0.514035561877668, 0.510631578947368, 0.510792307692309, 0.513374390243903, 0.512822299651568, 0.509977851605758, 0.511181818181818, 0.513818181818182, 0.513953623188405, 0.511195189639223, 0.512492021276596, 0.511812925170068, 0.514284081632652, 0.510876078431373];
// X scale will fit all values from data[] within pixels 0-w
var x = d3.scale.linear().domain([0, data.length]).range([0, w]);
// Y scale will fit values from 0-10 within pixels h-0 (Note the inverted domain for the y-scale: bigger is up!)
var y = d3.scale.linear().domain([0.3, 1]).range([h, 0]);
// automatically determining max range can work something like this
// var y = d3.scale.linear().domain([0, d3.max(data)]).range([h, 0]);
// create a line function that can convert data[] into x and y points
var line = d3.svg.line()
// assign the X function to plot our line as we wish
.x(function(d,i) {
// verbose logging to show what's actually being done
console.log('Plotting X value for data point: ' + d + ' using index: ' + i + ' to be at: ' + x(i) + ' using our xScale.');
// return the X coordinate where we want to plot this datapoint
return x(i);
})
.y(function(d) {
// verbose logging to show what's actually being done
console.log('Plotting Y value for data point: ' + d + ' to be at: ' + y(d) + " using our yScale.");
// return the Y coordinate where we want to plot this datapoint
return y(d);
})
// Add an SVG element with the desired dimensions and margin.
var graph = d3.select("#graph").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
// create yAxis
var xAxis = d3.svg.axis().scale(x).tickSize(-h).tickSubdivide(true);
// Add the x-axis.
graph.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
// create left yAxis
var yAxisLeft = d3.svg.axis().scale(y).ticks(4).orient("left");
// Add the y-axis to the left
graph.append("svg:g")
.attr("class", "y axis")
.attr("transform", "translate(-25,0)")
.call(yAxisLeft);
// Add the line by appending an svg:path element with the data line we created above
// do this AFTER the axes above so that the line is above the tick-lines
graph.append("svg:path").attr("d", line(data));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment