Published
Edited
Jan 27, 2021
1 fork
2 stars
Insert cell
Insert cell
Insert cell
md`## Imports

First, we need to import software libraries - vega-lite, d3, and printTable utility.

Remember that each cell supports only one executable statement, so you have to enter the two import lines into two separate cells.`
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
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.
num == str
Insert cell
num===str
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 removes an element when the return value is not truthy
data.filter(d => d.fruit_name === "apple")
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
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
//Your code here
Insert cell
Insert cell
md`
In this exercise, we will learn how to create basic data visualizations using vega-lite. We will learn markPoint, markBar, and markLine on how each can be used to represent different aspects of information.
`
Insert cell
vl.markPoint()
.data(df)
.render()
Insert cell
Insert cell
vl.markPoint()
.data(df)
.encode(
vl.x().fieldQ("WeeksAhead"),
vl.y().fieldQ("TicketPrice")
)
.render()
Insert cell
vl.markPoint()
.data(df)
.encode(
vl.x().fieldQ("WeeksAhead"),
vl.y().fieldQ("TicketPrice"),
vl.color().fieldN("Route")
)
.render()
Insert cell
vl.markPoint()
.data(df)
.encode(
vl.x().fieldQ("WeeksAhead"),
vl.y().average("TicketPrice"),
vl.color().fieldN("Route")
)
.render()
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
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
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
vl.markBar()
.data(df)
.encode(
vl.y().fieldN("Mode"),
vl.x().fieldQ("EcoPassengerCO2"),
vl.color().fieldN("Mode")
)
.render()
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
//Your code here
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