Skip to content

Instantly share code, notes, and snippets.

View nkabrown's full-sized avatar

Nathan Brown nkabrown

View GitHub Profile
@nkabrown
nkabrown / createFilter.js
Last active September 10, 2015 14:13
Reusable function to create data agnostic select filters
// on function call autocreate data agnostic filters
function createFilter(name, data) {
var items = [];
for (i in data) {
items.push(data[i][name]);
}
var list = items.filter(uniqueValues);
list.sort();
$.each(list, function(i, item) {
@nkabrown
nkabrown / percentage.js
Created November 11, 2015 01:53
create or convert a chart to display elements with percentages in d3.js
// percentage = value / total * 100
// store total in variable
var total = d3.sum(data, function(d) { return d.values; });
// set upper domain of y-axis not to 100% but to the percentage of the greatest value in your data
y.domain([0, d3.max(data, function(d) { return Math.round((d.values / total) * 100); })]);
// percentage in bar chart
var bars = d3.selectAll('.bar')
.data(data)
@nkabrown
nkabrown / actantial-model.md
Last active February 22, 2016 20:12
modeling narrative structure
@nkabrown
nkabrown / index.html
Last active April 7, 2018 04:25
d3 triangles
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.container {
width: 90%;
}
.triangle-grid {
width: 315px;
height: 315px;
margin: 0 auto;
@nkabrown
nkabrown / lastChild.js
Last active March 1, 2016 14:48
a way to access and style last child elements with d3 and DOM web api
// a workaround to apply style properties to only one element of a collection of elements in a d3 selection
d3.select('.container-div')
.selectAll('div')
.data(data)
.enter().append('div')
.attr('class', 'contained-divs')
// show last child element on hover
.on('mouseover', function(d) {
var s = d3.select(this)[0][0],
s0 = s.lastChild;
@nkabrown
nkabrown / index.html
Last active March 3, 2016 14:18
d3 contour map
<!DOCTYPE html>
<meta charset="UTF-8">
<title>D3 Contour Map</title>
<style>
html,
body {
background: #000;
}
.container {
@nkabrown
nkabrown / index.html
Last active March 4, 2016 15:03
d3 geojson map of allegheny county pa polling stations
<!DOCTYPE html>
<meta charset="UTF-8">
<title>D3 GeoJSON</title>
<style>
html,
body {
background-color: #000;
}
</style>
<div class="container">
@nkabrown
nkabrown / explanation.js
Last active March 14, 2016 19:53
replace label with explanation on hover
// function to replace label with label's explanation on hover.
function mouseover() {
const s = d3.select(this)[0][0];
s.innerHTML = 'true positive rate';
s.className = 'explanation';
}
function mouseout() {
const s = d3.select(this)[0][0];
s.innerHTML = 'Sensitivity';
@nkabrown
nkabrown / index.html
Last active March 15, 2016 19:42
d3 date icons
<!DOCTYPE html>
<meta charset="utf-8">
<title>D3 date</title>
<link href='https://fonts.googleapis.com/css?family=Fira+Sans' rel='stylesheet' type='text/css'>
<style>
.date-row {
margin-left: 100px;
}
.date-area {
@nkabrown
nkabrown / dedupe.js
Created March 22, 2016 15:14
dedupe arrays in javascript
// deduplicate array of primitives
let deduped = arr.filter((el, i, arr) => arr.indexOf(el) === i);
// or
deduped = Array.from(new Set(arr));
// deduplicate array of ojects using a hash table
function dedupe(arr) {
let hashTable = {};
return arr.filter(function(el) {