-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.qmd
232 lines (201 loc) · 6.72 KB
/
index.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
---
title: "State Incentives Map"
subtitle: Rebecca Lester, Stanford Graduate School of Business
title-block-banner: "#15235b"
title-block-banner-color: white
---
```{r}
library(dplyr)
library(stringr)
dataset <- redivis::user("datapages")$dataset("subsidy_map")
regime_table <- dataset$table("state_disclosure_regimes")$to_tibble()
regime_data <- regime_table |> rename_with(tolower) |> select(
state,
year = year_of_disclosure_law,
program = specific_subsidy_program_affected_by_law____means_update_to_,
disclosure_law,
state_statute,
excerpt,
sample = subsidies_in_sample___y_n_,
internal = internal_disclosure___y_n_,
external = external_disclosure___y_n_
) |>
mutate(across(everything(), \(s) s |>
str_trim() |>
str_replace_all("¬ß", "§") |>
str_replace_all("¬∑", "·") |>
str_replace_all("‚Ä[ôò]", "'") |>
str_replace_all("‚Ä[úù]", "'") |>
str_replace_all("‚Ä[îì]", "–") |>
str_replace_all("‚Ä[Çâ]", " ") |>
str_replace_all("…", "..."))) |>
arrange(desc(year), state)
ojs_define(regime_data)
```
```{ojs}
Plot = import("https://esm.sh/@observablehq/[email protected]")
//redivis = require("redivis")
//reg = await redivis
// .user("stanford_templates")
// .dataset("subsidy_map")
// .table("state_disclosure_regimes")
// .listRows()
//reg = FileAttachment("regimes.csv").csv()
reg = transpose(regime_data)
dir = (int, ext) => (
[int === "Y" ? "internal" : "", ext === "Y" ? "external" : ""].filter(Boolean)
)
regimes = reg.map(obj => ({
...obj,
year: new Date(obj.year.toString(), 0, 1),
direction: dir(obj.internal, obj.external)
}));
// map
// adapted from https://observablehq.com/@mbostock/u-s-state-map
viewof state = {
let value = null;
const colors = { excluded: "#ccc", included: "#B83A4B" };
const dim = 0.6;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 975, 610])
const statesGroup = svg.append("g");
// state shapes
statesGroup.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path)
.attr("data-id", d => d.id)
.attr("fill", d => states.get(d.id).included ? colors.included : colors.excluded)
.attr("fill-opacity", dim)
.on("click", (event, d) => {
if (states.get(d.id).included) {
// set value to clicked state
const node = svg.node();
node.value = value = value === d.id ? null : d.id;
node.dispatchEvent(new Event("input", {bubbles: true}));
// change clicked state opacity
statesGroup.selectAll("path")
.attr("fill-opacity", d => value === d.id ? 1 : dim)
}
})
.on("mouseover", (event, d) => {
if (states.get(d.id).included) {
// change hovered state opacity
statesGroup.select(`path[data-id="${d.id}"]`)
.attr("fill-opacity", 1);
}
})
.on("mouseout", (event, d) => {
if (states.get(d.id).included) {
// reset hovered state opacity
statesGroup.select(`path[data-id="${d.id}"]`)
.attr("fill-opacity", d => value === d.id ? 1 : dim);
}
});
// text labels
statesGroup.selectAll("text")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("text")
.filter(d => states.get(d.id).included)
.attr("transform", d => `translate(${path.centroid(d)})`)
.attr("dy", "0.35em")
.attr("text-anchor", "middle")
.attr("pointer-events", "none")
.text(d => states.get(d.id).abbr);
// outlines
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("pointer-events", "none")
.attr("d", path);
return Object.assign(svg.node(), {value: null});
}
us = FileAttachment("states-albers-10m.json").json() // map topology
abbr = FileAttachment("states-abbr.json").json() // state names + abbreviations
states = new Map(us.objects.states.geometries.map(
d => [d.id, {"name": d.properties.name,
"abbr": abbr[d.properties.name],
"included": regimes.map(d => d.state).includes(d.properties.name)}])
)
path = d3.geoPath()
```
```{ojs}
// currently selected state
state_obj = states.get(state)
state_name = state_obj ? state_obj["name"] : null
// all regimes with selected state variable
reg_state = regimes.map(obj => ({
...obj,
curr_state: state_name ? obj.state === state_name : true
}));
// only regimes for selected state
reg_curr = reg_state.filter(d => d.curr_state)
caption = htl.html`<div class="caption">Click on a highlighted state to view its incentive programs and disclosure regimes.<br>Click again on the selected state to view all regimes.</div>`
// dot plot
viewof timeline = Plot.plot({
height: 500,
width: 400,
x: {label: "Year"},
color: {range: ["#ccc", "#B83A4B"]},
marks: [
Plot.dot(reg_state,
Plot.dodgeY({
x: "year",
fill: "curr_state",
title: (d) => [d.state, d.disclosure_law].join("\n"),
sort: "curr_state",
reverse: true,
tip: true
})
),
]
})
// place map and plot side by side
html`<div style="display: flex; align-items: center; margin-bottom: 2em;">
<div style="flex-basis:50%"> ${viewof state} ${caption} </div>
<div style="flex-basis:50%"> ${viewof timeline} </div>
</div>`
```
```{ojs}
// search bar
viewof reg_search = Inputs.search(reg_curr)
// table
viewof row = Inputs.table(reg_search, {
rows: 100, // rows displayed at start
// sort: "year", reverse: true, // reverse chronological order
multiple: false, // only select one row at a time
required: false, // if nothing is selected, selection is empty
columns: [
"state",
"year",
"direction",
"disclosure_law",
],
header: {
state: "State",
year: "Year",
direction: "",
disclosure_law: "Disclosure law",
},
format: {
year: x => x.getFullYear(),
direction: badgify
}
})
badgify = x => htl.html`<div>
${x.map(y => htl.html.fragment`
<span class="badge ${y}">${y}</span>`)}
</div>`
```
```{ojs}
// sidebar
pullout = (head, text) => htl.html.fragment`<b>${head}</b><p>${text}</p>`
placeholder = htl.html.fragment`<p class="pullout"><i>Select a row to see more information...</i></p>`
viewof pullouts = row ? htl.html.fragment`${pullout("Subsidy program affected", row.program)} ${pullout("State statute", row.state_statute)} ${pullout("Excerpt", row.excerpt)}` : placeholder
htl.html`<div class="grid-container">
<div> ${viewof row} </div>
<div class="pullout"> ${viewof pullouts} </div>
</div>`
```