Skip to content

Instantly share code, notes, and snippets.

@herbps10
Last active November 14, 2016 16:52
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 herbps10/674808a51b01df390c5b1807318ec591 to your computer and use it in GitHub Desktop.
Save herbps10/674808a51b01df390c5b1807318ec591 to your computer and use it in GitHub Desktop.
IQ
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.area {
fill: steelblue;
}
.label {
fill: steelblue;
font-size: 15px;
}
.mean-label {
font-weight: bold;
font-size: 12px;
}
.mean-container {
width: 165px;
padding-left: 413px;
}
</style>
<div class='mean-container'>
<input id='mean' type='range' min='90' max='110' value='100'>
<br/>
</div>
<svg></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdn.jsdelivr.net/jstat/latest/jstat.min.js"></script>
<script type='text/javascript'>
//setting up empty data array
var data = [];
var dataBelowCutoff = [];
var cutoffCdf = 0;
var mean;
var sd;
getData(); // popuate data
// line chart based on http://bl.ocks.org/mbostock/3883245
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 50
},
width = 960 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var x = d3.scaleLinear()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var xAxis = d3.axisBottom()
.scale(x);
var yAxis = d3.axisLeft()
.scale(y);
var line = d3.line()
.x(function(d) {
return x(d.q);
})
.y(function(d) {
return y(d.p);
});
var svg = d3.select("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain([40, 160]);
y.domain(d3.extent(data, function(d) {
return d.p;
}));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
var path = svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
var area = d3.area()
.x(function(d) {
return x(d.q);
})
.y(function(d) {
return y(d.p);
})
.y1(y(0));
var areaPath = svg.append('path')
.attr('class', 'area');
var textLabel = svg.append('text')
.attr('class', 'label')
.attr('x', x(71));
var meanLabel = svg.append('text')
.attr('class', 'mean-label')
.attr('y', -10)
.attr('text-anchor', 'middle');
update();
function update() {
path.datum(data).attr("d", line);
areaPath.datum(dataBelowCutoff).attr("d", area);
textLabel
.attr('y', y(dataBelowCutoff[dataBelowCutoff.length - 1].p / 2))
.text(d3.format(".1%")(cutoffCdf));
meanLabel
.attr('x', x(mean))
.text(mean);
}
d3.select('#mean').on('input', function() {
getData();
update();
});
function getData() {
mean = document.getElementById('mean').value;
sd = 15;
data = [];
// loop to populate data array with
// probabily - quantile pairs
for (var i = 40; i < 160; i += 1) {
q = i
p = jStat.normal.pdf(i, mean, sd);
el = {
"q": q,
"p": p
}
data.push(el);
};
dataBelowCutoff = data.slice(0, 31);
cutoffCdf = jStat.normal.cdf(70, mean, sd);
}
</script>
<style type='text/css'>
input[type=range] {
-webkit-appearance: none;
margin: 18px 0;
width: 100%;
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
width: 100%;
height: 8.4px;
cursor: pointer;
animate: 0.2s;
background: steelblue;
box-shadow: inset 0px 0px 5px rgba(0, 0, 0, 0.3);
border-radius: 8px;
}
input[type=range]::-webkit-slider-thumb {
border: 1px solid #000000;
height: 36px;
width: 16px;
border-radius: 3px;
background: #ffffff;
cursor: pointer;
-webkit-appearance: none;
margin-top: -14px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
background: steelblue;
}
input[type=range]::-moz-range-track {
width: 100%;
height: 8.4px;
cursor: pointer;
animate: 0.2s;
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
background: steelblue;
border-radius: 1.3px;
}
input[type=range]::-moz-range-thumb {
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
border: 1px solid #000000;
height: 36px;
width: 16px;
border-radius: 3px;
background: #ffffff;
cursor: pointer;
}
input[type=range]::-ms-track {
width: 100%;
height: 8.4px;
cursor: pointer;
animate: 0.2s;
background: transparent;
border-color: transparent;
border-width: 16px 0;
color: transparent;
}
input[type=range]::-ms-fill-lower {
background: #2a6495;
border: 0.2px solid #010101;
border-radius: 2.6px;
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
}
input[type=range]::-ms-fill-upper {
background: #3071a9;
border: 0.2px solid #010101;
border-radius: 2.6px;
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
}
input[type=range]::-ms-thumb {
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
border: 1px solid #000000;
height: 36px;
width: 16px;
border-radius: 3px;
background: #ffffff;
cursor: pointer;
}
input[type=range]:focus::-ms-fill-lower {
background: #3071a9;
}
input[type=range]:focus::-ms-fill-upper {
background: #367ebd;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment