Public
Edited
Apr 26, 2023
Insert cell
Insert cell
hybridBarLineChart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width+100, height]);

svg.append("g")
.attr("fill", "steelblue")
.attr("fill-opacity", 0.8)
.selectAll("rect")
.data(idiom2Data)
.join("rect")
.attr("x", d => x(d.year))
.attr("width", x.bandwidth())
.attr("y", d => y1(d.h1b_count))
.attr("height", d => y1(0) - y1(d.h1b_count));

svg.append("path")
.attr("fill", "none")
.attr("stroke", "currentColor")
.attr("stroke-miterlimit", 1)
.attr("stroke-width", 3)
.attr("d", line(idiom2Data));

svg.append("g")
.attr("fill", "none")
.attr("pointer-events", "all")
.selectAll("rect")
.data(idiom2Data)
.join("rect")
.attr("x", d => x(d.year))
.attr("width", x.bandwidth())
.attr("y", 0)
.attr("height", height)
.append("title")
.text(d => `${d.year}
${d.h1b_count.toLocaleString("en")} H1B Petition Count
${d.unemployment.toLocaleString("en")} Unemployment Rate`);

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

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

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

return svg.node();
}
Insert cell
y2Axis = g => g
.attr("transform", `translate(${width - margin.right},0)`)
.call(d3.axisRight(y2).ticks(null, "%"))
.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("Unemployment Rate"))
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("H1B Petition Count"))
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))
Insert cell
y2 = d3.scaleLinear()
.domain(d3.extent(idiom2Data, d => d.unemployment))
.rangeRound([height - margin.bottom, margin.top])
Insert cell
y1 = d3.scaleLinear()
.domain([0, d3.max(idiom2Data, d => d.h1b_count)])
.rangeRound([height - margin.bottom, margin.top])
Insert cell
x = d3.scaleBand()
.domain(idiom2Data.map(d => d.year))
.rangeRound([margin.left, width - margin.right])
.padding(0.1)
Insert cell
line = d3.line()
.x(d => x(d.year) + x.bandwidth() / 2)
.y(d => y2(d.unemployment))
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
height = 500
Insert cell
idiom2Data = FileAttachment("idiom2-data.csv").csv({typed: true})
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more