Published
Edited
May 31, 2020
Insert cell
Insert cell
d3.range(10)
Insert cell
d3.range(10).map(d=>d*2)
Insert cell
d3.range(10).map(d=>({x:d,y:d*2}))
Insert cell
Math.floor(Math.random() * 100)
Insert cell
Insert cell
Math.random()
Insert cell
Math.random() * 10000
Insert cell
Math.floor(Math.random() * 10000)
Insert cell
line1 = { yield d3.range(10).map(d=>({x:d,y:Math.random() * 10000})) }

Insert cell
line2 = { yield d3.range(10).map(d=>({x:d,y:Math.random() * 10000})) }
Insert cell
line3 = { yield d3.range(10).map(d=>({x:d,y:Math.random() * 10000})) }
Insert cell
line4 = { yield d3.range(10).map(d=>({x:d,y:Math.random() * 10000})) }
Insert cell
Insert cell
anArray = []
Insert cell
anArray.push(20)
Insert cell
anArray
Insert cell
aDifferentArray = [[0,[23]],[0,100],[10,100]]
Insert cell
aDifferentArray
Insert cell
collectLines = [line1,line2,line3,line4]
Insert cell
Insert cell
collectLines.map(d=>d.map(e=>e.x))
Insert cell
allXvalues = { yield d3.merge(collectLines.map(d=>d.map(e=>e.x))) }
Insert cell
collectLines.map(d=>d.map(e=>e.y))
Insert cell
d3.merge([[1,2,3],[4,5,6],[7,8,9]])
Insert cell
allYvalues = { yield d3.merge(collectLines.map(d=>d.map(e=>e.y))) }
Insert cell
Insert cell
Insert cell
{
const svg = d3.select(DOM.svg(width, height))
var color = d3.scaleLinear().range([0,1]).domain([0,collectLines.length]);
var palette = d3.interpolatePlasma; // https://github.com/d3/d3-scale-chromatic
const margin = 35
const x = d3.scaleLinear().range([margin,width-margin]).domain(d3.extent(allXvalues))
const y = d3.scaleLinear().range([height-margin,margin]).domain(d3.extent(allYvalues))
svg
.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (height-margin) + ")")
.call(d3.axisBottom(x));
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate("+ (margin) + ",0)")
.call(d3.axisLeft(y));
const line = d3.line()
.x(d=>x(d.x))
.y(d=>y(d.y))
.curve(d3.curveLinear)
svg.selectAll("path.line").data(collectLines).enter().append("path")
.attr("class", "line")
.attr("d", d=> line(d))
.style("stroke",(d,i)=>palette(color(i)))
.style("stroke-width","1px")
.style("fill","none")
/* const g = svg.selectAll("g.points").data(collectLines).enter().append("g")
.attr("class", "points")
.style("stroke",(d,i)=>palette(color(i)))
.style("stroke-width","1px")
.style("fill",(d,i)=>palette(color(i)))
g.selectAll("circle.points").data(d=>d).enter().append("circle")
.attr("cx",d=>x(d.x))
.attr("cy",d=>y(d.y))
.attr("r",5)
.append("title")
.text(d=>"x: "+d.x+" y: "+d.y)*/

yield svg.node();
}
Insert cell
d3 = require("d3@5", "d3-random@2")
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