Public
Edited
Aug 1, 2023
10 forks
Importers
Insert cell
Insert cell
chart = {

// Specify the chart’s dimensions.
const width = 928;
const height = 600;
const marginTop = 25;
const marginRight = 20;
const marginBottom = 35;
const marginLeft = 40;

// Prepare the scales for positional encoding.
const x = d3.scaleLinear()
.domain(d3.extent(cars, d => d.mpg)).nice()
.range([marginLeft, width - marginRight]);

const y = d3.scaleLinear()
.domain(d3.extent(cars, d => d.hp)).nice()
.range([height - marginBottom, marginTop]);

// Create the SVG container.
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;");

// Create the axes.
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(d3.axisBottom(x).ticks(width / 80))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", width)
.attr("y", marginBottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text("Miles per gallon →"));

svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -marginLeft)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("↑ Horsepower"));

// Create the grid.
svg.append("g")
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", marginTop)
.attr("y2", height - marginBottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", marginLeft)
.attr("x2", width - marginRight));

// Add a layer of dots.
svg.append("g")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("fill", "none")
.selectAll("circle")
.data(cars)
.join("circle")
.attr("cx", d => x(d.mpg))
.attr("cy", d => y(d.hp))
.attr("r", 3);

// Add a layer of labels.
svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("text")
.data(cars)
.join("text")
.attr("dy", "0.35em")
.attr("x", d => x(d.mpg) + 7)
.attr("y", d => y(d.hp))
.text(d => d.name);

return svg.node();
}
Insert cell
cars = FileAttachment("mtcars.csv").csv({typed: true})
Insert cell
Insert cell
Plot.plot({
grid: true,
nice: true,
x: {ticks: 10},
marks: [
Plot.dot(cars, {x: "mpg", y: "hp", stroke: "steelblue"}),
Plot.text(cars, {x: "mpg", y: "hp", text: "name", textAnchor: "start", dx: 6})
]
})
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