Built with blockbuilder.org
forked from romsson's block: simple line chart from dataset
By RadeMathis, for the University of Lyon 1 Rendu du TP2 de DataViz
Ce code est en partie (la partie de la cration de l'infobulle) emprunte à cet exemple de Mike Bostock
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
.line {
fill: none;
stroke: black;
stroke-width: 1.5px;
}
.tooltip {
position: absolute;
text-align: center;
font: 15px sans-serif;
pointer-events: none;
}
</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.ocks.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);
/* added code */
g.selectAll("circle").data(data).enter().append("circle")
.attr("cx", function(d) { return x(d.date) ;})
.attr("cy", function(d) { return y(d.value) ;})
.attr("r", 10 )
.attr("fill", "#999999")
.attr("fill-opacity","0.4")
.on("mouseover", function(d,i) {
d3.select(this).style("fill","red")
.style("fill-opacity","1.0");
div
.style("display", "inline")
.text(d.value)
.style("left", x(d.date) + 50 + "px")
.style("top", y(d.value) - 20 + "px");
pointille
.attr("x1", x(d.date))
.attr("y1", y(d.value))
.attr("x2", x(d.date))
.attr("y2", y(0))
.attr("style", "stroke:black;stroke-width:1;stroke-linecap:square;stroke-dasharray:5, 4");
})
.on("mouseout", function() {
d3.select(this).style("fill","#999999")
.style("fill-opacity","0.4");
div.style("display","none");
pointille
.attr("style", "none");
});
});
var div = d3.select("body").append("div")
.style("display", "none")
.attr("class", "tooltip");
var pointille = g.append("line");
</script>
</body>
https://d3js.org/d3.v4.min.js