Published unlisted
Edited
Jan 1, 2021
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3
.create('svg')
.attr('class', 'chart-test')
.call(setSize, width, 100);

const g = svg.append('g');

let node = g.selectAll();

invalidation.then(() => console.log('invalidated'));

return Object.assign(svg.node(), {
update(data) {
scaleRadius.domain([d3.min(data, d => d.value), d3.max(data, d => d.value)]);
scaleX.domain([0, d3.max(data, d => d.id)]);
node = node.data(data, d => d.id).join(enterNode, updateNode);
}
});
}
Insert cell
chart.update(data)
Insert cell
mutable data = d3.range(10).map((_, i) => ({ id: i, value: i }))
Insert cell
onShuffle = () => mutable data = data.map(v => ({ ...v, value: Math.random() * 10 }))
Insert cell
scaleRadius = d3
.scaleLinear()
.range([10, 25])
Insert cell
scaleX = d3
.scaleLinear()
.range([30, width - 30])
Insert cell
enterNode = node =>
node
.append('circle')
.attr('class', 'node')
.attr('data', d => d)
.call(tr =>
tr
.call(setCoords, (d, i) => [scaleX(d.id), 30])
.attr('fill', 'black')
.attr('r', 0)
.transition(200)
.attr('r', d => scaleRadius(d.value))
)
Insert cell
// it appears update does not work for mutable data
updateNode = node =>
node.call(tr =>
tr
.transition(200)
.attr('fill', 'red')
.call(setCoords, d => [scaleX(d.id), 30])
.attr('r', d => scaleRadius(d.value))
)
Insert cell
import { setCoords, setSize, trans2d } from '@rabelais/tree-utils'
Insert cell
d3 = require('d3@v6.3.1')
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