Published
Edited
Jan 8, 2021
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
sample_svg = {
// Make and svg
const svg = d3.create("svg").attr("width", width);

// Append some shapes
const circle = svg
.append("circle")
.attr("cx", 100)
.attr("cy", 100)
.style("fill", "blue")
.attr("r", 10)
.style("opacity", .5);

const circle2 = svg
.append("circle")
.attr("cx", 100)
.attr("cy", 50)
.style("fill", "red")
.attr("r", 10)
.style("opacity", .5);

// Return the dom node to show
return svg.node();
}
Insert cell
Insert cell
// Show a preview table
render_data_table(penguins)
Insert cell
// Max. body mass
max_body_mass = d3.max(penguins, d => +d.body_mass_g)
Insert cell
// Max. body mass
min_body_mass = d3.min(penguins, d => +d.body_mass_g)
Insert cell
// If you want to get both at once
extent_mass = d3.extent(penguins, d => +d.body_mass_g)
Insert cell
// Create the scale
scale = d3
.scaleLinear()
.domain([min_body_mass, max_body_mass])
.range([0, width])
Insert cell
Insert cell
margin = ({left:30, right:30})
Insert cell
scale_with_margin = d3
.scaleLinear()
.domain(extent_mass)
.range([margin.left, width - margin.right])
Insert cell
// Displaying axis with JS
{
const svg = d3.create("svg").attr("width", width);

const axis = d3.axisBottom().scale(scale_with_margin);
const g = svg
.append("g")
.attr("transform", "translate(0, 50)")
.call(axis);

return svg.node();
}
Insert cell
Insert cell
// Displaying axis with JS
{
const svg = d3.create("svg").attr("width", width);

const axis = d3.axisBottom().scale(scale_with_margin);
const g = svg
.append("g")
.attr("transform", "translate(0, 50)")
.call(axis);

penguins
.filter(d => !isNaN(+d.body_mass_g)) // filter out missing data
.map(d => {
svg
.append("circle")
.attr("r", 3)
.attr("cx", scale_with_margin(d.body_mass_g))
.style("opacity", .3)
.attr("cy", 10);
});
return svg.node();
}
Insert cell
Insert cell
import { penguins } from "@enjalot/palmer-penguins"
Insert cell
data = d3.csv(await FileAttachment("data.csv").url())
Insert cell
Insert cell
Insert cell
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