Published
Edited
Apr 8, 2022
Importers
2 stars
Insert cell
Insert cell
Insert cell
// return offsets found by splitting an array length into sub-arrays with almost equal lengths
function getSampledOffsets(arrayLength, n) {
return d3.range(n).map((i) => Math.round((i / (n - 1)) * (arrayLength - 1)));
}
Insert cell
// return n samples from array
function getSampledValues(array, n){
return getSampledOffsets(array.length, n).map(o => array[o])
}
Insert cell
Insert cell
getSampledOffsets(256,12)
Insert cell
dotChart(getSampledValues(infernoColors,12))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function getPatchOffsets (width, height, n) {
let ggvalues = []
const xs = getSampledOffsets(width, n)
const ys = getSampledOffsets(height, n)
ys.forEach((row,i) => xs.forEach((col, j) => {
if(i !== 0 && j !== 0){
ggvalues.push({x0: xs[j-1], x1: col,
y0: ys[i-1], y1: row})
}
}))
return ggvalues
}
Insert cell
getPatchOffsets (100, 150, 4)
Insert cell
getSampledOffsets(100, 4)
Insert cell
Insert cell
function gridSamples (n) {
const tw = 1/n;
const gvalues = d3.range(n).map(i => i * tw + tw/2)
let ggvalues = []
gvalues.forEach(i => gvalues.forEach(j => ggvalues.push({x:i,y:j})))
return ggvalues
}
Insert cell
gridSamples(5)
Insert cell
Insert cell
function getRandomSubarray(arr, size) {
const shuffled = arr.slice(0)
let i = arr.length, min = i - size, temp, index;
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(min);
}
Insert cell
getRandomSubarray(d3.range(300), 10)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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