Public
Edited
Jan 23, 2023
Insert cell
Insert cell
data_normal = Array.from({length: 20000}, d3.randomNormal(1, 1))
Insert cell
bin = d3.bin().thresholds(50)
Insert cell
data = bin(data_normal)
Insert cell
histogram(data)
Insert cell
histogram = (data, w=400, h=200, g) => {

const svg =
g ||
d3
.create("svg")
.attr("viewBox", [0, 0, w, h])
.attr("width", w)
.attr("height", h);

let margin = { top: 10, right: 10, bottom: 10, left: 50 };

let width = w - margin.left - margin.right,
height = h - margin.top - margin.bottom;
const x = d3.scaleLinear()
.domain([data[0].x0, data[data.length - 1].x1])
.range([margin.left, width]);

const y = d3.scaleLinear()
.domain([0, d3.max(data.map(d => d.length))])
.range([height + margin.top, margin.top]);

svg.selectAll('rect.hist').data(data).enter().append('rect')
.attr('class', 'hist')
.attr('x', d => x(d.x0))
.attr('y', d => y(d.length))
.attr('height', d => height - y(d.length))
.attr('width', d => x(d.x1) - x(d.x0))

let xAxis = d3.axisBottom()
.scale(x);

let yAxis = d3.axisLeft().scale(y)

svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
return svg.node();
}
Insert cell
bar_chart()
Insert cell
bar_chart = (w=200, h=100, g, dataset) => {
const svg =
g ||
d3
.create("svg")
.attr("viewBox", [0, 0, w, h])
.attr("width", w)
.attr("height", h);

let margin = { top: 10, right: 10, bottom: 10, left: 10 };

let width = w - margin.left - margin.right,
height = h - margin.top - margin.bottom;

let data = dataset ||
[22, 32, 21, 23, 10, 22, 11, 19, 30, 50, 19, 30, 50, 19];

let y = d3
.scaleLinear()
.domain([0, d3.max(data)])
.range([0, height]);

let x = d3
.scaleBand()
.padding(0.1)
.domain(data)
.range([0, width]);

var s = svg
.selectAll("rect")
.data(data)
.enter()
.append("rect");

svg
.selectAll("rect").data(data)
.attr("fill", "white")
.attr("stroke", "black")
.attr("stroke-width", 1)
.attr("width", () => x.bandwidth())
.attr("height", (d, i) => y(d))
.attr("x", function(d, i) {
return x(d);
})
.attr("y", function(d) {
return height - y(d);
});

return svg.node();
}
Insert cell
data_normal_cumsum = d3.cumsum(data_normal)
Insert cell
Plot.line(data_normal_cumsum, {x: (d, i) => i, y: (d, i) => d}).plot()
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