Published
Edited
Dec 31, 2020
1 fork
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.append("g")
.attr("fill", "steelblue")
.attr("fill-opacity", 0.8)
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.year))
.attr("width", x.bandwidth())
.attr("y", d => y1(d.population))
.attr("height", d => y1(0) - y1(d.population));
//.attr("height", d => y1(d.population) - y1(0));

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

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


return svg.node();
}
Insert cell
chart1 = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])

svg.append("g")
.style("font", "0.8em sans-serif")
.call(xAxis);

svg.append("g")
.style("font", "0.8em sans-serif")
.call(y1Axis);
svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);

const tooltip = svg.append("g");

svg.on("touchmove mousemove", function() {
const {year, population} = bisect(d3.mouse(this)[0]);

tooltip
.attr("transform", `translate(${x(year)},${y1(population)})`)
.call(callout, `${formatValue(d3.format(".2s")(population))}

${(year)}`);
});

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

return svg.node();
}
Insert cell
callout = (g, value) => {
if (!value) return g.style("display", "none");

g
.style("display", null)
.style("pointer-events", "none")
.style("font", "0.8em sans-serif");

const path = g.selectAll("path")
.data([null])
.join("path")
.attr("fill", "white")
.attr("stroke", "black");

const text = g.selectAll("text")
.data([null])
.join("text")
.call(text => text
.selectAll("tspan")
.data((value + "").split(/\n/))
.join("tspan")
.attr("x", 0)
.attr("y", (d, i) => `${i * 1.1}em`)
.style("font-weight", (_, i) => i ? null : "bold")
.text(d => d));

const {x, y, width: w, height: h} = text.node().getBBox();

text.attr("transform", `translate(${-w / 2},${15 - y})`);
path.attr("d", `M${-w / 2 - 10},5H-5l5,-5l5,5H${w / 2 + 10}v${h + 20}h-${w + 20}z`);
}
Insert cell
bisect = {
const bisect = d3.bisector(d => d.year).left;
return mx => {
const date = x.invert(mx);
const index = bisect(data, date, 1);
//const a = data[index - 1];
const a = data[index];
const b = data[index];
return date - a.date > b.date - date ? b : a;
};
}
Insert cell
function formatValue(value) {
return value;
}
Insert cell
data2 = Object.assign(d3.csvParse(await FileAttachment("new-passenger-cars.csv").text(), d3.autoType), {y1: "↑ New cars sold", y2: "Avg. fuel efficiency (mpg) ↑"})
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("Frenchpopulationto2016i - Sheet1 (1).csv").text(), d3.autoType), {y1: "Population in millions", y2: "Avg. fuel efficiency (mpg) ↑"})
Insert cell
data1 = Object.assign(d3.csvParse(await FileAttachment("new-passenger-cars.csv").text(), d3.autoType), {y1: "↑ New cars sold", y2: "Avg. fuel efficiency (mpg) ↑"})
Insert cell
data3 = Object.assign(d3.csvParse(await FileAttachment("Frenchpopulationto2016i - Sheet1 (1).csv").text(), d3.autoType), {y1: "↑ New cars sold", y2: "Avg. fuel efficiency (mpg) ↑"})
Insert cell
line = d3.line()
.x(d => x(d.year) + x.bandwidth() / 2)
.y(d => y1(d.population))
Insert cell
/*line1 = d3.line()
.x(d => x(d.year) + x.bandwidth() / 2)
.y(d => y1(d.sales))*/
Insert cell
x = d3.scaleBand()
.domain(data.map(d => d.year))
.rangeRound([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y1 = d3.scaleLinear()
.domain([0, d3.max(data, d => d.population)])
.rangeRound([height - margin.bottom, margin.top])
Insert cell
/*y2 = d3.scaleLinear()
.domain(d3.extent(data, d => d.efficiency))
.rangeRound([height - margin.bottom, margin.top])*/
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)
//.tickValues(d3.ticks(...d3.extent(x.domain()), width/40).filter(v => x(v) !== undefined))
//.tickSizeOuter(0)
//.tickArguments([5])
.tickValues(d3.ticks(...d3.extent(x.domain()), width/1).filter(v => x(v) !== undefined))
)
.selectAll("text")
.attr("transform", "translate(-1,1)rotate(-45)")
.style("text-anchor", "end")
//.attr("fill", "magenta")
//.style("font-weight", "bold")
Insert cell
y1Axis = g => g
.attr("transform", `translate(${margin.left},0)`)
.style("color", "steelblue")
.call(d3.axisLeft(y1).ticks(null, "s"))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(data.y1))
Insert cell
/*y2Axis = g => g
.attr("transform", `translate(${width - margin.right},0)`)
.call(d3.axisRight(y2))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", margin.right)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(data.y2))*/
Insert cell
height = 500
Insert cell
margin = ({top: 20, 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