Published
Edited
Feb 13, 2022
Insert cell
Insert cell
Insert cell
{
const div = document.createElement("div");
div.innerHTML = "Hello, world!";
return div;
}
Insert cell
Insert cell
{
const p = d3.selectAll("p");
// p.style("color", "red"); // Uncomment this line and run the code!
// p.style("color", null); // Uncomment this line to clear the color.
return p;
}
Insert cell
// Now you try it: change all <h2> elements on this page to a green color
Insert cell
Insert cell
{
const div = d3.create("div");
div.html("Hello, world!");
// make this div's hello world text white and background color black
return div.node();
}
Insert cell
Insert cell
d3.create("span")
.style("color", "red")
.html("Hello, world!")
.node()
Insert cell
// create another div with words of your choice, white text, and black background color, all one command (chained syntax)
Insert cell
Insert cell
Insert cell
data = [4, 8, 23, 15, 32, 19]
Insert cell
Insert cell
html`<svg viewBox="0,0,1000,100" width="1000" height="100">
<circle cx="50" cy="50" r="4"></circle>
<circle cx="100" cy="50" r="8"></circle>
<circle cx="150" cy="50" r="23"></circle>
<circle cx="200" cy="50" r="15"></circle>
<!-- add two more circles here -->
</svg>`
Insert cell
Insert cell
html`<svg id="basiccircles" viewBox="0,0,1000,100" width="1000" height="100">
<circle cx="50" cy="50" r="15"></circle>
<circle cx="100" cy="50" r="15"></circle>
<circle cx="150" cy="50" r="15"></circle>
<circle cx="200" cy="50" r="15"></circle>
<circle cx="250" cy="50" r="15"></circle>
<circle cx="300" cy="50" r="15"></circle>
</svg>`
Insert cell
// run this
d3.select('#basiccircles')
.selectAll('circle') // we can select the circles...
.data(data) // and then join the array to it...
.attr('r', d => d); // and then set each circle's `r` to the corresponding data element's value
Insert cell
Insert cell
Insert cell
html`<svg id="walletcircles" viewBox="0,0,1000,100" width="1000" height="100">
<!-- your circles here -->
</svg>`
Insert cell
// your d3 code to bind and modify wallet circles
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("width", 1000)
.attr("height", 100);
// create 1 circle
svg.append("circle")
.attr("fill", "purple")
.attr("cx", 50)
.attr("cy", 50)
.attr("r", 25);

return svg.node();
}
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("width", 1000)
.attr("height", 100);
// --- bind circle to data
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", (d, i) => (i+1)*50 )
.attr("cy", 50)
.attr("r", d => d);

// --- alternatively, using ".join"
// svg.selectAll("circle")
// .data(data)
// .join("circle") // the "join" selection is a convenience to merge the "enter" and "update" states, which is totally sufficient for our static chart
// .attr("cx", (d, i) => (i+1)*50 )
// .attr("cy", 50)
// .attr("r", d => d);
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// your wallet viz created entirely with D3
Insert cell
Insert cell
data // showing it again here for convenience
Insert cell
html`<svg width="420" height="120" font-family="sans-serif" font-size="10" text-anchor="end">
<g transform="translate(0,0)">
<rect fill="steelblue" width="40" height="19"></rect>
<text fill="white" x="37" y="9.5" dy=".35em">4</text>
</g>
<g transform="translate(0,20)">
<rect fill="steelblue" width="80" height="19"></rect>
<text fill="white" x="77" y="9.5" dy=".35em">8</text>
</g>
<g transform="translate(0,40)">
<rect fill="steelblue" width="230" height="19"></rect>
<text fill="white" x="227" y="9.5" dy=".35em">23</text>
</g>
<g transform="translate(0,60)">
<rect fill="steelblue" width="150" height="19"></rect>
<text fill="white" x="147" y="9.5" dy=".35em">15</text>
</g>
<!-- add two more bars here in SVG -->
</svg>`
Insert cell
Insert cell
Insert cell
Insert cell
scale = d3.scaleLinear()
Insert cell
Insert cell
// re-run this cell after updating the scale above
scale(2.5)
Insert cell
Insert cell
ordinalScale = d3.scaleOrdinal()
.domain(['cat', 'dog', 'parrot', 'hamster'])
.range(d3.schemePaired); // https://github.com/d3/d3-scale-chromatic#schemePaired
Insert cell
c = ordinalScale('parrot')
Insert cell
Insert cell
Insert cell
Insert cell
width = 420
Insert cell
height = 20 * data.length
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data)])
.range([0, width])
Insert cell
y = d3.scaleBand()
.domain(d3.range(data.length))
.range([0, height])
Insert cell
Insert cell
autobar = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", y.range()[1])
.attr("font-family", "sans-serif")
.attr("font-size", "10")
.attr("text-anchor", "end")
.style('border', '1px dotted #999'); // to help see the edges of our canvas

const bar = svg.selectAll("g")
.data(data)
.join("g")
.attr("transform", (d, i) => `translate(0,${y(i)})`);

bar.append("rect")
.attr("fill", "steelblue")
.attr("x", 0)
.attr("width", x)
.attr("height", y.bandwidth() - 1);

bar.append("text")
.attr("fill", "white")
.attr("x", 17) // <----- modify this line
.attr("y", y.bandwidth() / 2)
.attr("dy", "0.35em")
.text(d => d);

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
margin = ({top: 10, right: 10, bottom: 20, left: 20})
Insert cell
Insert cell
xMargin = x.copy().range([margin.left, width - margin.right])
Insert cell
yMargin = y.copy().range([margin.top, height - margin.bottom])
Insert cell
Insert cell
// Your `autobar` chart with value labels right aligned, with margins
Insert cell
Insert cell
{
const svg = d3.create("svg")
.attr("width", width)
.attr("height", y.range()[1])
.attr("font-family", "sans-serif")
.attr("font-size", "10")
.attr("text-anchor", "end")
.style('border', '1px dotted #999'); // to help see the edges of our canvas

svg.append('g')
.attr('transform', `translate(0, ${height - margin.bottom})`)
.call(d3.axisBottom(xMargin));
svg.append('g')
.attr('transform', `translate(${margin.left}, 0)`)
.call(d3.axisLeft(yMargin));

return svg.node();
}
Insert cell
Insert cell
Insert cell
// Your `autobar` chart with value labels right aligned, with margins and axes
Insert cell
Insert cell
Insert cell
Insert cell
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