Public
Edited
Mar 7, 2023
Fork of Untitled
Insert cell
Insert cell
import {uniqueValid} from "@uwdata/data-utilities"
Insert cell
wrangled@4.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
csvText = FileAttachment("wrangled@4.csv").text()
Insert cell
dataCSV = d3.csvParse(csvText)
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.call(zoom);

svg.append("text")
.attr("x", width / 2)
.attr("y", margin.top)
.attr("font-size", "1.5rem")
.attr("font-family", "sans-serif")
.attr("text-anchor", "middle")
.text("Bachelor's Degrees Awarded in Computer Science by Year");

svg.append("g")
.attr("class", "bars")
.attr("fill", "steelblue")
.selectAll("rect")
.data(dataCSV)
.join("rect")
.attr("x", d => x(d.Year))
.attr("y", d => y(d.BachelorTotal))
.attr("height", d => y(0) - y(d.BachelorTotal))
.attr("width", x.bandwidth());

svg.append("g")
.attr("class", "x-axis")
.call(xAxis);

svg.append("g")
.attr("class", "y-axis")
.call(yAxis);

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

svg.append("text")
.attr("x", width / 2)
.attr("y", margin.top)
.attr("font-size", "1.5rem")
.attr("font-family", "sans-serif")
.attr("text-anchor", "middle")
.text("Master's Degrees Awarded in Computer Science by Year");

svg.append("g")
.attr("class", "bars")
.attr("fill", "steelblue")
.selectAll("rect")
.data(dataCSV)
.join("rect")
.attr("x", d => x(d.Year))
.attr("y", d => y2(d.MasterTotal))
.attr("height", d => y2(0) - y2(d.MasterTotal))
.attr("width", x.bandwidth());

svg.append("g")
.attr("class", "x-axis")
.call(xAxis);

svg.append("g")
.attr("class", "y-axis")
.call(yAxis2);

return svg.node();
}
Insert cell
d3 = require("d3@6")
Insert cell
function zoom(svg) {
const extent = [[margin.left, margin.top], [width - margin.right, height - margin.top]];

svg.call(d3.zoom()
.scaleExtent([1, 8])
.translateExtent(extent)
.extent(extent)
.on("zoom", zoomed));

function zoomed(event) {
x.range([margin.left, width - margin.right].map(d => event.transform.applyX(d)));
y.range([height - margin.bottom, margin.top].map(d => event.transform.applyY(d)));
svg.selectAll(".bars rect").attr("x", d => x(d.Year))
.attr("y", d => y(d.BachelorTotal))
.attr("height", d => y(0) - y(d.BachelorTotal))
.attr("width", x.bandwidth());
svg.selectAll(".x-axis").call(xAxis);
svg.selectAll(".y-axis").call(yAxis);
}
}
Insert cell
function zoomMaster(svg) {
const extent = [[margin.left, margin.top], [width - margin.right, height - margin.top]];

svg.call(d3.zoom()
.scaleExtent([1, 8])
.translateExtent(extent)
.extent(extent)
.on("zoom", zoomed));

function zoomed(event) {
x.range([margin.left, width - margin.right].map(d => event.transform.applyX(d)));
y.range([height - margin.bottom, margin.top].map(d => event.transform.applyY(d)));
svg.selectAll(".bars rect").attr("x", d => x(d.Year))
.attr("y2", d => y2(d.MasterTotal))
.attr("height", d => y2(0) - y2(d.MasterTotal))
.attr("width", x.bandwidth());
svg.selectAll(".x-axis").call(xAxis);
svg.selectAll(".y2-axis").call(yAxis2);
}
}
Insert cell
x = d3.scaleBand()
.domain(dataCSV.map(d => d.Year))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(dataCSV, d => d.BachelorTotal)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
y2 = d3.scaleLinear()
.domain([0, 55000]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
Insert cell
yAxis2 = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y2))
.call(g => g.select(".domain").remove())
Insert cell
margin = ({top: 20, right: 0, bottom: 30, left: 40})
Insert cell
height = 650
Insert cell
Insert cell
survey_results_sample.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
csvSample = FileAttachment("survey_results_sample.csv").text()
Insert cell
sampleCSV = d3.csvParse(csvSample)
Insert cell
parsedSampleCSV = d3.group(sampleCSV, d => d.EdLevel)
Insert cell
Insert cell
Insert cell
Insert cell
test = {
const selection = vl.selectPoint('Select')
.fields('EdLevel', 'MainBranch')
.init({EdLevel: 'Bachelor’s degree (B.A., B.S., B.Eng., etc.)', MainBranch: 'I am a developer by profession'})
.bind({EdLevel: vl.menu(edLevel).name("Education Level "), MainBranch: vl.menu(branch).name("Proficiency Level ")});
// scatter plot, modify opacity based on selection
return vl.markCircle()
.data(sampleCSV)
.params(selection)
.encode(
vl.x().fieldN('MainBranch'),
vl.y().fieldN('EdLevel'),
vl.size().count(),
vl.tooltip().count(),
vl.opacity().if(selection, vl.color().value(1)).value(0.1),
)
.title("Count of Records based on Proficiency Level and Education Level")
.render();
}
Insert cell
Insert cell
try2.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
csvFuture = FileAttachment("try2.csv").text()
Insert cell
FutureCSV = d3.csvParse(csvFuture)
Insert cell
Degree = uniqueValid(FutureCSV, d=> d.Degree)
Insert cell
Bars2 = {
const selection = vl.selectSingle()
.encodings('x')
.fields('Degree')
.init({Degree: 'Computer and information sciences'})
.bind({Degree: vl.menu(Degree).name("Level and field of highest degree")});

const totalBar = vl.markBar()
.data(FutureCSV)
.transform(
vl.filter(selection),
vl.aggregate([
vl.sum('Total').as('Total'),
])
.groupby(['Degree'])
)
.encode(
vl.x().fieldN('Degree'),
vl.y().fieldQ('Total'),
vl.tooltip().fieldQ('Total'),
vl.color().condition(
{test: selection, value: 'blue'}
)
);

const closelyRelatedBar = vl.markBar()
.data(FutureCSV)
.transform(
vl.filter(selection),
vl.aggregate([
vl.sum('Closely related').as('Closely related'),
])
.groupby(['Degree'])
)
.encode(
vl.x().fieldN('Degree'),
vl.y().fieldQ('Closely related'),
vl.tooltip().fieldQ('Closely related'),
vl.color().condition(
{test: selection, value: 'red'}
)
);

return vl.hconcat(
vl.layer(totalBar, closelyRelatedBar)
.title("Total compared to Closely Related Fields "),
vl.markBar(FutureCSV)
.select(selection)
.title("Select Degree")
.width(200)
).render();
}

Insert cell
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