Public
Edited
Mar 28, 2024
Insert cell
Insert cell
viewof selectedPointIndices = createScatterplotView({ points, invalidation })
Insert cell
points = {// Create a random integer function

function getRandomNumber() {
return Math.random() * 2 - 1; // This will return a random number between -1 and 1
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const points = [];

for (let i = 0; i < 100; i++) {
const x = getRandomNumber();
const y = getRandomNumber();
const fifthComponent = Math.floor(i / 10); // Every 10 points will have the same fifth component
points.push([x, y, 0, 0, fifthComponent]);
}

// const points3 = [
// [1, 1, 0, 0, 0],
// [2, 2, 0, 0, 0],
// [3, 3, 0, 0, 1],
// [4, 4, 0, 0, 1],
// [5, 5, 0, 0, 0],
// ];
return points;
}
Insert cell
/*
* Simple helper to create a dynamic viewof scatterplot based on:
* - https://observablehq.com/@radames/hello-regl-scatterplot
* - https://observablehq.com/@fil/hello-regl-scatterplot
*/

function createScatterplotView({
width = 600,
height = 340,
pointSize = 5,
pointColor = "#000000",
points = [[0, 0]],
backgroundColor = "#ffffff",
invalidation
} = {}) {
const canvas = document.createElement("canvas");
const scatterplot = createScatterplot({
canvas,
pointSize,
pointColor,
width,
height,
lassoOnLongPress: true,
backgroundColor,
showPointConnections: true,
pointConnectionSize: 2,
pointConnectionColor: "#FFFF00"
});

scatterplot.subscribe("select", ({ points }) => {
canvas.value = points;
canvas.dispatchEvent(new Event("input"));
});

// scatterplot.set({ pointColor: "white", showPointConnections: true, pointConnectionSize: 2, pointConnectionColor: "yellow"}); // color by the third value
scatterplot.draw(points);

invalidation?.then(() => scatterplot.destroy());

return canvas;
}
Insert cell
Insert cell
Insert cell
Insert cell
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