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.v4.min.js"></script>
<style>
.line {
fill: none;
stroke: black;
stroke-width: 2.0px;
}
</style>
</head>
<body>
<script>
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
var width = 800;
var height = 400;
// See formats https://bl.ocs.org/zanarmstrong/raw/05c1e95bf7aa16c4768e/
// Data sample {"name": "G", "value": 10, "date": "2016-07"}
var parseDate = d3.timeParse("%Y-%m");
var displayDate = d3.timeFormat("%b %y");
var displayValue = d3.format(",.0f");
// Ordinal scale
var x = d3.scaleTime()
.range([0, width]);
// Linear scale
var y = d3.scaleLinear()
.range([height, height - 200]);
var line = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); });
var g = svg.append("g")
.attr("transform", "translate(50, 0)")
d3.json("dataset.json", function(error, data) {
if (error) throw error;
// Pre-processing
data.forEach(function(d) {
d.value = +d.value;
d["date"] = parseDate(d["date"]);
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
svg.selectAll(".label").data(data).enter()
.append("text")
.attr("class", "label")
.text(function(d, i) { return displayDate(d.date); })
.attr("y", 420)
.attr("x", function(d) { return x(d.date); })
.style("font-size", 10)
.style("font-family", "monospace");
// Display last value
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 + 10)
.style("font-size", 20)
.style("font-family", "monospace");
g.selectAll(".line").data([data]).enter().append("path")
.attr("class", "line")
.attr("d", line);
//---Code modifié---//
//------------------//
//Création des lignes
g.selectAll("line").data(data).enter()
.append("line")
.attr("class", "ligne")
// set la couleur du trait
.style("stroke", "black")
.attr("opacity", "0.0")
// pointillés
.style("stroke-dasharray", ("3, 3"))
.attr("x1", function(d) {return x(d.date);})
.attr("y1", function(d) {return y(d.value);})
.attr("x2",function(d) {return x(d.date);} )
.attr("y2",400 )
//Ecriture de toutes les valeurs avec opacité à 0
g.selectAll(".valeur").data(data).enter()
.append("text")
.text(function(d, i) { return displayValue(d.value); })
.attr("class", "valeur")
.attr("x", function(d){return x(d.date) - 5})
.attr("y", function(d){return y(d.value) - 20})
.attr("fill", "black")
.attr("opacity", "0.0")
.style("font-size", 20)
.style("font-family", "monospace");
//Gestion couleur en fonction du passage de la souris
g.selectAll("circle").data(data).enter().append("circle")
.attr("cx", function(d){return x(d.date)})
.attr("cy", function(d){return y(d.value)})
//Attributs par défaut du cercle
.attr("r", 10)
.attr("fill", "grey")
.attr("opacity", "0.5")
//Quand la souris se positionne, le cercle devient rouge
.on("mouseover", function(d, i){
d3.select(this).style("fill", "red");
d3.select(this).transition().duration(300).style("opacity", 1);
//Apparition des valeurs quand le curseur se positionne
d3.selectAll(".valeur").filter(function(e, j) {
return i === j;
})
.attr("opacity", "1.0");
//Apparition de la ligne quand le curseur se positionne
d3.selectAll(".ligne").filter(function(e) {
return d === e;
}).attr("opacity", "1.0");
})
//Quand la souris n'est plus dessus, le cercle redevient gris
.on("mouseout", function(d, i){
d3.select(this).style("fill", "grey");
d3.select(this).transition().duration(300).style("opacity", 0.5);
//Disparition de la valeur quand le curseur bouge
d3.selectAll(".valeur").filter(function(e, j) {
return i === j;
})
.attr("opacity", "0.0");
//Disparition de la ligne quand le curseur bouge
d3.selectAll(".ligne").filter(function(e) {
return d === e;
}).attr("opacity", "0.0");
})
//Deux méthodes différentes ; jeu avec l'opacité pour les valeurs et jeu avec les couleurs pour les lignes
//J'ai volontairement ajouté des données
});
</script>
</body>
https://d3js.org/d3.v4.min.js