Built with blockbuilder.org
forked from romsson's block: Scatterplot Iris (d3.v4)
forked from romsson's block: Coordinated Scatterplot Iris (d3.v4)
forked from romsson's block: Matrix Scatterplot Iris (d3.v4)
forked from romsson's block: Matrix Scatterplot Iris (d3.v4)
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
stroke-width: .5px;
}
.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
}
.chart {
display: inline-block;
height: 300px;
width: 400px;
margin: 30px;
}
.container {
display: grid;
grid-column-gap: 0px;
grid-template-columns: auto auto auto auto;
padding: 10px;
}
</style>
<body>
<div align="center">
Scatterplot Matrix using Iris Dataset
</div>
<div class="container" style="float: left;"></div>
<div class="legend" style="float: left;"></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var margin = {top: 10, right: 10, bottom: 20, left: 20},
width = 400 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var color = d3.scaleOrdinal(d3.schemeCategory20);
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
function scatterplot(el, data, w, h, var_x, var_y, var_r, var_color) {
var x = d3.scaleLinear()
.range([0, w]);
var y = d3.scaleLinear()
.range([h, 0]);
var r = d3.scaleSqrt()
.range([1,3]);
// https://observablehq.com/@d3/axis-ticks
var xAxis = d3.axisBottom()
.ticks(2)
.scale(x);
var yAxis = d3.axisLeft()
.ticks(5)
.scale(y);
x.domain(d3.extent(data, function(d){
return d[var_x];
})).nice();
y.domain(d3.extent(data, function(d){
return d[var_y];
})).nice();
r.domain(d3.extent(data, function(d){
return d[var_r];
})).nice();
var svg = el.append("svg")
.attr("width", w + margin.left + margin.right)
.attr("height", h + margin.bottom + margin.top)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append('g')
.attr('transform', 'translate(0,' + h + ')')
.attr('class', 'x axis')
.call(xAxis);
svg.append('g')
.attr('transform', 'translate(0,0)')
.attr('class', 'y axis')
.call(yAxis);
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", function(d) { return r(d[var_r]); })
.attr("cx", function(d) { return x(d[var_x]); })
.attr("cy", function(d) { return y(d[var_y]); })
.style("fill", function(d) { return color(d[var_color]); })
.on("mouseover", function(d){
// highlight on
d3.selectAll(".dot").filter(function(e) {
return e === d;
})
.style("stroke-width", 10)
})
.on("mouseout", function(d){
d3.selectAll(".dot").filter(function(e) {
return e === d;
})
.style("stroke-width", .5)
})
.on("click", function(d){
// selection on/off
});
}
d3.csv('iris.csv', function(error, data){
data.forEach(function(d){
d.sepal_length = +d.sepal_length;
d.sepal_width = +d.sepal_width;
d.petal_length = +d.petal_length;
d.petal_width = +d.petal_width;
});
var dims = ["sepal_length", "sepal_width", "petal_width", "petal_length"];
dims.forEach(function(d) {
dims.forEach(function(e) {
scatterplot(d3.select(".container").append("div"), data, width/dims.length, height/dims.length, d, e, "petal_length", "species")
});
})
var clicked = null
// color.domain()
var legend = d3.select(".legend").append("svg")
.attr("width", 200)
.attr("height", 400)
.selectAll("g")
.data(color.domain())
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; })
legend.append("rect")
.style("fill", function(d) { return color(d); })
.attr("width", 20)
.attr("height", 20)
.on("click", function(d) {
// Sets the current cateogry opacity
d3.selectAll(".symbol").style("opacity", 1)
if(clicked !== d) {
d3.selectAll(".symbol")
.filter(function(e) {
return e.species !== d
})
.style("opacity", 0.1);
// memorize the clicked category
clicked = d;
} else {
// reset
clicked = null;
}
});
legend.append("text")
// .attr("x", width - 24)
.attr("y", 9)
.attr("dx", 20)
.attr("dy", ".35em")
// .style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
https://d3js.org/d3.v4.min.js