Public
Edited
Apr 11, 2023
1 fork
Importers
Insert cell
Insert cell
Insert cell
toNum = (text) => {
if (typeof(text) === "string") {return Number(text.replace(/,/g,''))}
else {return text}
}
Insert cell
spaceToUnderscore = (text) => {
if (typeof(text) == "string") {text = text.replace(/ /g,'_');}
return text;
}
Insert cell
rFromArea = (area) => {
if (typeof(area) == "string") {area = toNum(area)} // convert to a number if given a string.
return Math.sqrt(area/Math.PI); // get radius from an area value. A = PI * r^2
}
Insert cell
whFromRectArea = (area,aspect) => {
area = toNum(area);
aspect = toNum(aspect);
if (!aspect) {aspect = 1;}
let h = Math.sqrt(area/aspect);
let w = aspect * h;
return {"width":w, "height":h};
}
Insert cell
radiansFromDeg = (deg) => {
deg = toNum(deg);
return deg * Math.PI / 180;
}
Insert cell
degFromRadians = (rad) => {
rad = toNum(rad);
return rad * 180 / Math.PI;
}
Insert cell
polarXY = (magnitude,angle) => {
magnitude = toNum(magnitude);
angle = toNum(angle);
let x = magnitude * Math.cos(radiansFromDeg(angle));
let y = magnitude * Math.sin(radiansFromDeg(angle));
return {"x":x, "y":y};
}

// can be replaced by d3.pointRadial

Insert cell
// get center of an element
centerXY = (elem) => {
let bbox = elem.node().getBBox();
return [bbox.x + bbox.width/2, bbox.y + bbox.height/2];
}
Insert cell
ticker = function* (tickMS) {
let i=0;
while (true) {
yield Promises.delay(tickMS, i++);
}
}
Insert cell
colorByID = (objectSet,data,objectIDfield,dataIDfield,dataValueField,extents,colorScale) => {
let minValue = (typeof extents === 'undefined') ? d3.min(data, d => toNum(d[dataValueField])) : extents[0];
let maxValue = (typeof extents === 'undefined') ? d3.max(data, d => toNum(d[dataValueField])) : extents[1];
if (!colorScale) {colorScale = d3.scaleLinear().domain([minValue, maxValue]).range(["white","black"])};

data.forEach(d => {
objectSet.filter(o=>o[objectIDfield] == d[dataIDfield])
.style("fill",colorScale(toNum(d[dataValueField])))
})
}
Insert cell
importSVGnode = (svgText) => {
const document = (new DOMParser).parseFromString(svgText, "image/svg+xml");
const svg = d3.select(document.documentElement).remove();
return svg.node();
}
Insert cell
function getProperty( propertyName, object ) {
var parts = propertyName.split( "." ),
length = parts.length,
i,
property = object || this;

for ( i = 0; i < length; i++ ) {
property = property[parts[i]];
}

return property;
}

// gets a nested.dot.notation property from an object
// effectively converts nested.dot.notation... into ["nested"]["dot"]["notation"]...
// from https://gist.github.com/jasonrhodes/2321581
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