Published
Edited
May 9, 2019
Insert cell
Insert cell
Insert cell
Insert cell
chart = {

const svg = d3.select(DOM.svg(width, height));
svg.append("path")
.datum(means_clean)
.attr("fill", "none")
.attr("stroke", "orange")
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
return svg.node();


}
Insert cell
Insert cell
height = 500
Insert cell
width = 800
Insert cell
margin = ({top:100, right:20, bottom: 70, left: 50})
Insert cell
Insert cell
xAxis = g => g
.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(xScale).ticks(width/80).tickSizeOuter(0));
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left}, 0)`)
.call(d3.axisLeft(yScale).tickSizeOuter(0))
.call(g => g.select(".domain").remove());
Insert cell
Insert cell
yScale = d3.scaleLinear()
.domain([0, d3.max(means_clean, d => d.value)]).nice()
.range([height - margin.bottom, margin.top]);
Insert cell
xScale = d3.scaleTime()
.domain(d3.extent(means_clean, d => d.key))
.range([margin.left, width - margin.right]);
Insert cell
line = d3.line()
.x(d => xScale(d.key))
.y(d => yScale(d.value))
.curve(d3.curveLinear)
Insert cell
Insert cell
Insert cell
freedom = d3.csv("https://gist.githubusercontent.com/will-r-chase/3f4503ccfe9d7a559023ce62cfafbe6c/raw/b45c9cc5b512a36f7a4ae2755b03a6acb39be489/human_freedom_index_2018.csv", d3.autoType)
Insert cell
////this is a total dumpster fire, I have no idea what I'm doing
////I find js logic for array manipulation completely awful compared to R
////why can't I just say freedom.year = timeParse(freedom.year)??? would be so much easier
////this works, but I know it's bad, should study this more later
Insert cell
dat2 = year_means.map(d => {return +d.key});
Insert cell
dat3 = dat2.map(d => timeParse(d));
Insert cell
means_clean = year_means.map((d, i) => ({...d, key: dat3[i]}));
Insert cell
year_means = d3.nest()
.key(d => d.year)
.rollup(function(v) { return d3.mean(v, d => d.pf_score); })
.entries(freedom)
.reverse();
Insert cell
timeParse = d3.timeParse("%Y")
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