Published
Edited
Mar 31, 2021
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// code here
Insert cell
Insert cell
// code here
Insert cell
Insert cell
// code here
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3")
Insert cell
Insert cell
Insert cell
simpleContainer = {
const container = d3.select(DOM.svg(1000, 100));
container
.insert('rect')
.attr('width', 30)
.attr('height', 30)
.attr('x', 10)
.attr('y', 10)
.attr('fill', 'purple')
.attr('stroke', 'pink')
.attr('stroke-width', 4);
return container.node();
}
Insert cell
Insert cell
Insert cell
colorGroupsContainer = {
const container = d3.select(DOM.svg(1000, 100));
const pinkGroup = container
.append("g")
.attr("id", "pinkGroup")
const greenGroup = container
.append("g")
.attr("id", "greenGroup")
pinkGroup
.append('rect')
.attr('x', 10)
.attr('y', 10)
.attr('width', 50)
.attr('height', 10)
.attr('fill', '#ff96ca');
greenGroup
.append('rect')
.attr('x', 500)
.attr('y', 50)
.attr('width', 100)
.attr('height', 10)
.attr('fill', 'green');
return container.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
createCirclesWithData = {
const container = d3.select(DOM.svg(1000, 100));
const arr = [ 20, 5, 10, 25, 15 ]
container.selectAll("circle")
.data(arr)
.enter().append("circle")
.attr("fill", "steelblue")
.attr("r", function(d) { return d; })
.attr("cx", function(d, i) { return (i+1)*50; })
.attr("cy", 50);
return container.node();
}
Insert cell
Insert cell
oneOddballCircle = {
const container = d3.select(DOM.svg(1000, 100));
const arr = [ 20, 5, 10, 25, 15 ]
container.append("circle")
.attr("fill", "steelblue")
.attr("cx", 50)
.attr("cy", 50)
.attr("r", 20);
container.selectAll("circle")
.data(arr)
.enter().append("circle")
.attr("r", function(d) { return d; })
.attr("cx", function(d, i) { return (i+1)*50; })
.attr("cy", 50)
// .attr('fill', 'green')
;
return container.node();
}
Insert cell
Insert cell
Insert cell
circlesWithJoin = {
const container = d3.select(DOM.svg(1000, 100));
const arr = [ 20, 5, 10, 25, 15 ]
container.append("circle")
.attr("fill", "steelblue")
.attr("cx", 50)
.attr("cy", 50)
.attr("r", 20);
container.selectAll("circle")
.data(arr)
// .enter().append("circle")
.join("circle")
.attr("r", function(d) { return d; })
.attr("cx", function(d, i) { return (i+1)*50; })
.attr("cy", 50)
// .attr('fill', 'green');
return container.node();
}
Insert cell
Insert cell
Insert cell
circlesWithGroups = {
const width = 1000;
const height = 100;
const container = d3.select(DOM.svg(width, height));
const arr = [ 20, 5, 10, 25, 15 ]
const componentGroup =
container.selectAll("g")
.data(arr)
.enter().append("g")
.attr("class", "circleGroup")
.attr("transform", (d, i) => `translate(${((i+1) * 50)}, 50)`);
/* Note: this is an alternative if you don't like the backtick syntax:
.attr("transform", (d,i) => "translate(" + (i+1) * 50 + "," + 50 + ")");
*/

/*
Notice that we are not setting x and y locations for the circles or the text below;
they are inherited from the g group.
notice also that componentGroup refers to a list of selections with data bound to them
To see what's going on, uncomment the line below and look in the Console of the Inspector
*/
// console.log(componentGroup);
componentGroup
.append("circle")
.attr("r", d=> d)
.attr("fill", "steelblue");
componentGroup
.append("text")
.attr("fill", "yellow")
.attr("text-anchor", "middle") // SVG attributes
.style("font", "0.75em sans-serif") // CSS styles
.attr("dy", "0.35em") // a tweak to center the text in the circle
.text(d => d);
return container.node();
}
Insert cell
Insert cell
Insert cell
FileAttachment("simple_bar_chart.png").image()
Insert cell
// This is your starter code
simpleBarCharts = {
const width = 1000;
const height = 100;
const barwidth = 40;
const container = d3.select(DOM.svg(width, height));
const arr = [ 20, 5, 10, 25, 15 ]

return container.node();
}
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