Last active
June 25, 2019 12:12
-
-
Save andy-esch/a4e778e6a8fc08a76afa5e0de63ee7a5 to your computer and use it in GitHub Desktop.
moran's i scatter plot
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> | |
<html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.dot { | |
stroke: #000; | |
} | |
circle { | |
fill-opacity: .7; | |
} | |
circle.hidden { | |
fill: #ccc !important; | |
} | |
.extent { | |
fill: #000; | |
fill-opacity: .125; | |
stroke: #fff; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v4.min.js"></script> | |
<!-- <script src="https://cartodb-libs.global.ssl.fastly.net/carto.js/v4.0.0-beta/carto.min.js"></script> --> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 640 - margin.left - margin.right, | |
height = 640 - margin.top - margin.bottom; | |
// data scaler (takes data to range 0,width) | |
var x = d3.scaleLinear() | |
.range([0, width]); | |
// data scaler (takes data to range height,0) | |
var y = d3.scaleLinear() | |
.range([height, 0]); | |
// colors | |
var color = d3.scaleOrdinal(d3.schemeCategory10); | |
var xAxis = d3.axisBottom() | |
.scale(x); | |
var yAxis = d3.axisLeft() | |
.scale(y); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
d3.csv("http://eschbacher.carto.com/api/v2/sql?q=select+cartodb_id,quads,orig_val_std+as+val,spatial_lag_std+as+spatial_lag,significance+from+moran_i_output_2+order+by+random()+limit+all&format=csv", (error, data) => { | |
if (error) throw error; | |
// convert to numbers | |
data.forEach(function(d) { | |
d.spatial_lag = +d.spatial_lag; | |
d.val = +d.val; | |
}); | |
// set domain centered on zero, symmetric on max values | |
var xmax = 1.1 * d3.max(data, (d) => { return d.val }); | |
var ymax = 1.1 * d3.max(data, (d) => { return d.spatial_lag }); | |
x.domain([-xmax, xmax]).nice(); | |
y.domain([-ymax, ymax]).nice(); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height / 2 + ")") | |
.call(xAxis) | |
.append("text") | |
.attr("class", "label") | |
.attr("x", width) | |
.attr("y", -6) | |
.style("text-anchor", "end") | |
.text("Value"); | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + width / 2 + ",0)") | |
.call(yAxis) | |
.append("text") | |
.attr("class", "label") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("Spatial Lag"); | |
// TODO: have a slider change the value of the significance | |
svg.selectAll(".dot") | |
.data(data) | |
.enter().append("circle") | |
.attr("class", "dot") | |
.attr("r", 3.5) | |
.attr("cx", (d) => { return x(d.val); }) | |
.attr("cy", (d) => { return y(d.spatial_lag); }) | |
.style("fill", (d) => { return color(d.quads); }) | |
.style("opacity", opacityFunc); | |
var legend = svg.selectAll(".legend") | |
.data(color.domain()) | |
.enter().append("g") | |
.attr("class", "legend") | |
.attr("transform", (d, i) => { return "translate(0," + i * 20 + ")"; }); | |
legend.append("rect") | |
.attr("x", width - 18) | |
.attr("width", 18) | |
.attr("height", 18) | |
.style("fill", color); | |
legend.append("text") | |
.attr("x", width - 24) | |
.attr("y", 9) | |
.attr("dy", ".35em") | |
.style("text-anchor", "end") | |
.text((d) => { return d; }); | |
// Clear the previously-active brush, if any. | |
// function brushstart(p) { | |
// if (brushCell !== this) { | |
// d3.select(brushCell).call(brush.clear()); | |
// // x.domain(domainByTrait[p.x]); | |
// // y.domain(domainByTrait[p.y]); | |
// brushCell = this; | |
// } | |
// console.log('making brushing', this); | |
// } | |
// Highlight the selected circles. | |
function brushmove() { | |
var s = d3.event.selection, | |
x0 = s[0][0], | |
y0 = s[0][1], | |
dx = s[1][0] - x0, | |
dy = s[1][1] - y0; | |
svg.selectAll('circle') | |
.style("opacity", (d) => { | |
if (x(d.val) >= x0 && x(d.val) <= x0 + dx && y(d.spatial_lag) >= y0 && y(d.spatial_lag) <= y0 + dy) | |
{ | |
// console.log(d.cartodb_id); | |
return 1; | |
} | |
else { | |
return 0.3; | |
} | |
}); | |
} | |
// If the brush is empty, select all circles. | |
function brushend() { | |
if (!d3.event.selection) { | |
svg.selectAll('circle') | |
.transition() | |
.duration(150) | |
.ease(d3.easeLinear) | |
.style("opacity", opacityFunc); | |
} | |
// console.log(d3.event.selection); | |
} | |
// }); | |
var brushCell; | |
var brush = d3.brush() | |
.extent([[0, 0], [width, height]]) | |
// .x(x) | |
// .y(y) | |
// .on("brushstart", brushstart) | |
.on("brush", brushmove) | |
.on("end", brushend); | |
// .on("brushend", brushend); | |
svg.call(brush); | |
}); | |
function opacityFunc(d) { | |
return d.significance <= 0.05 ? 1.0 : 0.5 * d.significance; | |
} | |
// var slider = svg.append("g") | |
// .attr("class", "slider") | |
// .attr("transform", "translate(" + margin.left + "," + 1.2 * height + ")"); | |
// | |
// slider.append("line") | |
// .attr("class", "track") | |
// .attr("x1", x.range()[0]) | |
// .attr("x2", x.range()[1]) | |
// .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); }) | |
// .attr("class", "track-inset") | |
// .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); }) | |
// .attr("class", "track-overlay") | |
// .call(d3.drag() | |
// .on("start.interrupt", function() { slider.interrupt(); }) | |
// .on("start drag", function() { hue(x.invert(d3.event.x)); })); | |
// | |
// slider.insert("g", ".track-overlay") | |
// .attr("class", "ticks") | |
// .attr("transform", "translate(0," + 18 + ")") | |
// .selectAll("text") | |
// .data(x.ticks(10)) | |
// .enter().append("text") | |
// .attr("x", x) | |
// .attr("text-anchor", "middle") | |
// .text(function(d) { return d + "°"; }); | |
// | |
// var handle = slider.insert("circle", ".track-overlay") | |
// .attr("class", "handle") | |
// .attr("r", 9); | |
// | |
// slider.transition() // Gratuitous intro! | |
// .duration(750) | |
// .tween("hue", function() { | |
// var i = d3.interpolate(0, 70); | |
// return function(t) { hue(i(t)); }; | |
// }); | |
// | |
// function hue(h) { | |
// handle.attr("cx", x(h)); | |
// svg.style("background-color", d3.hsl(h, 0.8, 0.8)); | |
// } | |
// d3.slider().on("slide", function(evt, value) { | |
// console.log(evt, value); | |
// d3.select('circle').style("opacity", opacityFunc); | |
// }); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment