Public
Edited
Sep 12, 2023
Insert cell
Insert cell
class BumpRadial {
constructor(context) {
this._context = context;
}
lineStart() {
this._point = 0;
}
lineEnd() {}
point(x, y) {
x = +x, y = +y;
if (this._point === 0) {
this._point = 1;
} else {
//x=theta,y=r

const p0 = pointRadial(this._x0, this._y0);
const p1 = pointRadial(x,y);

this._context.moveTo(...p0);
// console.group()
// console.log(this._y0)
// console.log(this._x0)
// console.log(x,y)
// console.groupEnd()
this._context.arc(0,0,this._y0,this._x0,x)
this._context.lineTo(...p1)

}
this._x0 = x, this._y0 = y;
}
}
Insert cell
function bumpRadial(context) {
return new BumpRadial(context);
}
Insert cell
pointRadial = function(theta, r) {
return [(r = +r) * Math.cos(theta), r * Math.sin(theta)];
}
Insert cell
pointRadial(100,Math.Pi)

Insert cell
function linkRadial() {
const l = d3.link(bumpRadial);
l.angle = l.x, delete l.x;
l.radius = l.y, delete l.y;
return l;
}
Insert cell
linker=linkRadial().angle(d=>d.theta).radius(d=>d.r)
Insert cell
function linkStep(startAngle, startRadius, endAngle, endRadius) {
const c0 = Math.cos(startAngle = (startAngle - 90) / 180 * Math.PI);
const s0 = Math.sin(startAngle);
const c1 = Math.cos(endAngle = (endAngle - 90) / 180 * Math.PI);
const s1 = Math.sin(endAngle);
return "M" + startRadius * c0 + "," + startRadius * s0
+ (endAngle === startAngle ? "" : "A" + startRadius + "," + startRadius + " 0 0 " + (endAngle > startAngle ? 1 : 0) + " " + startRadius * c1 + "," + startRadius * s1)
+ "L" + endRadius * c1 + "," + endRadius * s1;
}
Insert cell
svg`<svg width=600 height=600><path d="${linker(data[0])}" fill="none" stroke="black" transform="translate(150,150)"></svg>`
Insert cell
c = d3.path()
Insert cell
data = [{source:{r:100,theta:3/2*Math.PI},target:{r:200,theta:Math.PI/2}},{source:{r:6,theta:0},target:{r:5,theta:1}}]
Insert cell
function getLinker(data){
const c= d3.path();
const center = [0,0];
c.moveTo(data.source.r*Math.cos(data.source.theta),data.source.r*Math.sin(data.source.theta))
c.arc(center[0],center[1],data.source.r,data.source.theta,data.target.theta)
c.lineTo(data.target.r*Math.cos(data.target.theta),data.target.r*Math.sin(data.target.theta))
return c.toString()
}
Insert cell
getLinker(data[0])
Insert cell
data[0]
Insert cell
linkStep(data[0].source.theta*180/Math.PI,data[0].source.r,data[0].target.theta*180/Math.PI,data[0].target.r)
Insert cell
linker(data[0])
Insert cell
class BumpRectangular {
constructor(context,c ) {
this._context = context;
this._c = c;
this._point = 0;
this._line = 0;

}
areaStart() {
this._line = 0;
}
areaEnd() {
this._line = NaN;
}
lineStart() {
this._point = 0;
}
lineEnd() {
if (this._line || (this._line !== 0 && this._point === 1)) this._context.closePath();
this._line = 1 - this._line;
}

// x and y are the end and x0 and y0 the start
point(x, y) {
// let _x = +x, _y = +y;
if (this._point === 0) {
this._point = 1;
} else {

if (this._c===0){ // no curve
var x1 = this._x0 +0.001 ;// tiny adjustment for faded line (can't have y or x dimension not change at all
this._context.moveTo(x1,this._y0);
this._context.lineTo(x1, y); //draws a straight line vertically
this._context.lineTo(x, y+0.001); // draws a straight line horizontally
}else if(this._c<1){
// curve
this._context.moveTo(this._x0,this._y0);
this._context.bezierCurveTo(this._x0,y, this._x0+Math.abs(this._c*(this._x0-x)), y, x, y);

} else if (this._c==1){
this._context.moveTo(this._x0,this._y0); // go to start
this._context.lineTo(x, y);// straight line to end
}
}
this._x0 = x;
this._y0 = y;
}
}


Insert cell
bumpRectangular=(c) =>(context) => new BumpRectangular(context,c);
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