Published
Edited
Dec 8, 2019
Importers
2 stars
Insert cell
Insert cell
Insert cell
{
const w = width / 2;
const h = width / 2;
const padding = 30;

const pointNum = 500;

let svg = d3.select(DOM.svg(w, h));

let xScale = d3.scaleLinear([0, 1], [padding, w - padding]);
let yScale = d3.scaleLinear([1, 0], [padding, h - padding]);

let clipPath = svg
.append('clipPath')
.attr('id', 'chart-area')
.append('rect')
.attr('x', padding)
.attr('y', padding)
.attr('width', w - 2 * padding)
.attr('height', h - 2 * padding);

const curves = [];
for (let i = 0; i <= n; ++i) {
const bi_4 = [];
for (let t = 0; t <= pointNum; t++) {
bi_4.push([t / pointNum, bernstein_poly(i, n, t / pointNum)]);
}
curves.push(bi_4);
}

let line_log = d3
.line()
.x(d => xScale(d[0]))
.y(d => yScale(d[1]));

for (let curve of curves) {
svg
.append('path')
.datum(curve)
.attr("clip-path", "url(#chart-area)")
.attr('fill', 'none')
.attr('stroke', randomColor())
.attr('stroke-width', 2)
.attr('d', line_log);
}

let xAxis = d3.axisBottom(xScale);
let yAxis = d3.axisLeft(yScale);

svg
.append('g')
.attr('transform', `translate(0,${h - padding})`)
.call(xAxis);
svg
.append('g')
.attr('transform', `translate(${padding},0)`)
.call(yAxis);

return svg.node();
}
Insert cell
function factorial(n) {
let res = BigInt(1);
for (let i = 1n; i <= n; i += 1n) res = res * i;
return res;
}
Insert cell
function binomial(n, k) {
return Number(factorial(n) / (factorial(k) * factorial(n - k)));
}
Insert cell
function bernstein_poly(i, n, t) {
return binomial(n, i) * t ** i * (1 - t) ** (n - i);
}
Insert cell
function randomColor() {
const color = [
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256)
].join(',');
return `rgb(${color})`;
}
Insert cell
import { slider } from '@jashkenas/inputs'
Insert cell
d3 = require('d3')
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