Published
Edited
Jan 27, 2021
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
import {vl} from '@vega/vega-lite-api'
Insert cell
d3 = require('d3')
Insert cell
import {printTable} from '@uwdata/data-utilities'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
1234132454+382479384732
Insert cell
Math.sqrt(121)
Insert cell
Math.random()
Insert cell
Insert cell
"hello "+"world"
Insert cell
"monkey"/4
Insert cell
"how long is this string?".length
Insert cell
"ALL LOWER CASE".toLowerCase()
Insert cell
Insert cell
num = 1
Insert cell
str = "1"
Insert cell
Insert cell
2<1
Insert cell
"a" < "b"
Insert cell
//== and != indicates implicit check while === and !== are explicit check. This way the operator compares the pure values.
1 == "1"
Insert cell
1==="1"
Insert cell
Insert cell
// AND operations
true && false
Insert cell
false && false
Insert cell
true && false
Insert cell
Insert cell
true || true
Insert cell
true || false
Insert cell
false || false
Insert cell
Insert cell
// putting the values in a bracket [] separated by commas create a datatype array
[1, 2, 3]
Insert cell
// arrays can consist of elements of different data types
arr = ["hello", 1, 4, 3, 2, "bye"]
Insert cell
// can index into the element in array
arr[1]
Insert cell
// -1 index doesn't work for javascript to access the last element
arr[-1]
Insert cell
arr[arr.length-1]
Insert cell
arr.indexOf(2)
Insert cell
//slicing from left, python equivalent of arr[2:]
arr.slice(2)
Insert cell
//slicing from right, python equivalent of arr[-2:]
arr.slice(-2)
Insert cell
//slicing from both sides, python equivalent of arr[2:4]
arr.slice(2,4)
Insert cell
//turn array into string
arr.join(' *** ')
Insert cell
//append new element at the end of the array
arr.concat('zzz')
Insert cell
//can also append a nested list
arr.concat([["a"], "b", "c"])
Insert cell
Insert cell
obj = ({key1: "apple", key2: "banana"})
Insert cell
obj.key1
Insert cell
//return an array of property values
Object.values(obj)
Insert cell
Object.keys(obj)
Insert cell
Insert cell
data = [
{fruit_name: "apple", calories_per_serving: 95, carb_amount_grams: 25},
{fruit_name: "banana", calories_per_serving: 105, carb_amount_grams: 27},
{fruit_name: "grape", calories_per_serving: 62, carb_amount_grams: 16}
]
Insert cell
data[0].fruit_name
Insert cell
//The map function above applies the function to each element in the array and returns the results as a new array.
data.map(d => d.fruit_name)
Insert cell
// The filter function evaluates a function for each element in the array, and returns only the element that satisfies the filtering criteria.
data.filter(d => d.fruit_name === "apple")
Insert cell
data.filter(d => d.calories_per_serving < 100 && d.carb_amount_grams < 25)
Insert cell
// => is an arrow function that shortens function exppressions
data.find(d => d.fruit_name === "apple").carb_amount_grams
Insert cell
Insert cell
function add_two(a, b) { // function signature: name and input arguments
return a + b; // function body, with return statement for output
}
Insert cell
add_two(1, 2)
Insert cell
add_two("a", "b")
Insert cell
function add_carbs(a,b) {
var a_carb = data.find(x => x.fruit_name === a).carb_amount_grams
var b_carb = data.find(x => x.fruit_name === b).carb_amount_grams
return a_carb + b_carb
}
Insert cell
add_carbs("apple", "banana")
Insert cell
Insert cell
Insert cell
fileContents = FileAttachment("example-1.csv")
Insert cell
//Parsing data into array
df = d3.csvParse(await fileContents.text(), d3.autoType)
Insert cell
printTable(df.slice(0, 20))
Insert cell
Insert cell
Insert cell
function co2(r, w) {
return df.find(x => x.Mode == "Train" && x.Route === r && x.WeeksAhead == w).TicketPrice
}
Insert cell
co2("London-Amsterdam",2)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
vl.markPoint()
.data(df)
.render()
Insert cell
Insert cell
Insert cell
vl.markPoint()
.data(df)
.encode(
vl.x().fieldQ("WeeksAhead"),
vl.y().fieldQ("TicketPrice")
)
.render()
Insert cell
Insert cell
vl.markPoint()
.data(df)
.encode(
vl.x().fieldQ("WeeksAhead"),
vl.y().fieldQ("TicketPrice"),
vl.color().fieldN("Route")
)
.render()
Insert cell
Insert cell
vl.markPoint()
.data(df)
.encode(
vl.x().fieldQ("WeeksAhead"),
vl.y().average("TicketPrice"),
vl.color().fieldN("Route")
)
.render()
Insert cell
Insert cell


vl.markPoint()
.data(df.filter(d => d.Mode === "Plane"))
.encode(
vl.x().fieldQ("WeeksAhead"),
vl.y().average("TicketPrice"),
vl.color().fieldN("Route")
)
.render()
Insert cell
Insert cell
vl.markLine()
.data(df.filter(d => d.Mode === "Plane"))
.encode(
vl.x().fieldQ("WeeksAhead"),
vl.y().average("TicketPrice"),
vl.color().fieldN("Route")
)
.render()
Insert cell
Insert cell
vl.markLine()
.data(df.filter(d => (d.Mode === "Plane" && d.Route === "Munich-Budapest")))
.encode(
vl.x().fieldQ("WeeksAhead"),
vl.y().average("TicketPrice"),
vl.color().fieldN("Route")
)
.render()
Insert cell
Insert cell
vl.markBar()
.data(df)
.encode(
vl.y().fieldN("Mode"),
vl.x().fieldQ("EcoPassengerCO2"),
vl.color().fieldN("Mode")
)
.render()
Insert cell
Insert cell
df.forEach(function (element) {
element.co2_per_min = element.EcoPassengerCO2 / element.RawTravelTime;
})
Insert cell
vl.markBar()
.data(df)
.encode(
vl.y().fieldN("Mode"),
vl.x().fieldQ("co2_per_min"),
vl.color().fieldN("Mode")
)
.title({
text: 'CO2 released per minute',
anchor: 'middle',
fontSize: 15
})
.render()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
vl.markPoint()
.data(df.filter(d => d.Mode === "Train"))
.encode(
vl.x().average("TicketPrice"),
//vl.x().fieldQ("WeeksAhead"),
vl.y().fieldN("Route"),
vl.color().fieldN("Route")
)
.render()
Insert cell
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