Published
Edited
Jun 18, 2020
Insert cell
Insert cell
radius = 12
Insert cell
area = Math.PI * Math.pow(radius, 2);
Insert cell
Insert cell
Insert cell
visualChannels.length
Insert cell
{
let a = [1,2,3]
let b = [4,6,6]
return a.concat(b)
}
Insert cell
apple = ({
name: 1,
age: 2
})
Insert cell
apple.age
Insert cell
{
let fruit = {
apple: 1,
banana: 2
}
return fruit.banana
}
Insert cell
convert = (num)=>{
return num.value
}
Insert cell
function convert2(num){
return num.value;
}
Insert cell
{
let a = {
value: 1
}
return convert2(a)
}
Insert cell
function getDataFromServer(num, callback) {
setTimeout(function(){
return callback(num);
}, 0)
}
Insert cell
mutable num = 2
Insert cell
getDataFromServer(2, function (num){
mutable num = 3
})
Insert cell
d3 = require('d3@5')
Insert cell
mutable i = null;
Insert cell
a = {
while(true) yield Promises.delay(1000, mutable i += 1);
}
Insert cell
html`
<svg width="120" height="120">
<line x1=10 y1 = 50 x2=100 y2=100 stroke="black">
</svg>
`
Insert cell
linearScale = d3.scaleLinear().range([0,100]).domain([0, 40])
Insert cell
[10, 20, 30, 50].map(item => linearScale(item))
Insert cell
{
const height = 60;
const svg = d3.select(DOM.svg(width, height));
const margin = {left: 30, top: 10, right: 10, bottom: 30};
const xScale = d3.scaleLinear()
.range([margin.left, width-margin.right])
.domain([0, 40])
svg.append('g').call(d3.axisBottom(xScale))
return svg.node()
}
Insert cell
{
const height = 600;
const svg = d3.select(DOM.svg(60, height));
const margin = {left: 30, top: 10, right: 10, bottom: 30}
const yScale = d3.scaleLinear()
.range([height-margin.bottom, margin.top])
.domain([0, 40])
svg.append('g').call(d3.axisLeft(yScale)).attr('transform', `translate(${margin.left}, 0)`);
return svg.node()
}
Insert cell
{
const height = 60
const svg = d3.select(DOM.svg(width, height))
const margin = {left: 30, top: 10, right: 10, bottom: 20 }

const xScale = d3.scaleLinear()
.range([margin.left, width-margin.right])
.domain([0, 40])
svg.append('g')
.call(d3.axisBottom(xScale))
.attr('transform', `translate(0, ${height-margin.bottom})`)
svg.selectAll('circle')
.data([10, 20, 30, 40])
.enter()
.append('circle')
.attr('cx', d => xScale(d))
.attr('cy', 20)
.attr('r', 10)
.attr('fill', 'SteelBlue')
return svg.node()
}
Insert cell
{
const height = 600
const svg = d3.select(DOM.svg(width, height))
const margin = {left: 30, top: 10, right: 10, bottom: 20 }

const yScale = d3.scaleLinear()
.range([height-margin.bottom, margin.top])
.domain([0, 40])
svg.append('g')
.call(d3.axisLeft(yScale))
.attr('transform', `translate(${margin.left}, 0)`)
svg.selectAll('circle')
.data([10, 20, 30, 40])
.enter()
.append('circle')
.attr('cy', d => yScale(d))
.attr('cx', 50)
.attr('r', 10)
.attr('fill', 'SteelBlue')
return svg.node()
}
Insert cell
exponentialData = {
let exponentialData = []
for(let i=0; i<= 4; i++){
exponentialData.push({
x: i,
y: 5*Math.pow(2, i)
})
}
yield exponentialData
}
Insert cell
d3.extent(exponentialData.map(data => data.x))
Insert cell
d3.extent(exponentialData.map(data => data.y))
Insert cell
{
const height = 600
const svg = d3.select(DOM.svg(width, height))
const margin = {left: 30, top: 10, right: 10, bottom:20 }
const xScale = d3.scaleLinear()
.range([margin.left, width-margin.right])
.domain(d3.extent(exponentialData.map(data => data.x)))
const yScale = d3.scaleLinear()
.range([height-margin.bottom, margin.top])
.domain(d3.extent(exponentialData.map(data => data.y)))
svg.append('g')
.call(d3.axisBottom(xScale))
.attr('transform',`translate(0, ${height-margin.bottom})`)
svg.append('g')
.call(d3.axisLeft(yScale))
.attr('transform', `translate(${margin.left},0)`)
svg.selectAll('circle')
.data(exponentialData)
.enter()
.append('circle')
.attr('cx', d=>xScale(d.x))
.attr('cy', d=>yScale(d.y))
.attr('r', 10)
.attr('fill', 'SteelBlue')
return svg.node()
}
Insert cell
{const line = d3.line()
.x(d => d.x)
.y(d => d.y)
return line([{x:3,y:4}])
}
Insert cell
lineData = [[1,2]]
Insert cell
{
let lineGenerator = d3.line()
return lineGenerator(lineData);
}
Insert cell
d3.line().x(d=>d[0]).y(d=>d[1])(lineData)
Insert cell
{
const exponentialData = []
for(let i=0; i<= 4; i++){
exponentialData.push({
x: i,
y: Math.pow(2, i)
})
}
const height = 400
const svg = d3.select(DOM.svg(width, height))
const margin = {left: 30, top: 10, right: 10, bottom:20}
const xScale = d3.scaleLinear()
.range([margin.left, width-margin.right])
.domain(d3.extent(exponentialData.map(item=>item.x)))
const yScale = d3.scaleLinear()
.range([height-margin.bottom, margin.top])
.domain(d3.extent(exponentialData.map(item=>item.y)))

const line = d3.line()
.x(d=>xScale(d.x))
.y(d=>yScale(d.y))
svg.append('g')
.call(d3.axisBottom(xScale))
.attr('transform', `translate(0, ${height-margin.bottom})`)
svg.append('g')
.call(d3.axisLeft(yScale))
.attr('transform', `translate(${margin.left}, 0)`)
svg.append('g')
.selectAll('path')
.data([exponentialData])
.enter()
.append('path')
.attr('d', d=>line(d))
.attr('fill', 'none')
.attr('stroke', 'SteelBlue')
return svg.node()
}
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