Published
Edited
Jun 25, 2019
Importers
Insert cell
md`# D3-Workshop-Exercises`
Insert cell
Insert cell
updateExercise = (multi) => {
const HEIGHT = 100
const WIDTH = width
const testData = [15, 45, 75] // with this test data lets practice making circle enter/exit
const allDatas = [12, 8, 15, 45, 75, 33, 100, 190]
const transitionDuration = 1000
const svg = d3.select(DOM.svg(WIDTH, HEIGHT))
// Fill in update function
const update = (data) => {
// =========== Step 1: Data Join ============
const node = svg.selectAll('.myUpdate')
.data(data)

// =========== Step 2: Enter / Merge ============
const nodeEnter = node.enter()
.append("circle")
.attr("cx", 0)
.attr("cy", 0)
.attr("r", 10)
nodeEnter.merge(node)
.attr('class', 'myUpdate')
.transition()
.duration(transitionDuration) // duration of the animation
.delay((d, i) => 100 * i) // stagger delay animation start
.attr("cx", (d)=> d)
.attr("cy", 50)

// =========== Step 3: Exit / Remove ============
const nodeExit = node.exit().remove()
}
// UNCOMMENT TO TEST YOUR FUNCTION
if(multi) {
const interval = d3.interval((t) => {
if (t > 1500 * 10) { // stop after 10 times
interval.stop()
return
}
let endIndex = Math.floor(Math.random() * allDatas.length - 1)
if (endIndex < 1) {
endIndex = 1
}
update(d3.shuffle([...allDatas]).slice(0, endIndex).sort())
}, 1500)
} else {
update(testData)
}
return svg.node()

}
Insert cell
Insert cell
axisExample = {
const height = 300
const margin = {top: 30, bottom: 30, left: 40, right: 40}
const svg = d3.select(DOM.svg(width, height))
const xScale = d3.scaleLinear().domain([20, 100]).range([margin.bottom, width - margin.right])
const yScale = d3.scaleLinear().domain([30, 0]).range([margin.bottom, height - margin.top])
const xaxis = d3.axisBottom().scale(xScale)
const xaxis_container = svg.append("g")
.attr("class", "xaxis")
.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(xaxis)
const yaxis = d3.axisLeft().scale(yScale)
const yaxis_container = svg.append("g")
.attr("class", 'yaxis')
.attr('transform', `translate(${margin.top}, 0)`)
.call(yaxis)
return svg.node()
}
Insert cell
d3 = require("d3@5")
Insert cell
axisWithData = {
const height = 300
const margin = {top: 30, bottom: 30, left: 40, right: 40}
const data = [{x:30, y:20}, {x:55, y:8},{x:79, y:24}]
const svg = d3.select(DOM.svg(width, height))
const xScale = d3.scaleLinear().domain([20, 100]).range([margin.bottom, width - margin.right])
const yScale = d3.scaleLinear().domain([30, 0]).range([margin.bottom, height - margin.top])
const xaxis = d3.axisBottom().scale(xScale)
const xaxis_container = svg.append("g")
.attr("class", "xaxis")
.attr("transform", `translate(0, ${height - margin.bottom})`)
.call(xaxis)
const yaxis = d3.axisLeft().scale(yScale)
const yaxis_container = svg.append("g")
.attr("class", 'yaxis')
.attr('transform', `translate(${margin.top}, 0)`)
.call(yaxis)
svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("r", 10)
.attr("cy", (d) => yScale(d.y))
.attr("cx", (d) => xScale(d.x))
return svg.node()
}
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