Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`Here is some Markdown. Edit me! You can add...
# Headers of
### different sizes,
* bullet
* points

\`fixed width font\`

1. Ordered
2. Lists,

and more. For reference, here is a [guide to formatting using the Markdown syntax](https://observablehq.com/@jaynel/markdown-summary).`
Insert cell
Insert cell
Here is some Markdown. Edit me! You can add...
# Headers of
### different sizes,
* bullet
* points

\`fixed width font\`

1. Ordered
2. Lists,

and more. For reference, here is a [guide to formatting using the Markdown syntax](https://observablehq.com/@jaynel/markdown-summary).
Insert cell
Insert cell
html`<h4>Here is some HTML</h4>`
Insert cell
<h4>Here is some HTML</h4>
Insert cell
{
const course = "CS448B";
const message = "Welcome to " + course;
return message;
}
Insert cell
{
const course = "CS448B";
const message = "Welcome to " + course;
return message;
}
Insert cell
message = "I am taking " + course;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
currentWeek = 3
Insert cell
"There are " + (10 - currentWeek) + " weeks left in the quarter"
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
442 // a number
Insert cell
"Data Visualization"// a string
Insert cell
true // a boolean
Insert cell
isNaN(442) // you may use isNaN(a) to check if a is Not-A-Number
Insert cell
(1 + Math.sqrt(5)) / 2 // you can also do math
Insert cell
Math.random() // or generate 'random' numbers
Insert cell
2 < 1 // you can compare things
Insert cell
"A" < "a" // including string comparisons
Insert cell
2 + 2 === 4 // more comparison
Insert cell
2 + 2 !== "4" // and more
Insert cell
Insert cell
2 + 2 == "4" // double equals
Insert cell
2 + 2 === "4" // triple equals
Insert cell
Insert cell
Insert cell
date = new Date()
Insert cell
Insert cell
Insert cell
Insert cell
function addMe(a, b) { // function signature: name and input arguments
return a + b; // function body, with return statement for output
}
Insert cell
Insert cell
addMe(2, 2)
Insert cell
addMe('data', ' visualization')
Insert cell
addMe('data', ' visualization', 'CS448B') //extra arguments are ignored!
Insert cell
Insert cell
addMe('1') // something from nothing
Insert cell
Insert cell
addMe2 = (a, b) => a + b
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
arr1 = [1, 2, 3, 'hi'] // you can initialize an array like this
Insert cell
{
arr1.push('Husky'); // you can push elements to array, this updates the array content
return arr1;
}
Insert cell
Insert cell
Insert cell
arr1.map(d => d * 2)
Insert cell
Insert cell
result = arr1.filter(d => !isNaN(d)).map(d => d * 2)
// keep valid numbers only, then double them
Insert cell
result.slice().sort() // the default sort method is ascending
Insert cell
result
.slice() // you can first make a copy of an array, so we don't overwrite earlier values
.sort((a, b) => b - a) // you can define a custom sort comparator; here we sort numbers in descending order
Insert cell
result
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
object['key']['key2']
Insert cell
object['newKey'] = 'new value'
Insert cell
object
Insert cell
Object.keys(object) // return an array of property key strings
Insert cell
Object.values(object) // return an array of property values
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
csvText = FileAttachment("sample1-1@3.csv").text() // loading a text file attached in Observable
Insert cell
dataCSV = d3.csvParse(csvText) // if we have the text loaded already, we can use d3.csvParse
Insert cell
csvURL = FileAttachment("sample1-1@3.csv").url() // you can get the url of a file you attached to Observable with .url()
Insert cell
dataCSV2 = d3.csv(csvURL) // if we have a URL to a CSV, we can use d3.csv to both load and parse
Insert cell
Insert cell
auto = d3.csv(csvURL, d3.autoType) // can handle numbers and booleans, but not all date formats
Insert cell
manual = d3.csv(csvURL, ({Name, Job, date}) => ({Name: Name, Job: Job, 'OH Date': new Date(date)}))
Insert cell
Insert cell
datee = manual[1]['OH Date'].getDay()
Insert cell
Insert cell
Insert cell
FileAttachment("sample1@1.json").text() // here's what JSON data looks like as text
Insert cell
dataJSON = FileAttachment("sample1@1.json").json() // load and parse JSON from a file attached in Observable
Insert cell
json_url = FileAttachment("sample1@1.json").url() // we can also get a URL for a file attachment
Insert cell
Insert cell
Insert cell
strJson = JSON.stringify(dataJSON)
Insert cell
JSON.parse(strJson)
Insert cell
Insert cell
Insert cell
Insert cell
// your code goes here
// uncomment cell below to see the solution
// .filter() ; .sort(+, 0, -)
data.filter(d => d['Job'] !== "Instructor").sort((a,b) => {
if (a['Name'] > b['Name']) {
return 1;
}
else if (a['Name'] < b['Name']) {
return -1;
}
else {
return 0;
}
})
Insert cell
Insert cell
Insert cell
// your code goes here
data.filter(d => d['Job'] === 'Course Assistant' && (d['OH Date'].getDay() === 2 || d['OH Date'].getDay() === 3))
// uncomment cell below to see the solution
Insert cell
data.filter(d => d['Job'] === 'Instructor' && d['OH Date'].getDay() === 1)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cars = (await require('vega-datasets@1'))['cars.json']()
Insert cell
Insert cell
printTable(cars.slice(0,10))
Insert cell
Insert cell
Insert cell
cars_table = aq.from(cars)
Insert cell
cars_table.view()
Insert cell
Insert cell
// Your code goes here:
// Hints:
// op.upper(): Returns the string value converted to upper case.
// op.includes(array, value): Determines whether an array includes a certain value among its entries,
// returning true or false as appropriate.
// Uncomment the code below to see the solution!
cars_table.filter(d => op.includes(d.Origin, "USA")).view()

Insert cell
Insert cell
Insert cell
// Your code goes here:
// Uncomment the code below to see the solution!
cars_table.filter(d => op.includes(d.Origin, "USA")).orderby('Miles_per_Gallon').view()
Insert cell
Insert cell
Insert cell
// Your code goes here:
// filter for cars only made from USA, sort in decending order, and select a subset of collumn to show for 5 rows only.
// Hint: Use aq.desc(field)!
cars_table.filter(d => op.includes(d.Origin, "USA")).orderby(aq.desc('Miles_per_Gallon')).view(5)

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cars_table
.groupby('Origin')
.rollup({
count: d => op.count(),
med_cylinders: d => op.median(d.Cylinders),
min_year: d => op.min(op.year(d.Year)),
max_year: d => op.max(op.year(d.Year)),
avg_mpg: d => op.mean(d.Miles_per_Gallon),
avg_displacement: d => op.mean(d.Displacement)
/**
* Insert your code here!!
*/
})
.orderby(aq.desc('count'))
.view()
Insert cell
Insert cell
Insert cell
//Your code here
cars_table.derive({weight_in_kg: d => d.Weight_in_lbs * 0.453592}).view()
Insert cell
Insert cell
Insert cell
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