Skip to content

Commit e21003c

Browse files
committed
week 13
1 parent d6603ad commit e21003c

File tree

5 files changed

+514
-1
lines changed

5 files changed

+514
-1
lines changed

observablehq.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export default {
5858
pages: [
5959
{ name: "Instructions", path: "/lab_4/readme" },
6060
{ name: "Dashboard", path: "/lab_4/index" },
61+
{ name: "Week 13 Notes", path: "/lab_4/week_13_notes" },
62+
{ name: "Week 13 Class", path: "/lab_4/week_13_class" },
6163
],
6264
}
6365

src/lab_3/week_10_notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ display(unemployment)
251251
4. Use topojson from d3 to convert this topology file into a feature collection we can render in svg:
252252

253253
```js echo
254-
const counties = topojson.feature(geo, geo.objects.data)
254+
const counties = topojson.feature(geo, geo.objects.counties)
255255
display(counties)
256256
```
257257
5. [optional] If your GeoJSON data doesn't already have the data as part of the properties, you'll need to create a join to add that information.

src/lab_4/components/charts.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// this time, we _do_ have to import plot and d3, because we aren't in the observable markdown file that includes these libraries by default.
2+
import * as Plot from "npm:@observablehq/plot";
3+
import * as d3 from "npm:d3";
4+
5+
export function plotChart(width) {
6+
return Plot.plot({
7+
width,
8+
marks: [
9+
Plot.frame(),
10+
Plot.text(["Hello, world!"], {
11+
frameAnchor: "middle",
12+
})
13+
]
14+
})
15+
};
16+
17+
export function d3Chart(width) {
18+
const svg = d3.create("svg")
19+
.attr("width", width)
20+
.attr("height", 100)
21+
.style("border-style", "solid")
22+
23+
svg
24+
.append("text")
25+
.text("Hello, world!")
26+
.attr("x", width / 2)
27+
.attr("y", 50)
28+
.attr("text-anchor", "middle")
29+
30+
return svg.node();
31+
}
32+
33+
34+
export function scatterplot(width, data, x, y, color) {
35+
console.log("scatterplot with data:", data)
36+
37+
// Set up margins for axes
38+
const margin = { top: 20, right: 30, bottom: 40, left: 50 }
39+
const height = 300
40+
const innerWidth = width - margin.left - margin.right
41+
const innerHeight = height - margin.top - margin.bottom
42+
43+
const xScale = d3.scaleLinear()
44+
.domain(d3.extent(data.map(d => d[x])))
45+
.range([0, innerWidth])
46+
47+
const yScale = d3.scaleLinear()
48+
.domain(d3.extent(data.map(d => d[y])))
49+
.range([innerHeight, 0]) // Flip y axis so smaller values are at bottom
50+
51+
const colorScale = d3.scaleOrdinal()
52+
.domain(data.map(d => d[color]))
53+
.range(d3.schemeTableau10)
54+
55+
const svg = d3.create("svg")
56+
.attr("width", width)
57+
.attr("height", height)
58+
59+
// Create a group for the chart content
60+
const g = svg.append("g")
61+
.attr("transform", `translate(${margin.left},${margin.top})`)
62+
63+
// Add x axis
64+
g.append("g")
65+
.attr("transform", `translate(0,${innerHeight})`)
66+
.call(d3.axisBottom(xScale))
67+
.append("text")
68+
.attr("x", innerWidth / 2)
69+
.attr("y", 35)
70+
.attr("fill", "black")
71+
.style("text-anchor", "middle")
72+
// obviously this is hard coded, so it would still render if we changed the x variable, but its a helpful example of how to make axis text appear.
73+
.text("Culmen Depth (mm)")
74+
75+
// Add y axis
76+
g.append("g")
77+
.call(d3.axisLeft(yScale))
78+
.append("text")
79+
.attr("transform", "rotate(-90)")
80+
.attr("y", -35)
81+
.attr("x", -innerHeight / 2)
82+
.attr("fill", "black")
83+
.style("text-anchor", "middle")
84+
// same here -- hard coded, and would still render if we changed the y variable.
85+
.text("Culmen Length (mm)")
86+
87+
// Add circles
88+
g.selectAll("circle")
89+
.data(data)
90+
.join("circle")
91+
.attr("cx", d => xScale(d[x]))
92+
.attr("cy", d => yScale(d[y]))
93+
.attr("r", 3)
94+
.attr("fill", d => colorScale(d[color]))
95+
96+
return svg.node();
97+
}

src/lab_4/week_13_class.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
title: "Week 13 Class"
3+
toc: true
4+
---
5+
6+
```js
7+
display(width)
8+
```
9+
<!-- // // <circle cy="100" cx="50" r="50"></circle> -->
10+
<!-- <svg style="border: 1px solid;" id="container" width="400" height="400">
11+
12+
</svg> -->
13+
14+
15+
<!-- ```js
16+
const svg = d3.select("#container")
17+
.append('text')
18+
.text('Hello world!')
19+
.attr("x", 50)
20+
.attr("y", 50)
21+
22+
display(svg)
23+
console.log(svg)
24+
``` -->
25+
26+
<!-- ```js
27+
const svg = d3.select("#container")
28+
.style("background-color", "lightgray")
29+
30+
const circles = svg.selectAll(".words")
31+
.data([
32+
"quick", "brown", "fox", "jumped", "over",
33+
])
34+
.join("text")
35+
.attr("class", "words")
36+
.text(d => d)
37+
// .text("hello world")
38+
.attr("x", (d, i) => i * 75)
39+
.attr("y", 50)
40+
41+
42+
console.log(circles)
43+
44+
45+
display(svg)
46+
console.log(svg)
47+
``` -->
48+
49+
<!--
50+
```js
51+
display(penguins)
52+
```
53+
```js
54+
Plot.plot({
55+
marks: [
56+
Plot.dot(penguins, {
57+
x: "culmen_length_mm",
58+
y: d => d["culmen_depth_mm"]
59+
})
60+
]
61+
})
62+
``` -->
63+
64+
65+
<!-- ```js
66+
console.log(penguins)
67+
const svg = d3.select("#container")
68+
// .style("background-color", "lightgray")
69+
70+
const xDomain = d3.extent(penguins.map(d => d["culmen_length_mm"]))
71+
const xScale = d3.scaleLinear()
72+
.domain(xDomain)
73+
.range([0, 400])
74+
.nice()
75+
76+
77+
const yDomain = d3.extent(penguins.map(d => d["culmen_depth_mm"]))
78+
const yScale = d3.scaleLinear()
79+
.domain(yDomain)
80+
.range([400, 0])
81+
.nice()
82+
83+
const penguinCircles = svg.selectAll()
84+
.data(penguins)
85+
.join("circle")
86+
.style("stroke", "black")
87+
.style("fill", "none")
88+
.attr("r", 3)
89+
.attr("cx", d => xScale(d["culmen_length_mm"]))
90+
.attr("cy", d => yScale(d["culmen_depth_mm"]))
91+
92+
display(penguinCircles)
93+
94+
95+
96+
``` -->
97+
98+
<svg style="border: 1px solid;" id="container" width="${width}$" height="200">
99+
100+
</svg>
101+
102+
```js
103+
const stocks = await FileAttachment('../lab_2/stock_data/stocks.csv').csv({ typed: true})
104+
display(stocks)
105+
106+
const svg = d3.select("#container")
107+
108+
const xDomain = d3.extent(stocks.map(d => d["Date"]))
109+
const xScale = d3.scaleTime()
110+
.domain(xDomain)
111+
.range([0, width])
112+
.nice()
113+
114+
const yDomain = d3.extent(stocks.map(d => d["Close"]))
115+
const yScale = d3.scaleLinear()
116+
.domain(yDomain)
117+
.range([200, 0])
118+
.nice()
119+
120+
console.log(stocks[864])
121+
console.log(xScale(stocks[864].Date))
122+
console.log(yScale(stocks[864].Close))
123+
124+
const pathGen = d3.line()
125+
.x(d => xScale(d["Date"]))
126+
.y(d => yScale(d["Close"]))
127+
128+
console.log(pathGen)
129+
130+
const tickerData = d3.groups(stocks, d => d["Ticker"]).map(([ticker, data]) => data)
131+
console.log(tickerData)
132+
133+
const stockPath = svg.selectAll(".stocks")
134+
.data(tickerData)
135+
// .data(stocks)
136+
.join("path")
137+
.attr("class", "stocks")
138+
.attr("d", d => pathGen(d))
139+
.style("stroke", "black")
140+
.style("fill", "none")
141+
142+
143+
// d3.select("#container")
144+
// .selectAll(".stocks")
145+
// .data(tickerData)
146+
// // .data(stocks)
147+
// .join("path")
148+
// .attr("class", "stocks")
149+
// .attr("d", d => pathGen(d))
150+
// .style("stroke", "black")
151+
// .style("fill", "none");
152+
153+
154+
```

0 commit comments

Comments
 (0)