Hello,
I’m trying to make very basic graph with some user interactivity.
I’ve read whole Learn D3 but as this tutorial is centered around using Observable I quickly hit problems when trying to make something on my own (Observable is nice, but I really do not like working with interactive documents while learning stuff and I much rather work with raw HTML + JS to understand how it actually works).
After some troubles I managed to write this very basic example with line plot:
<!DOCTYPE html>
<html>
<head>
<!-- Load d3.js -->
<script src="d3.min.js"></script>
<script src="d3-fetch@3.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container">
<svg id="graph"></svg>
</div>
</body>
<script src="main.js"></script>
</html>
(async function () {
const data = await d3.dsv(",", "data.csv", (d) => {
return {
date: new Date(d.date),
close: +d.close
};
});
// Select the svg area
const svg = d3.select("#graph");
let margin = ({ top: 20, right: 30, bottom: 30, left: 40 });
let width = 1800;
let height = 900;
let x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.range([margin.left, width - margin.right]);
let y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.close)])
.range([height - margin.bottom, margin.top]);
let xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0));
let yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(height / 40))
.call(g => g.select(".domain").remove());
let line = d3.line().x(d => x(d.date)).y(d => y(d.close));
svg.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-miterlimit", 1)
.attr("d", line(data));
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
})()
It basically recreates graph from the section Interaction from the tutorial, but without tooltips.
Later I saw this interactive example Focus + Context and I would like to implement something similar but I’m not sure how to rewrite this named cells inside raw JS? It should be some kind of function that return… what? svg.node();
?
I don’t what to do next or where to look for explanations.