Published
Edited
Nov 9, 2021
Insert cell
# Getting started
Insert cell
Numbers
### Numbers

Insert cell
a = 3
Insert cell
b = 6
Insert cell
a * b
Insert cell
## Strings
Insert cell
c = "Hello"
Insert cell
d = "World"
Insert cell
m = c + " " + d
Insert cell
## Function
Insert cell
m.toUpperCase ()
Insert cell
// Create my own function
function multiply(a, b) {
return a * b;
}
Insert cell
multiply(2, 3)
Insert cell
## Array[]
Insert cell
sales = [5, 25, 10, 20, 15]
Insert cell
// First Number
sales[0]
Insert cell
## Object
Insert cell
people = ({ name: "Akshita", handle: "@aksh" })
Insert cell
people.name
Insert cell
people.handle
Insert cell
Object.keys(people)
Insert cell
Object.values(people)
Insert cell
## Data:Table Structure

- Column Way: Object of Arrays
- Key Names items are Column Names
- Values are an Array of the Column Values
- Row Way: Array of Objects
- Array items are row
- Object is ColumnHeader: Value
Insert cell
// Column Way
dataColumns = ({
area: ["north", "east", "west", "south", "central"],
sales: [5, 15, 10, 20, 15],
profit: [2, 8, 6, 5, 3]
})
Insert cell
// Row Way
dataRow = [
{ area: "north", sales: 5, profit: 2 },
{ area: "east", sales: 25, profit: 8 },
{ area: "west", sales: 10, profit: 6 },
{ area: "south", sales: 20, profit: 5 },
{ area: "central", sales: 15, profit: 3 }
]
Insert cell
## Data CSV
Insert cell
textCSV = `Area,Sales,Profit
North,5,2
East,25,8
West,10,6
South,20,5
Central,15,3`
Insert cell
To convert CSV to our Row based data - We can use d3 data loading function

d3.csvParse(string)
d3.csv(url)

Insert cell
dataCSV = d3.csvParse(textCSV)
Insert cell
## Making Visualisation
Insert cell
Using VegaLite (https://vega.github.io/vega-lite/)

Use this vl.spec( specification).render()
Import vega-embed
Insert cell
vl
.spec({
data: { values: dataRow },
mark: "bar",
encoding: {
x: { field: "area", type: "nominal" },
y: { field: "sales", type: "quantitative" }
}
})
.render()
Insert cell
vegaEmbed = require("vega-embed")
Insert cell
vegaEmbed({
data: { values: dataRow },
mark: "bar",
encoding: {
x: { field: "area", type: "N" },
y: { field: "sales", type: "Q" },
color: { field: "profit", type: "Q" }
}
})
Insert cell
vegaEmbed({
width: 300,
height: 300,
data: { values: dataRow },
mark: "circle",
encoding: {
x: { field: "area", type: "N" },
y: { field: "sales", type: "Q" },
color: { field: "area", type: "N" },
size: { field: "profit", type: "Q" }
}
})
Insert cell
vegaEmbed({
width: 300,
height: 300,
data: { values: dataRow },
mark: "text",
encoding: {
x: { field: "sales", type: "Q", axis: null },
y: { field: "profit", type: "Q", axis: null },
text: { field: "area", type: "N" },
color: { field: "area", type: "N" },
size: { field: "profit", type: "Q" }
},
config: { view: { stroke: null } }
})
Insert cell
vegaEmbed({
data: { values: dataRow },
mark: "bar",
encoding: {
x: { field: "sales", type: "quantitative" },
y: { field: "area", type: "nominal" }
}
})
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