Published
Edited
Jun 14, 2021
1 fork
1 star
Insert cell
Insert cell
md `Entrer ici les informations et le contexte à propos du graphique`
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height))
.style("-webkit-tap-highlight-color", "transparent")
.style("overflow", "visible");

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
//crée la courbe
svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue") //couleur de la ligne
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);
//ajoute des points sur la ligne
const dots = svg
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.style("fill", "steelblue") //couleur des points
.attr("class", "dot")
.attr("r", 3) //taille des points
.attr("cx", function(d) {
return x(d.d);
})
.attr("cy", function(d) {
return y(d.v);
});

//tout ce qui suit détermine l'interactivité --> suit le déplacement de la souris et affiche l'information contenue au point le plus proche
const tooltip = svg.append("g");

svg.on("touchmove mousemove", function(event) {
const {d, v} = bisect(d3.pointer(event, this)[0]);

tooltip
.attr("transform", `translate(${x(d)},${y(v)})`)
.call(callout, `${v}
${formatDate(d)}`);
});

svg.on("touchend mouseleave", () => tooltip.call(callout, null));

return svg.node();
}
Insert cell
Insert cell
Insert cell
data = donnees.slice(0,25).map(({date, vues}) => ({d: parseDate(date), v: vues}))
Insert cell
donnees= d3.csv("https://gist.githubusercontent.com/lenaMK/8fc8c87c87148266317b018fc56700ea/raw/16e3c69b5c2814244bb8a4c7fd2dc88b5ee7cc45/Fig2.csv")
Insert cell
parseDate= d3.timeParse("%Y-%m-%d")
Insert cell
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.d))
.range([margin.left, width - margin.right])
Insert cell
d3.max(data, d => d.v)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
line = d3.line()
.curve(d3.curveMonotoneY)
.defined(d => !isNaN(d.v))
.x(d => x(d.d))
.y(d => y(d.v))
Insert cell
function formatDate(date) {
return date.toLocaleString("en", {
month: "short",
day: "numeric",
year: "numeric",
timeZone: "UTC"
});
}
Insert cell
bisect = {
const bisect = d3.bisector(d => d.d).left;
return mx => {
const date = x.invert(mx);
const index = bisect(data, date, 1);
const a = data[index - 1];
const b = data[index];
return b && (date - a.date > b.date - date) ? b : a;
};
}
Insert cell
height = 500
Insert cell
margin = ({top: 50, right: 30, bottom: 30, left: 40})
Insert cell
d3 = require("d3@6")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more