Public
Edited
Sep 2, 2024
Insert cell
Insert cell
viewof selection = {
const height = 600;
const marginTop = 0;
const marginRight = 0;
const marginBottom = 0;
const marginLeft = 0;
console.log("rendered");
// Create the scales
const x = d3
.scaleLinear()
.domain([0, d3.max(data, (d) => d[xVariable])])
.nice()
.range([marginLeft, width - marginRight]);

const y = d3
.scaleLinear()
.domain([0, d3.max(data, (d) => d[yVariable])])
.nice()
.range([height - marginBottom, marginTop]);

// Set up canvas
const canvas = DOM.canvas(width, height);
const ctx = canvas.getContext("2d");
canvas.style.display = "block";
canvas.style.maxWidth = "100%";
canvas.style.margin = "auto";

// Add 'brushed' and 'radius' properties to each data point
data.forEach((d) => {
d.brushed = 0;
d.radius = 3;
});

// Function to draw all points
function drawPoints() {
ctx.clearRect(0, 0, width, height);
data.forEach((d) => {
// ctx.globalCompositeOperation = "multiply";
ctx.globalAlpha = 0.5;
ctx.beginPath();
ctx.arc(x(d[xVariable]), y(d[yVariable]), d.radius, 0, 2 * Math.PI);
ctx.fillStyle = colorScale(d[colorVariable]);
ctx.fill();
});
}

// Initial draw
drawPoints();

// Set up GSAP ticker to redraw points
gsap.ticker.add(drawPoints);

// Create the SVG overlay for the brush
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("position", "absolute")
.style("top", marginTop + "px")
.style("left", marginLeft + "px")
.style("width", "100%")
.style("height", "100%")
.style("pointer-events", "none");

// Append brush to SVG
const brush = d3
.brush()
.extent([
[marginLeft, marginTop],
[width - marginRight, height - marginBottom]
])
.on("start brush end", brushed);

svg
.append("g")
.attr("class", "brush")
.style("pointer-events", "all")
.call(brush);

// Brushed function
function brushed(event) {
const selection = event.selection;
if (selection) {
const [[x0, y0], [x1, y1]] = selection;
data.forEach((d) => {
const isBrushed =
x0 <= x(d[xVariable]) &&
x(d[xVariable]) <= x1 &&
y0 <= y(d[yVariable]) &&
y(d[yVariable]) <= y1;
gsap.to(d, {
brushed: isBrushed ? 1 : 0,
radius: isBrushed ? 6 : 3, // Animate radius between 3 and 6
duration: 0.1,
ease: "power2.inOut"
});
});
} else {
// If brush is cleared, animate all points back to unbrushed state
gsap.to(data, {
brushed: 0,
radius: 3,
duration: 3,
ease: "power2.inOut"
});
}
}

return htl.html`
<div style="position: relative">
${canvas}
${svg.node()}
</div>
`;
}
Insert cell
Insert cell
Insert cell
Insert cell
colorScale = {
const redColors = ["#b91c1c", "#dc2626", "#ef4444", "#f87171", "#fca5a5"];
const grayColors = ["#12375A", "#4E6C8A", "#7590AB", "#A7BDD3", "#D2E4F6"];
const positiveThreshold = d3
.scaleThreshold()
.domain([0.2, 0.4, 0.6, 0.8, 1])
.range(redColors);

const negativeThreshold = d3
.scaleThreshold()
.domain([-0.8, -0.6, -0.4, -0.2, 0])
.range(grayColors.slice().reverse());

return (value) => {
if (value >= 0) {
return positiveThreshold(value);
} else {
return negativeThreshold(value);
}
};
}
Insert cell
// data = FileAttachment("cars-2.csv").csv({typed: true})
Insert cell
Insert cell
data.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
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