TP du 17/11/2016 dans le cadre de l'Unité d'Enseignement Visualisation de données du Master 2 Informatique - Parcours Intelligence Artificielle de l'Université Claude Bernard Lyon 1
Built with blockbuilder.org
forked from romsson's block: simple line chart from dataset
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.line {
fill: none;
stroke: black;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
var width = 800;
var height = 400;
// https://bl.ocks.org/zanarmstrong/raw/05c1e95bf7aa16c4768e/
var parseDate = d3.time.format("%Y-%m");
var displayDate = d3.time.format("%b %y");
var displayValue = d3.format(",.0f");
// Ordinal scale
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .5);
var y = d3.scale.linear()
.range([height, height - 200]);
var line = d3.svg.line()
.x(function(d) { return x(d.name); })
.y(function(d) { return y(d.value); });
var g = svg.append("g")
.attr("transform", "translate(50, 0)")
// Chargement des données
d3.json("dataset.json", function(data) {
// Pre-processing
data.forEach(function(d) {
d.value = +d.value;
d["date"] = parseDate.parse(d["date"]);
});
x.domain(data.map(function(d) { return d.name; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
// Création et affichage de la légende de l'axe des abcisses
svg.selectAll("text").data(data).enter()
.append("text")
.text(function(d, i) { return displayDate(d.date); })
.attr("y", 420)
.attr("x", function(d) { return x(d.name); })
.attr("id", function(d, i) { return "id_"+i; })
.style("font-size", 10)
.style("font-family", "monospace");
// Création et affichage de la dernière valeur du graphe
g.selectAll(".value").data([data[data.length -1]]).enter()
.append("text")
.text(function(d, i) { return displayValue(d.value); })
.attr("class", "value")
.attr("y", function(d) { return y(d.value); })
.attr("x", width - 20)
.style("font-size", 20)
.style("font-family", "monospace");
// Création et affichage du graphe
g.selectAll("path").data([data]).enter().append("path")
.attr("class", "line")
.attr("d", line);
// Création et non-affichage des pointillés de chaque sommet
g.selectAll("dash_array").data(data).enter()
.append("line")
.style("stroke", "black") // colour the line
.attr("x1", function(d) { return x(d.name); })
.attr("y1", function(d) { return y(d.value); })
.attr("x2", function(d) { return x(d.name); })
.attr("y2", function(d) { return y(0); })
.attr("id", function(d, i) { return "id_"+i; })
.attr("class", "dash_array")
.style("stroke-dasharray", ("3, 3"))
.style("display", "none");
// Création et affichage des cercles
g.selectAll("circle").data(data).enter().append("circle")
.attr("cx", function(d) { return x(d.name); })
.attr("cy", function(d) { return y(d.value); })
.attr("r", 10)
.attr("id", function(d, i) { return "id_" + i;})
.attr("fill", "black")
.attr("opacity", 0.2)
// Action lors du passage de la souris
.on("mouseover", function(d, i) {
// Changement de couleur du cercle
d3.select(this).style("fill", "red").attr("opacity", 1.0);
// Affichage du texte du cercle
d3.selectAll(".circle_text").filter(function(id) {
return d === id;
}).style("display", "flex");
// Affichage des pointillés
d3.selectAll(".dash_array").filter(function(id) {
return d === id;
}).style("display", "flex");
})
// Action lors de la sortie de la souris
.on("mouseout", function(d) {
// Changement de couleur du cercle
d3.select(this).style("fill", "black")
.attr("opacity", 0.2);
// Non-affichage du texte du cercle
d3.selectAll(".circle_text").filter(function(id) {
return d === id;
}).style("display", "none");
// Non-affichage des pointillés
d3.selectAll(".dash_array").filter(function(id) {
return d === id;
}).style("display", "none");
});
// Création et non-affichage du texte au niveau des sommets
g.selectAll("circle_text").data(data).enter()
.append("text")
.text(function(d, i) { return d.value; })
.attr("y", function(d) { return y(d.value + 4); })
.attr("x", function(d) { return x(d.name)-10; })
.attr("id", function(d, i) { return "id_"+i; })
.attr("class", "circle_text")
.style("font-size", 20)
.style("font-family", "monospace")
.style("display", "none");
});
</script>
</body>
https://d3js.org/d3.v3.min.js