Published
Edited
May 20, 2021
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chartOccup = {
/* 2021-05-18 Program: Activities on vert + occup on horiz and see where clusters are
scatterplot:
https://www.d3-graph-gallery.com/graph/scatter_basic.html

https://observablehq.com/@d3/scatterplot << This was used
*/
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.append("g")
.call(xAxis);

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

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

svg.append("g")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.0)
.attr("fill", "red")
.selectAll("circle")
//.data(data)
.data(dataSOC)
.join("circle")
.attr("cx", d => xScale(d.x))
.attr("cy", d => yScale((35 - d.y)))
.attr("r", 2);

/* svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("text")
.data(data)
.join("text")
.attr("dy", "0.35em")
.attr("x", d => xScale(d.x) + 7)
.attr("y", d => yScale(d.y * 3))
.text(d => d.name); */

return svg.node();
}
Insert cell
d3.extent(dataSOC, d => d.x)
Insert cell
data = Object.assign(d3.csvParse(await FileAttachment("mtcars.csv").text(), ({name, mpg: x, hp: y}) => ({name, x: +x, y: +y})), {xAxisLabel: "Occupation →", yAxisLabel: "↑ Skill"})
Insert cell
Insert cell
xScale = d3.scaleLinear()
.domain(d3.extent(dataSOC, d => d.x)).nice()
.range([margin.left, width - margin.right])
Insert cell
yScale = d3.scaleLinear()
.domain(d3.extent(dataSOC, d => d.y)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale).ticks(width / 80))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(data.xAxisLabel))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(yScale)) // what is y / yScale?
.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.yAxisLabel))
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(xScale.ticks())
.join("line")
.attr("x1", d => 0.5 + xScale(d))
.attr("x2", d => 0.5 + xScale(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(yScale.ticks())
.join("line")
.attr("y1", d => 0.5 + yScale(d))
.attr("y2", d => 0.5 + yScale(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
margin = ({top: 25, right: 20, bottom: 35, left: 40})
Insert cell
height = 600
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