Built with blockbuilder.org
forked from romsson's block: Scatterplot Iris using different marks
forked from anonymous's block: Scatterplot Iris using different marks
forked from romsson's block: [introd3] scatterplot using iris dataset
forked from romsson's block: [introd3] scatterplot using iris dataset (template)
forked from romsson's block: [introd3] scatterplot using iris dataset (template)
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="https://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([0,height])
var r = d3.scaleSqrt()
.range([5,10])
var xAxis = d3.axisBottom()
.scale(x);
var yAxis = d3.axisLeft()
.scale(y);
var color = d3.scaleOrdinal(d3.schemeCategory10)
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){
// pre-processing
d.sepal_length = +d.sepal_length;
d.sepal_width = +d.sepal_width;
d.petal_length = +d.petal_length;
d.petal_width = +d.petal_width;
});
// update scales
x.domain(d3.extent(data,function(d) {
return d.sepal_length;
}));
y.domain(d3.extent(data,function(d) {
return d.sepal_width;
}));
r.domain(d3.extent(data,function(d) {
return d.petal_length;
}));
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')
.text ("sepal_length")
.attr("x", function() {return width;})
.attr("y", function() {return height - 10;})
.style("text-anchor", "end")
svg.selectAll("circle").data(data).enter()
.append("circle")
.attr("class", "dot")
.attr("r", function(d) {return r(d.petal_length)})
.style("fill", function(d) {return color(d.species)})
.attr("transform", function(d) {
return "translate(" + x(d.sepal_length) + "," + y(d.sepal_width) + ")";
})
.on("click", function(){
d3.selectAll("circles")
.style("stroke-width",0)
d3.select(this)
.style("stroke-width",3)
.style("stroke", "red")
})
.on("mouseenter", function(d){
svg
.append("text")
.style("font-size", "36px")
.attr("class","label")
.attr("transform", function() {
return "translate(" + x(d.sepal_length) + "," + y(d.sepal_width) + ")";
})
.text("(" + d.sepal_length + "," + d.sepal_width + ")")
})
.on("mouseleave", function(){
d3.selectAll(".label").remove()
})
// create legend
var legend = svg.selectAll(".legend")
// .data( ...
// legend.append("rect")
// ...
// legend.append("text")
// ...
});
</script>
Modified http://d3js.org/d3.v4.min.js to a secure url
https://d3js.org/d3.v4.min.js