Skip to content

Instantly share code, notes, and snippets.

@riff4
Created January 20, 2018 14:20
Show Gist options
  • Save riff4/b5b2f875c490a95833d0bba887f5eba7 to your computer and use it in GitHub Desktop.
Save riff4/b5b2f875c490a95833d0bba887f5eba7 to your computer and use it in GitHub Desktop.
Cours 2
license: mit
<!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;
}
.tooltip {
position: absolute;
width: 200px;
height: 28px;
pointer-events: none;
}
</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 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){
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');
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", function(d) { return r(d.petal_length); })
.attr("cx", function(d) { return x(d.sepal_length); })
.attr("cy", function(d) { return y(d.sepal_width); })
.style("fill", function(d) { return color(d.species); })
.on("mouseover", function(d){
tooltip.transition()
.duration(0)
.style("opacity", .9);
tooltip.html("Species:" + d.species)
.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("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(function(d) { return d; });
// get the x and y values for least squares
var xSeries = data.map(function (d) { return parseFloat(d['sepal_length']); })
var ySeries = data.map(function(d) { return parseFloat(d['sepal_width']); });
var leastSquaresCoeff = leastSquares(xSeries, ySeries);
// apply the reults of the least squares regression
var x1 = xSeries[0];
var y1 = leastSquaresCoeff[0] + leastSquaresCoeff[1];
var x2 = xSeries[xSeries.length - 1];
var y2 = leastSquaresCoeff[0] * xSeries.length + leastSquaresCoeff[1];
var trendData = [[x1,y1,x2,y2]];
var trendline = svg.selectAll(".trendline")
.data(trendData);
trendline.enter()
.append("line")
.attr("class", "trendline")
.attr("x1", function(d) { return x(d[0]); })
.attr("y1", function(d) { return y(d[1]); })
.attr("x2", function(d) { return x(d[2]); })
.attr("y2", function(d) { return y(d[3]); })
.attr("stroke", "black")
.attr("stroke-width", 1);
// display equation on the chart
svg.append("text")
.text("eq: " + decimalFormat(leastSquaresCoeff[0]) + "x + " +
decimalFormat(leastSquaresCoeff[1]))
.attr("class", "text-label")
.attr("x", function(d) {return xScale(x2) - 60;})
.attr("y", function(d) {return yScale(y2) - 30;});
// display r-square on the chart
svg.append("text")
.text("r-sq: " + decimalFormat(leastSquaresCoeff[2]))
.attr("class", "text-label")
.attr("x", function(d) {return x(x2) - 60;})
.attr("y", function(d) {return y(y2) - 10;});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment