|
<!DOCTYPE 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; |
|
} |
|
|
|
</style> |
|
<body> |
|
<script src="http://d3js.org/d3.v4.min.js"></script> |
|
<script> |
|
|
|
var margin = {top: 20, right: 20, bottom: 30, left: 40}, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom; |
|
|
|
var x = d3.scaleLinear() |
|
.range([0, width]); |
|
|
|
var y = d3.scaleLinear() |
|
.range([height, 0]); |
|
|
|
var r = d3.scaleSqrt() |
|
.range([2,10]); |
|
|
|
var xAxis = d3.axisBottom() |
|
.scale(x); |
|
|
|
var yAxis = d3.axisLeft() |
|
.scale(y); |
|
|
|
var color = d3.scaleOrdinal(d3.schemeCategory20); |
|
|
|
var symbols = d3.scaleOrdinal(d3.symbols); |
|
|
|
// creates a generator for symbols |
|
var symbol = d3.symbol().size(100); |
|
|
|
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('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; |
|
}); |
|
|
|
x.domain(d3.extent(data, function(d){ |
|
return d.sepal_length; |
|
})).nice(); |
|
|
|
y.domain(d3.extent(data, function(d){ |
|
return d.sepal_width; |
|
})).nice(); |
|
|
|
r.domain(d3.extent(data, function(d){ |
|
return d.petal_length; |
|
})).nice(); |
|
|
|
// Axis |
|
svg.append('g') |
|
.attr('transform', 'translate(0,' + height + ')') |
|
.attr('class', 'x axis') |
|
.call(xAxis); |
|
|
|
svg.append('g') |
|
.attr('transform', 'translate(0,0)') |
|
.attr('class', 'y axis') |
|
.call(yAxis); |
|
|
|
svg.append('text') |
|
.attr('x', 10) |
|
.attr('y', 10) |
|
.attr('class', 'label') |
|
.text('Sepal Width'); |
|
|
|
svg.append('text') |
|
.attr('x', width) |
|
.attr('y', height - 10) |
|
.attr('text-anchor', 'end') |
|
.attr('class', 'label') |
|
.text('Sepal Length'); |
|
|
|
// Use the ordinal scale symbols to generate symbols |
|
// such as d3.symbolCross, etc.. |
|
// -> symbol.type(d3.symbolCross)() |
|
svg.selectAll("path") |
|
.data(data) |
|
.enter().append("path") |
|
.attr("class", "symbol") |
|
.attr("d", function(d, i) { return symbol.type(symbols(d.species))(); }) |
|
.style("fill", function(d) { return color(d.species); }) |
|
.attr("transform", function(d) { |
|
return "translate(" + x(d.sepal_length) + "," + y(d.sepal_width) +")"; |
|
}); |
|
|
|
// color.domain() |
|
var legend = svg.selectAll(".legend") |
|
.data(color.domain()) |
|
.enter().append("g") |
|
.attr("class", "legend") |
|
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); |
|
|
|
var clicked = null |
|
|
|
legend.append("path") |
|
.style("fill", function(d) { return color(d); }) |
|
.attr("d", function(d, i) { return symbol.type(symbols(d))(); }) |
|
.attr("transform", function(d, i) { |
|
return "translate(" + (width -10) + "," + 10 + ")"; |
|
}) |
|
.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("dy", ".35em") |
|
.style("text-anchor", "end") |
|
.text(function(d) { return d; }); |
|
|
|
}); |
|
|
|
</script> |