Published
Edited
Sep 18, 2020
3 forks
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require('d3@6')
Insert cell
Insert cell
Insert cell
function createData(size) {
let data = [];
for (let i = 0; i < size; ++i) {
data.push({
x: Math.random() * 1000,
y: Math.random() * 1000,
size: Math.floor(Math.random() * 200 + 1)
});
}
return data;
}
Insert cell
data = createData(100)
Insert cell
Insert cell
minData = ({ x: d3.min(data, d => d.x), y: d3.min(data, d => d.y), size: d3.min(data, d => d.size) })
Insert cell
maxData = ({ x: d3.max(data, d => d.x), y: d3.max(data, d => d.y), size: d3.max(data, d => d.size) })
Insert cell
medianData = ({ x: d3.median(data, d => d.x), y: d3.median(data, d => d.y), size: d3.median(data, d => d.size) })
Insert cell
extentX = d3.extent(data, d => d.x)
Insert cell
extentY = d3.extent(data, d => d.y)
Insert cell
extentSize = d3.extent(data, d => d.size)
Insert cell
Insert cell
margin = ({ left: 30, top: 30, right: 30, bottom: 30 })
Insert cell
width = 900
Insert cell
height = 480
Insert cell
gHeight = height - margin.top - margin.bottom
Insert cell
gWidth = width - margin.left - margin.right
Insert cell
Insert cell
x = d3.scaleLinear()
.domain(extentX)
.range([0, gWidth])
Insert cell
cx = d3.scaleLinear()
.domain(extentX)
.range([margin.left, gWidth + margin.left])
Insert cell
[ x(minData.x), x(maxData.x), x(medianData.x) ]
Insert cell
y = d3.scaleLinear()
.domain(extentY)
.range([0, gHeight])
Insert cell
[ y(minData.y), y(maxData.y), y(medianData.y) ]
Insert cell
cy = d3.scaleLinear()
.domain(extentY)
.range([margin.top, gHeight + margin.top])
Insert cell
[ cy(minData.y), cy(maxData.y), cy(medianData.y) ]
Insert cell
size = d3.scaleLinear()
.domain(extentSize)
.range([1, gWidth / 20])
Insert cell
Insert cell
xAxis = d3.axisTop(x)
Insert cell
yAxis = d3.axisLeft(y)
Insert cell
md `## Draw`
Insert cell
function setupCanvas() {
// 1. Clear last painted
d3.select('.canvas').selectAll('svg').remove();
// 2. Create svg
const svg = d3.select('.canvas').append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
// 3. Define clip-path to clip graph out of the bounds
svg.append('defs')
.append('svg:clipPath')
.attr('id', 'clip')
.append('svg:rect')
.attr('width', gWidth)
.attr('height', gHeight)
.attr('x', 0)
.attr('y', 0);
// 4. Append x axis
svg.append('g')
.attr('id', 'axis_x')
.call(xAxis);
// 5. Append y axis
svg.append('g')
.attr('id', 'axis_y')
.attr('transform', `translate(0,0)`)
.call(yAxis);
return svg;
}
Insert cell
svg = setupCanvas()
Insert cell
function setupScatter() {
// 6. Create scatter section to place circles
const scatter = svg.append('g')
.attr('id', 'scatterplot')
.attr('clip-path', 'url(#clip)')
.on('dblclick', onDblClicked);
// 7. Add circles
scatter.selectAll('.dot')
.data(data)
.join('circle')
.attr('class', 'dot')
.attr('r', d => size(d.size))
.attr('cx', d => cx(d.x))
.attr('cy', d => cy(d.y))
.attr('opacity', 0.5)
.style('fill', '#d989d6');
return scatter;
}
Insert cell
scatter = setupScatter()
Insert cell
function setupBrush() {
const brush = d3.brush()
.extent([[0,0],[gWidth, gHeight]])
.on('end', onBrushEnd);
return brush;
}
Insert cell
brush = setupBrush()
Insert cell
scatter.append('g')
.attr('class', 'brush')
.call(brush)
Insert cell
Insert cell
Insert cell
function onBrushEnd(ev) {
const s = ev.selection;
if (!!s) {
x.domain([s[0][0], s[1][0]].map(x.invert, x));
y.domain([s[0][1], s[1][1]].map(y.invert, y));
cx.domain([s[0][0], s[1][0]].map(cx.invert, cx));
cy.domain([s[0][1], s[1][1]].map(cy.invert, cy));
scatter.selectAll('.brush').call(ev.target.clear);
zoom();
}
}
Insert cell
Insert cell
function zoom() {
scatter.transition().duration(750);
svg.select('#axis_x').transition().call(xAxis);
svg.select('#axis_y').transition().call(yAxis);
scatter.selectAll('circle')
.transition()
.attr('cx', d => cx(d.x))
.attr('cy', d => cy(d.y))
.attr('r', d => size(d.size));
}
Insert cell
Insert cell
function onDblClicked() {
x.domain(extentX);
y.domain(extentY);
cx.domain(extentX);
cy.domain(extentY);
svg.select('#axis_x').transition().call(xAxis);
svg.select('#axis_y').transition().call(yAxis);
// avoid circular definition
d3.select('#scatterplot')
.selectAll('circle')
.transition()
.attr('cx', d => cx(d.x))
.attr('cy', d => cy(d.y))
.attr('r', d => size(d.size));
}
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