Published
Edited
Jun 8, 2020
Importers
Insert cell
md`# Average ACT Score`
Insert cell
Insert cell
Insert cell
act_data_new = data.filter(function(d) {
return d[year] != "";
})
Insert cell
minY2 = 0
Insert cell
maxY2 = 36
Insert cell
uniqX = _.uniqBy(act_data, d => d.x).map(d => d.x)
Insert cell
xScale2 = d3
.scaleBand()
.domain(uniqX)
.range([margin.left, width - margin.right])
.padding(.3)
Insert cell
yDomain2 = [minY2 - 1, maxY2 + 1]
Insert cell
yScale2 = d3
.scaleLinear()
.domain(yDomain2)
.range([height - margin.bottom, margin.top])
Insert cell
xAxis2 = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale2))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-40)")
Insert cell
yAxis2 = g =>
g.attr("transform", `translate(${margin.left}, 0)`).call(d3.axisLeft(yScale2))
Insert cell
Insert cell
act_chart = {
const svg = d3.create("svg").attr("viewBox", [-50, 0, width, height + 100]);

svg.append("g").call(yAxis2);
svg.append("g").call(xAxis2);

svg
.selectAll('rect')
.data(act_data)
.join(enter => enter.append('rect'))
.attr('x', d => xScale2(d.x))
.attr('y', d => yScale2(minY2))
.attr('width', xScale2.bandwidth())
.attr('height', d => 0)
.transition()
.duration(1000)
// .attr('fill', d => colorScale2(d.color))
.style("fill", "steelblue")
.attr("y", d => yScale2(d.y) + 15)
.attr("height", d => yScale2(minY2) - yScale2(d.y));

svg
.append("text")
.attr("class", "x label")
.attr("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", height + 60)
.text("Sex And Ethnicity");
svg
.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("x", -height / 2)
.attr("y", 6)
.attr("transform", "rotate(-90)")
.text("Average Score");

svg
.append("text")
.text("Average ACT Score For Different Sex And Ethnicity")
.style("text-anchor", "middle")
.attr("transform", `translate(${width / 2}, 100)`)
.style("font-size", "1.2em");

const tooltip = svg.append("g");
svg
.selectAll('rect')
.on("mouseover", function(d) {
tooltip.call(
callout,
`${d.x}
Average Score: ${d.y}`
);
d3.select(this)
.attr("stroke", "black")
.attr("stroke-width", 2);
})
.on("mousemove", function() {
tooltip.attr(
"transform",
`translate(${d3.mouse(this)[0]},${d3.mouse(this)[1]})`
);
d3.select(this).style("fill", "orange");
})
.on("mouseout", function() {
tooltip.call(callout, null);
d3.select(this)
.attr("stroke", null)
.style("fill", "steelblue");
});

return svg.node();
return svg.node();
}
Insert cell
d3 = require("d3")
Insert cell
_ = require("lodash")
Insert cell
margin = ({ top: 100, right: 50, bottom: 30, left: 40 })
Insert cell
height = 700
Insert cell
import { select } from "@jashkenas/inputs"
Insert cell
import { callout } from "@d3/line-chart-with-tooltip"
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