Skip to content

Commit fa5ce9a

Browse files
committed
week 9
1 parent 809a8a0 commit fa5ce9a

3 files changed

Lines changed: 138 additions & 0 deletions

File tree

observablehq.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default {
3030
{ name: "Week 6: Intro to Observable Plot", path: "/class_code/week_6" },
3131
{ name: "Week 7: Transforms and Data Manipulation", path: "/class_code/week_7" },
3232
{ name: "Week 8: Data Types <> Scales <> Marks", path: "/class_code/week_8" },
33+
{ name: "Week 9: Data Joins and Annotations", path: "/class_code/week_9" },
3334
],
3435
},
3536
{
@@ -71,6 +72,13 @@ export default {
7172
// { name: "Instructions", path: "/lab_4/readme" },
7273
// { name: "Dashboard", path: "/lab_4/index" },
7374
],
75+
},
76+
{
77+
name: "Scratchpad",
78+
open: false,
79+
pages: [
80+
{ name: "Scratchpad", path: "/scratchpad/index" },
81+
],
7482
}
7583

7684
],

src/class_code/week_9.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
title: "Week 9: Annotations + Data Joins"
3+
toc: true
4+
---
5+
6+
# Week 9: Annotations + Data Joins
7+
8+
### Adding `z` or `stroke`
9+
10+
```js
11+
const stocks = FileAttachment("./stock_data/stocks.csv").csv({ typed: true })
12+
```
13+
14+
```js
15+
Inputs.table(stocks)
16+
```
17+
18+
```js
19+
Plot.plot({
20+
marks: [
21+
Plot.ruleX([stocks[0].Date]),
22+
Plot.ruleY([0]),
23+
Plot.line(stocks, {
24+
x: "Date",
25+
y: "Close",
26+
// fill: "blue",
27+
// stroke: "blue",
28+
stroke: "Ticker",
29+
tip: true,
30+
})
31+
]
32+
})
33+
```
34+
35+
### Data join: lookup position v1
36+
37+
```js
38+
Plot.plot({
39+
marks: [
40+
Plot.line(stocks.filter(d => d.Ticker !== "AAPL"), {
41+
x: "Date",
42+
y: "Close",
43+
z: "Ticker",
44+
stroke: "lightgrey",
45+
}),
46+
Plot.line(stocks.filter(d => d.Ticker === "AAPL"), {
47+
x: "Date",
48+
y: "Close",
49+
stroke: "black",
50+
}),
51+
Plot.ruleX([new Date("11/1/2021")]),
52+
Plot.dot([new Date("11/1/2021")], {
53+
x: d => d,
54+
y: datum => {
55+
console.log("datum", datum)
56+
const importantDataPiece = stocks
57+
.find(obj => {
58+
console.log("obj", obj.Date.toDateString())
59+
return obj.Date.toDateString() === datum.toDateString() && obj.Ticker === "AAPL"
60+
})
61+
// console.log("WE DID IT", importantDataPiece)
62+
return importantDataPiece.Close
63+
},
64+
fill: "pink",
65+
r: 10
66+
// tip: true
67+
}),
68+
Plot.tip([
69+
{
70+
Date: new Date("11/1/2021"),
71+
Note: "Something important"
72+
}
73+
], {
74+
x: "Date",
75+
y: 200,
76+
channels: {
77+
Date: "Date",
78+
Note: "Note",
79+
More: () => "something more"
80+
}
81+
})
82+
]
83+
})
84+
```
85+
86+
### Data join: lookup position v2
87+
88+
```js
89+
const aapl = await FileAttachment("./stock_data/aapl.csv").csv({ typed: true })
90+
const events = await FileAttachment("./stock_data/stock_events.csv").csv({ typed: true })
91+
display(aapl[0])
92+
display(events)
93+
```
94+
95+
```js
96+
const aaplEvents = events.filter(datum => datum["Related Tickers"].includes("AAPL"))
97+
display(aaplEvents)
98+
```
99+
100+
```js
101+
Plot.plot({
102+
marks: [
103+
Plot.line(aapl, {
104+
x: "Date",
105+
y: "Close",
106+
}),
107+
Plot.dot(aaplEvents, {
108+
x: "Date",
109+
y: eventDatum => {
110+
// console.log(eventDatum)
111+
const dateToFind = eventDatum.Date.toDateString()
112+
const foundAaplObj = aapl.find(
113+
aaplObj => aaplObj.Date.toDateString() === dateToFind
114+
)
115+
// console.log("foundAaplObj", foundAaplObj)
116+
if (foundAaplObj) return foundAaplObj.Close
117+
else return 0
118+
},
119+
tip: true,
120+
fill: "red",
121+
channels: {
122+
"Event": "Event Name",
123+
"Notes": "Notes"
124+
}
125+
}),
126+
]
127+
})
128+
```

src/scratchpad/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello world!!!
2+

0 commit comments

Comments
 (0)