Built with blockbuilder.org
Last active
May 4, 2019 23:27
-
-
Save jwilber/24c19a72462def691f0c18253ca78a5a to your computer and use it in GitHub Desktop.
reusable joyplot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="joyplot.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
.point { | |
fill: #000; | |
stroke: transparent; c | |
stroke-width: 5px; | |
} | |
.line { | |
fill: none; | |
stroke: #aaa; | |
} | |
.axis { | |
.domain, | |
.tick line { | |
display: none; | |
} | |
} | |
</style> | |
</head> | |
<body> | |
<!-- here's the div our chart will be injected into --> | |
<div class="chart-container" style="max-width: 1000px;"></div> | |
<script> | |
const chart = new Chart({ | |
element: document.querySelector('.chart-container'), | |
data: "iris.csv", | |
x: 'day', | |
y: 'retweets' | |
}); | |
</script> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
5.1 | 3.5 | 1.4 | 0.2 | setosa | |
4.9 | 3 | 1.4 | 0.2 | setosa | |
4.7 | 3.2 | 1.3 | 0.2 | setosa | |
4.6 | 3.1 | 1.5 | 0.2 | setosa | |
5 | 3.6 | 1.4 | 0.2 | setosa | |
5.4 | 3.9 | 1.7 | 0.4 | setosa | |
4.6 | 3.4 | 1.4 | 0.3 | setosa | |
5 | 3.4 | 1.5 | 0.2 | setosa | |
4.4 | 2.9 | 1.4 | 0.2 | setosa | |
4.9 | 3.1 | 1.5 | 0.1 | setosa | |
5.4 | 3.7 | 1.5 | 0.2 | setosa | |
4.8 | 3.4 | 1.6 | 0.2 | setosa | |
4.8 | 3 | 1.4 | 0.1 | setosa | |
4.3 | 3 | 1.1 | 0.1 | setosa | |
5.8 | 4 | 1.2 | 0.2 | setosa | |
5.7 | 4.4 | 1.5 | 0.4 | setosa | |
5.4 | 3.9 | 1.3 | 0.4 | setosa | |
5.1 | 3.5 | 1.4 | 0.3 | setosa | |
5.7 | 3.8 | 1.7 | 0.3 | setosa | |
5.1 | 3.8 | 1.5 | 0.3 | setosa | |
5.4 | 3.4 | 1.7 | 0.2 | setosa | |
5.1 | 3.7 | 1.5 | 0.4 | setosa | |
4.6 | 3.6 | 1 | 0.2 | setosa | |
5.1 | 3.3 | 1.7 | 0.5 | setosa | |
4.8 | 3.4 | 1.9 | 0.2 | setosa | |
5 | 3 | 1.6 | 0.2 | setosa | |
5 | 3.4 | 1.6 | 0.4 | setosa | |
5.2 | 3.5 | 1.5 | 0.2 | setosa | |
5.2 | 3.4 | 1.4 | 0.2 | setosa | |
4.7 | 3.2 | 1.6 | 0.2 | setosa | |
4.8 | 3.1 | 1.6 | 0.2 | setosa | |
5.4 | 3.4 | 1.5 | 0.4 | setosa | |
5.2 | 4.1 | 1.5 | 0.1 | setosa | |
5.5 | 4.2 | 1.4 | 0.2 | setosa | |
4.9 | 3.1 | 1.5 | 0.1 | setosa | |
5 | 3.2 | 1.2 | 0.2 | setosa | |
5.5 | 3.5 | 1.3 | 0.2 | setosa | |
4.9 | 3.1 | 1.5 | 0.1 | setosa | |
4.4 | 3 | 1.3 | 0.2 | setosa | |
5.1 | 3.4 | 1.5 | 0.2 | setosa | |
5 | 3.5 | 1.3 | 0.3 | setosa | |
4.5 | 2.3 | 1.3 | 0.3 | setosa | |
4.4 | 3.2 | 1.3 | 0.2 | setosa | |
5 | 3.5 | 1.6 | 0.6 | setosa | |
5.1 | 3.8 | 1.9 | 0.4 | setosa | |
4.8 | 3 | 1.4 | 0.3 | setosa | |
5.1 | 3.8 | 1.6 | 0.2 | setosa | |
4.6 | 3.2 | 1.4 | 0.2 | setosa | |
5.3 | 3.7 | 1.5 | 0.2 | setosa | |
5 | 3.3 | 1.4 | 0.2 | setosa | |
7 | 3.2 | 4.7 | 1.4 | versicolor | |
6.4 | 3.2 | 4.5 | 1.5 | versicolor | |
6.9 | 3.1 | 4.9 | 1.5 | versicolor | |
5.5 | 2.3 | 4 | 1.3 | versicolor | |
6.5 | 2.8 | 4.6 | 1.5 | versicolor | |
5.7 | 2.8 | 4.5 | 1.3 | versicolor | |
6.3 | 3.3 | 4.7 | 1.6 | versicolor | |
4.9 | 2.4 | 3.3 | 1 | versicolor | |
6.6 | 2.9 | 4.6 | 1.3 | versicolor | |
5.2 | 2.7 | 3.9 | 1.4 | versicolor | |
5 | 2 | 3.5 | 1 | versicolor | |
5.9 | 3 | 4.2 | 1.5 | versicolor | |
6 | 2.2 | 4 | 1 | versicolor | |
6.1 | 2.9 | 4.7 | 1.4 | versicolor | |
5.6 | 2.9 | 3.6 | 1.3 | versicolor | |
6.7 | 3.1 | 4.4 | 1.4 | versicolor | |
5.6 | 3 | 4.5 | 1.5 | versicolor | |
5.8 | 2.7 | 4.1 | 1 | versicolor | |
6.2 | 2.2 | 4.5 | 1.5 | versicolor | |
5.6 | 2.5 | 3.9 | 1.1 | versicolor | |
5.9 | 3.2 | 4.8 | 1.8 | versicolor | |
6.1 | 2.8 | 4 | 1.3 | versicolor | |
6.3 | 2.5 | 4.9 | 1.5 | versicolor | |
6.1 | 2.8 | 4.7 | 1.2 | versicolor | |
6.4 | 2.9 | 4.3 | 1.3 | versicolor | |
6.6 | 3 | 4.4 | 1.4 | versicolor | |
6.8 | 2.8 | 4.8 | 1.4 | versicolor | |
6.7 | 3 | 5 | 1.7 | versicolor | |
6 | 2.9 | 4.5 | 1.5 | versicolor | |
5.7 | 2.6 | 3.5 | 1 | versicolor | |
5.5 | 2.4 | 3.8 | 1.1 | versicolor | |
5.5 | 2.4 | 3.7 | 1 | versicolor | |
5.8 | 2.7 | 3.9 | 1.2 | versicolor | |
6 | 2.7 | 5.1 | 1.6 | versicolor | |
5.4 | 3 | 4.5 | 1.5 | versicolor | |
6 | 3.4 | 4.5 | 1.6 | versicolor | |
6.7 | 3.1 | 4.7 | 1.5 | versicolor | |
6.3 | 2.3 | 4.4 | 1.3 | versicolor | |
5.6 | 3 | 4.1 | 1.3 | versicolor | |
5.5 | 2.5 | 4 | 1.3 | versicolor | |
5.5 | 2.6 | 4.4 | 1.2 | versicolor | |
6.1 | 3 | 4.6 | 1.4 | versicolor | |
5.8 | 2.6 | 4 | 1.2 | versicolor | |
5 | 2.3 | 3.3 | 1 | versicolor | |
5.6 | 2.7 | 4.2 | 1.3 | versicolor | |
5.7 | 3 | 4.2 | 1.2 | versicolor | |
5.7 | 2.9 | 4.2 | 1.3 | versicolor | |
6.2 | 2.9 | 4.3 | 1.3 | versicolor | |
5.1 | 2.5 | 3 | 1.1 | versicolor | |
5.7 | 2.8 | 4.1 | 1.3 | versicolor | |
6.3 | 3.3 | 6 | 2.5 | virginica | |
5.8 | 2.7 | 5.1 | 1.9 | virginica | |
7.1 | 3 | 5.9 | 2.1 | virginica | |
6.3 | 2.9 | 5.6 | 1.8 | virginica | |
6.5 | 3 | 5.8 | 2.2 | virginica | |
7.6 | 3 | 6.6 | 2.1 | virginica | |
4.9 | 2.5 | 4.5 | 1.7 | virginica | |
7.3 | 2.9 | 6.3 | 1.8 | virginica | |
6.7 | 2.5 | 5.8 | 1.8 | virginica | |
7.2 | 3.6 | 6.1 | 2.5 | virginica | |
6.5 | 3.2 | 5.1 | 2 | virginica | |
6.4 | 2.7 | 5.3 | 1.9 | virginica | |
6.8 | 3 | 5.5 | 2.1 | virginica | |
5.7 | 2.5 | 5 | 2 | virginica | |
5.8 | 2.8 | 5.1 | 2.4 | virginica | |
6.4 | 3.2 | 5.3 | 2.3 | virginica | |
6.5 | 3 | 5.5 | 1.8 | virginica | |
7.7 | 3.8 | 6.7 | 2.2 | virginica | |
7.7 | 2.6 | 6.9 | 2.3 | virginica | |
6 | 2.2 | 5 | 1.5 | virginica | |
6.9 | 3.2 | 5.7 | 2.3 | virginica | |
5.6 | 2.8 | 4.9 | 2 | virginica | |
7.7 | 2.8 | 6.7 | 2 | virginica | |
6.3 | 2.7 | 4.9 | 1.8 | virginica | |
6.7 | 3.3 | 5.7 | 2.1 | virginica | |
7.2 | 3.2 | 6 | 1.8 | virginica | |
6.2 | 2.8 | 4.8 | 1.8 | virginica | |
6.1 | 3 | 4.9 | 1.8 | virginica | |
6.4 | 2.8 | 5.6 | 2.1 | virginica | |
7.2 | 3 | 5.8 | 1.6 | virginica | |
7.4 | 2.8 | 6.1 | 1.9 | virginica | |
7.9 | 3.8 | 6.4 | 2 | virginica | |
6.4 | 2.8 | 5.6 | 2.2 | virginica | |
6.3 | 2.8 | 5.1 | 1.5 | virginica | |
6.1 | 2.6 | 5.6 | 1.4 | virginica | |
7.7 | 3 | 6.1 | 2.3 | virginica | |
6.3 | 3.4 | 5.6 | 2.4 | virginica | |
6.4 | 3.1 | 5.5 | 1.8 | virginica | |
6 | 3 | 4.8 | 1.8 | virginica | |
6.9 | 3.1 | 5.4 | 2.1 | virginica | |
6.7 | 3.1 | 5.6 | 2.4 | virginica | |
6.9 | 3.1 | 5.1 | 2.3 | virginica | |
5.8 | 2.7 | 5.1 | 1.9 | virginica | |
6.8 | 3.2 | 5.9 | 2.3 | virginica | |
6.7 | 3.3 | 5.7 | 2.5 | virginica | |
6.7 | 3 | 5.2 | 2.3 | virginica | |
6.3 | 2.5 | 5 | 1.9 | virginica | |
6.5 | 3 | 5.2 | 2 | virginica | |
6.2 | 3.4 | 5.4 | 2.3 | virginica | |
5.9 | 3 | 5.1 | 1.8 | virginica |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Chart { | |
constructor(opts) { | |
console.log('inside') | |
this.element = opts.element; | |
this.x = opts.x; | |
this.y = opts.y; | |
d3.csv(opts.data, (d) => { | |
this.data = d; | |
this.draw(); | |
}); | |
} | |
draw() { | |
this.margin = { | |
top: 200, | |
bottom: 50, | |
right: 50, | |
left: 100 | |
}; | |
this.width = 800 - this.margin.right - this.margin.left; | |
this.height = this.width / 1.5 - this.margin.top - this.margin.bottom; | |
this.element.innerHTML = ''; | |
const svg = d3.select(this.element).append('svg'); | |
svg.attr('width', this.width + this.margin.right + this.margin.left); | |
svg.attr('height', this.height + this.margin.top + this.margin.bottom); | |
this.plot = svg.append('g') | |
.attr('transform', | |
`translate(${this.margin.left}, ${this.margin.top})`) | |
this.createScales(); | |
this.addAxes(); | |
this.addDensities(); | |
} | |
createScales() { | |
this.categories = this.data.columns.filter(d => !isNaN(this.data[0][d])) | |
this.n = this.categories.length | |
const {min, max} = csvExtent(this.data) | |
this.x = d3.scaleLinear() | |
.domain([min - 15, max + 15]) | |
.range([ 0, this.width ]); | |
// Create a Y scale for densities | |
this.y = d3.scaleLinear() | |
.domain([0, .25]) | |
.range([ this.height, 0]); | |
// Create the Y axis for names | |
this.yName = d3.scaleBand() | |
.domain(this.categories) | |
.range([0, this.height]) | |
.paddingInner(1) | |
console.log(this.yName) | |
}; | |
addAxes() { | |
this.plot.append("g") | |
.attr('class', 'x-axis') | |
.attr("transform", `translate(0, ${this.height})`) | |
.call(d3.axisBottom(this.x)); | |
this.plot.append("g") | |
.attr('class', 'y-axis') | |
.call(d3.axisLeft(this.yName)); | |
}; | |
addDensities() { | |
console.log('r', this.yName) | |
const that = this; | |
// Compute kernel density estimation for each column: | |
var kde = kernelDensityEstimator(kernelEpanechnikov(7), this.x.ticks(40)) | |
// increase this 40 for more accurate density. | |
var allDensity = [] | |
for (var i = 0; i < this.n; i++) { | |
let key = this.categories[i] | |
let density = kde( this.data.map(function(d){ return d[key]; }) ) | |
allDensity.push({key: key, density: density}) | |
} | |
// Add areas | |
this.plot.selectAll("areas") | |
.data(allDensity) | |
.enter() | |
.append("path") | |
// .attr('transform', | |
// d => { | |
// `translate(0, ${(that.yName(d.key)-that.height)})` | |
// }) | |
.attr("transform", function(d){ | |
return("translate(0," + (that.yName(d.key)-that.height) +")" ) | |
}) | |
.datum(d => d.density) | |
.attr("fill", "skyblue") | |
.attr('opacity', 1) | |
.attr("stroke", "azure") | |
.attr("stroke-width", 1) | |
.attr("d", d3.line() | |
.curve(d3.curveStep) | |
.x(function(d) { return that.x(d[0]); }) | |
.y(function(d) { return that.y(d[1]); }) | |
) | |
}; | |
} | |
function kernelDensityEstimator(kernel, X) { | |
return function(V) { | |
return X.map(function(x) { | |
return [x, d3.mean(V, function(v) { return kernel(x - v); })]; | |
}); | |
}; | |
} | |
function kernelEpanechnikov(k) { | |
return function(v) { | |
return Math.abs(v /= k) <= 1 ? 0.75 * (1 - v * v) / k : 0; | |
}; | |
} | |
function csvExtent(data) { | |
const colNames = data.columns | |
const extents = colNames.map(col => d3.extent(data, d => +d[col])); | |
const csvMin = d3.min(extents, d => d[0]) | |
const csvMax = d3.max(extents, d => d[1]) | |
return {'min': csvMin, 'max': csvMax} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment