Published
Edited
Feb 20, 2019
Insert cell
Insert cell
Insert cell
import {ramp} from "@mbostock/color-ramp" //this "from" uses last part of MBostock's Observables URL for this particular ramp page. needs to be published for a bit before import works on it...
Insert cell
ramp(t => `hsl(${t * 360}, 100%, 50%)`)
Insert cell
ramp(d3.interpolateLab("red","blue"))
Insert cell
import {multiply_this} from '@cesandoval/week-2a-introduction-to-javascript-part2/2'
Insert cell
Insert cell
Insert cell
Insert cell
import {canvas as map} from "@mbostock/interrupted-sinu-mollweide"
Insert cell
map
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {interval} from "@mbostock/countdown"
Insert cell
interval
Insert cell
Insert cell
//i
//reactive, notices import below and runs
Insert cell
import {i} from "@mbostock/generator-cells-functions-and-objects"
Insert cell
Insert cell
import {canvas as map2} from "@mbostock/interrupted-sinu-mollweide"
Insert cell
map === map2
Insert cell
Insert cell
Insert cell
primes = [2, 3, 5, 7, 11, 13]
Insert cell
Insert cell
mtcars = d3.csvParse(`name,mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb
Mazda RX4,21,6,160,110,3.9,2.62,16.46,0,1,4,4
Mazda RX4 Wag,21,6,160,110,3.9,2.875,17.02,0,1,4,4
Datsun 710,22.8,4,108,93,3.85,2.32,18.61,1,1,4,1
Hornet 4 Drive,21.4,6,258,110,3.08,3.215,19.44,1,0,3,1
Hornet Sportabout,18.7,8,360,175,3.15,3.44,17.02,0,0,3,2
Valiant,18.1,6,225,105,2.76,3.46,20.22,1,0,3,1
Duster 360,14.3,8,360,245,3.21,3.57,15.84,0,0,3,4
Merc 240D,24.4,4,146.7,62,3.69,3.19,20,1,0,4,2
`)
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
md`The first prime is ${primes[0]}.`
Insert cell
md`The first car is a ${mtcars[0].name} with ${mtcars[0].hp} horsepower.`
Insert cell
d3.median(mtcars, d => d.mpg) // The median miles per gallon.
Insert cell
Insert cell
flare = d3.csv("https://gist.githubusercontent.com/mbostock/4063570/raw/11847750012dfe5351ee1eb290d2a254a67051d0/flare.csv")
Insert cell
Insert cell
aapl = require("@observablehq/aapl@0")
Insert cell
Insert cell
DOM.download(
new Blob([d3.csvFormat(flare)], {type: "text/csv"}),
"flare.csv"
)
Insert cell
Insert cell
Insert cell
viewof image = html`<input type=file accept="image/*">`
Insert cell
Insert cell
html`<img src="${URL.createObjectURL(image)}">`
Insert cell
Insert cell
Files
//we'll learn how to keep info private later as passwords, etc must be kept private
Insert cell
Insert cell
viewof text = html`<input type=file accept="text/* ">`
Insert cell
Files.text(text)
Insert cell
Insert cell
viewof binary = html`<input type=file>`
Insert cell
Files.buffer(binary)
Insert cell
Insert cell
data = d3.csv("https://gist.githubusercontent.com/mbostock/ddc6d50c313ebe6edb45519f43358c6c/raw/c443ed14c34c5c1b544949a546dd9d0acd05bad3/temperatures.csv")
Insert cell
Insert cell
data.map(d => +d.Year)
Insert cell
Insert cell
Promises
Insert cell
Insert cell
Promises.delay(3000, "Hello, world!")
Insert cell
Insert cell
{
let i = 0;
while (true) {
yield Promises.tick(1000, ++i);
}
}
Insert cell
Insert cell
{
let greeting = await Promises.delay(3000, "Hello, ");
return greeting + "world!";
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
yield 13
Insert cell
Insert cell
13
Insert cell
Insert cell
{
let i = 0;
while (true) {
yield Promises.delay(1000, ++i);
}
}
Insert cell
Insert cell
{
let i = 0;
while (true) {
await Promises.delay(1000);
yield ++i;
}
}
Insert cell
Insert cell
{
const div = html`<div style="transition:background 250ms linear;height:33px;">`;
yield div;
while (true) {
div.style.background = "red";
yield Promises.delay(1000, div);
div.style.background = "green";
yield Promises.delay(1000, div);
div.style.background = "blue";
yield Promises.delay(1000, div);
}
}
Insert cell
Insert cell
{
yield 1;
yield 2;
yield 3;
return "ignored"; // It’s ignored!
}
Insert cell
Insert cell
input = html`<input type=range>`
Insert cell
Generators.observe(next => {
// Yield the input’s initial value.
next(input.value);

// Define an event listener to yield the input’s next value.
const inputted = () => next(input.value);

// Attach the event listener.
input.addEventListener("input", inputted);

// When the generator is disposed, detach the event listener.
return () => input.removeEventListener("input", inputted);
})
Insert cell
Insert cell
Generators.input(input)
Insert cell
Insert cell
viewof value = html`<input type=range>`
Insert cell
Insert cell
width
Insert cell
Insert cell
now
Insert cell
Insert cell
viewof x = html`<input type=range min=0 max=1 step=any>`
Insert cell
x
Insert cell
Insert cell
viewof message = html`<input type=text placeholder="Say hello">`
Insert cell
message
Insert cell
Insert cell
viewof color = html`<select>
<option>red
<option>orange
<option>yellow
<option selected>green
<option>blue
<option>violet
</select>`
Insert cell
color
Insert cell
Insert cell
Insert cell
point
Insert cell
Insert cell
viewof silly = {
const element = html`<div>I am a silly view!</div>`;
element.value = "I am a silly value.";
return element;
}
Insert cell
silly
Insert cell
Insert cell
viewof count = {
const element = html`<div style="font-size:64px;user-select:none;">🤪</div>`;
element.value = 0;
element.onclick = () => {
++element.value;
element.dispatchEvent(new CustomEvent("input"));
};
return element;
}
Insert cell
count
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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