Built with blockbuilder.org
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;
}
.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
}
</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([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 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 + ")");
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
d3.csv('iris.csv', function(error, data){
var dataset=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();
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');
var sum_x = 0;
var sum_y = 0;
var sum_xy = 0;
var sum_xx = 0;
var count = 0;
dataset.forEach(function(d) {
xn = d.sepal_length;
yn = d.sepal_width;
if (d.species=="setosa"){
svg.append("rect")
.attr("class", "dot")
.attr("x", x(d.sepal_length))
.attr("y", y(d.sepal_width))
.attr("height", r(d.sepal_length))
.attr("width", r(d.sepal_length))
.style("fill", color(d.species))
.on("mouseover", function(d){
tooltip.transition()
.duration(0)
.style("opacity", .9);
tooltip.html("Specie: setosa")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d){
tooltip.transition()
.duration(500)
.style("opacity",0);
});
sum_x += xn;
sum_y += yn;
sum_xx += xn*xn;
sum_xy += xn*yn;
count += 1;}
})
/*
* Calculate m and b for the formular:
* y = x * m + b
*/
var m_setosa = (count*sum_xy - sum_x*sum_y) / (count*sum_xx - sum_x*sum_x);
var b_setosa = (sum_y/count) - (m_setosa*sum_x)/count;
svg.append('line')
.attr('x1', x(4))
.attr('y1', y(4*m_setosa+b_setosa))
.attr('x2', x(9))
.attr('y2', y(9*m_setosa+b_setosa))
.attr('stroke', color("setosa"))
.attr('stroke-width','3')
.on("mouseover", function(d){
tooltip.transition()
.duration(0)
.style("opacity", .9);
tooltip.html("regression : "+m_setosa+" * sepal_length + "+b_setosa)
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d){
tooltip.transition()
.duration(500)
.style("opacity",0);
});
var sum_x = 0;
var sum_y = 0;
var sum_xy = 0;
var sum_xx = 0;
var count = 0;
dataset.forEach(function(d) {
xn = d.sepal_length;
yn = d.sepal_width;
rn = r(d.petal_length);
x1 = x(xn);
y1 = y(yn)-rn;
x2 = x(xn)+1.414*rn;
y2 = y(yn)+1.414*rn;
x3 = x(xn)-1.414*rn;
y3 = y(yn)+1.414*rn
if (d.species=="versicolor"){
svg.append("polygon")
.attr("class", "dot")
.attr("points",x1+" "+y1+" "+x2+" "+y2+" "+x3+" "+y3)
.style("fill", color(d.species))
.on("mouseover", function(d){
tooltip.transition()
.duration(0)
.style("opacity", .9);
tooltip.html("Specie: versicolor")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d){
tooltip.transition()
.duration(500)
.style("opacity",0);
});
sum_x += xn;
sum_y += yn;
sum_xx += xn*xn;
sum_xy += xn*yn;
count += 1;}
})
/*
* Calculate m and b for the formular:
* y = x * m + b
*/
var m_versicolor = (count*sum_xy - sum_x*sum_y) / (count*sum_xx - sum_x*sum_x);
var b_versicolor = (sum_y/count) - (m_versicolor*sum_x)/count;
svg.append('line')
.attr('x1', x(4))
.attr('y1', y(4*m_versicolor+b_versicolor))
.attr('x2', x(9))
.attr('y2', y(9*m_versicolor+b_versicolor))
.attr('stroke', color("versicolor"))
.attr('stroke-width','3')
.on("mouseover", function(d){
tooltip.transition()
.duration(0)
.style("opacity", .9);
tooltip.html("regression : "+m_versicolor+" * sepal_length + "+b_versicolor)
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d){
tooltip.transition()
.duration(500)
.style("opacity",0);
});
var sum_x = 0;
var sum_y = 0;
var sum_xy = 0;
var sum_xx = 0;
var count = 0;
dataset.forEach(function(d) {
xn = d.sepal_length;
yn = d.sepal_width;
if (d.species=="virginica"){
svg.append("circle")
.attr("class", "dot")
.attr("r", r(d.petal_length))
.attr("cx", x(d.sepal_length))
.attr("cy", y(d.sepal_width))
.style("fill", color(d.species))
.on("mouseover", function(d){
tooltip.transition()
.duration(0)
.style("opacity", .9);
tooltip.html("Specie: virginica")
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d){
tooltip.transition()
.duration(500)
.style("opacity",0);
});
sum_x += xn;
sum_y += yn;
sum_xx += xn*xn;
sum_xy += xn*yn;
count += 1;}
})
/*
* Calculate m and b for the formular:
* y = x * m + b
*/
var m_virginica = (count*sum_xy - sum_x*sum_y) / (count*sum_xx - sum_x*sum_x);
var b_virginica = (sum_y/count) - (m_virginica*sum_x)/count;
svg.append('line')
.attr('x1', x(4))
.attr('y1', y(4*m_virginica+b_virginica))
.attr('x2', x(9))
.attr('y2', y(9*m_virginica+b_virginica))
.attr('stroke', color("virginica"))
.attr('stroke-width','3')
.on("mouseover", function(d){
tooltip.transition()
.duration(0)
.style("opacity", .9);
tooltip.html("regression : "+m_virginica+" * sepal_length + "+b_virginica)
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d){
tooltip.transition()
.duration(500)
.style("opacity",0);
});
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; })
legend.append("")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color)
.on("mouseover", function(d){
if (d!="setosa"){
svg.selectAll("rect.dot")
.style('opacity',0)}
if (d!="versicolor"){
svg.selectAll("polygon")
.style('opacity',0)}
if (d!="virginica"){
svg.selectAll("circle")
.style('opacity',0)}})
.on("mouseout", function(d){
if (d!="setosa"){
svg.selectAll("rect")
.style('opacity',100)}
if (d!="versicolor"){
svg.selectAll("polygon")
.style('opacity',100)}
if (d!="virginica"){
svg.selectAll("circle")
.style('opacity',100)}
});
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
Modified http://d3js.org/d3.v4.min.js to a secure url
https://d3js.org/d3.v4.min.js