Built with blockbuilder.org
xxxxxxxxxx
<!--
based on
https://bl.ocks.org/mbostock/3887118
https://bl.ocks.org/starcalibre/f4b8bb0da3b2090c56d79646a338fd81
-->
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
.grid .tick {
stroke: lightgrey;
opacity: 0.7;
}
.grid path {
stroke-width: 0;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/d3-path.v0.1.min.js"></script>
<script src="//d3js.org/d3-shape.v0.6.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.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var shape = d3.scale.ordinal()
.range(["cross", "circle", "triangle-up"]);
// Axes du graphique
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
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 + ")");
// Donnees
d3.csv("iris.csv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.sepal_length = +d.sepal_length;
d.sepal_width = +d.sepal_width;
});
x.domain(d3.extent(data, function(d) { return d.sepal_width; })).nice();
y.domain(d3.extent(data, function(d) { return d.sepal_length; })).nice();
// Légende des axes
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Sepal Width (cm)");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Sepal Length (cm)")
// Creation des points
svg.selectAll(".dot")
.data(data)
.enter().append("path")
.attr("class", "dot")
.attr("d", function(d) {
return d3.svg.symbol().type(shape(d.species))();
})
.attr("transform", function(d) {
return "translate(" + x(d.sepal_width) + "," + y(d.sepal_length) +")";
})
.style("fill", function(d) { return color(d.species); });
// ---------------------------------------
// LEGENDE
// ---------------------------------------
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("path")
.attr("d", function(d) {
return d3.svg.symbol().type(shape(d))();
})
.attr("transform", function(d) {
return "translate(" + (width -10) + "," + 10 + ")";
})
.style("fill", function(d) { return color(d); });
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
// ---------------------------------------
// MAILLAGE
// ---------------------------------------
function make_x_axis() {
return d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(10)
}
function make_y_axis() {
return d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10)
}
svg.append("g")
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_axis()
.tickSize(-height, 0, 0)
.tickFormat("")
)
svg.append("g")
.attr("class", "grid")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat("")
)
// ---------------------------------------
// REGRESSION
// ---------------------------------------
});
</script>
https://d3js.org/d3.v3.min.js
https://d3js.org/d3-path.v0.1.min.js
https://d3js.org/d3-shape.v0.6.min.js