Platform
Resources
Pricing
Sign in
Get started
Mike Bostock
Visualization toolmaker. Founder @observablehq. Creator @d3. Former @nytgraphics. Pronounced BOSS-tock.
Workspace
Fork
Published
By
Mike Bostock
Edited
Feb 10, 2020
1 fork
Importers
13 stars
1
Insert cell
Insert cell
Insert cell
chart
=
{
const
svg
=
d3
.
create
(
"svg"
)
.
attr
(
"viewBox"
,
[
0
,
0
,
gridWidth
*
cellSize
,
gridHeight
*
cellSize
]
)
.
attr
(
"text-anchor"
,
"middle"
)
.
attr
(
"font-family"
,
"sans-serif"
)
.
attr
(
"font-size"
,
10
)
;
const
cell
=
svg
.
append
(
"g"
)
.
selectAll
(
"g"
)
.
data
(
d3
.
groups
(
senators
,
d
=>
d
.
statecode
)
)
.
join
(
"g"
)
.
attr
(
"transform"
,
(
[
statecode
]
)
=>
{
const
[
i
,
j
]
=
position
.
get
(
statecode
)
;
return
`translate(${
i
*
cellSize
},${
j
*
cellSize
})`
;
}
)
;
cell
.
append
(
"path"
)
.
attr
(
"d"
,
`M0,${
cellSize
-
1
}V0H${
cellSize
-
1
}z`
)
;
cell
.
append
(
"path"
)
.
attr
(
"d"
,
`M0,${
cellSize
-
1
}H${
cellSize
-
1
}V0z`
)
;
cell
.
selectAll
(
"path"
)
.
data
(
(
[
,
senators
]
)
=>
senators
)
.
attr
(
"fill"
,
(
{
party
}
)
=>
color
(
party
)
)
.
append
(
"title"
)
.
text
(
(
{
name
,
state
,
party
}
,
i
)
=>
`${
name
}
${
party
}
${
state
} (${
i
?
"Junior"
:
"Senior"
})`
)
;
cell
.
append
(
"text"
)
.
attr
(
"pointer-events"
,
"none"
)
.
attr
(
"x"
,
cellSize
/
2
)
.
attr
(
"y"
,
cellSize
/
2
)
.
attr
(
"dy"
,
"0.32em"
)
.
text
(
(
[
statecode
]
)
=>
statecode
)
;
return
svg
.
node
(
)
;
}
Insert cell
color
=
d3
.
scaleOrdinal
(
[
"Republican"
,
"Democratic"
,
"Independent"
]
,
[
"#e41a1c"
,
"#377eb8"
,
"#4daf4a"
]
)
Insert cell
senators
=
d3
.
csvParse
(
await
FileAttachment
(
"senators-116.csv"
)
.
text
(
)
,
d3
.
autoType
)
Insert cell
position
=
grid
`
, , , , , , , , , ,ME
, , , , ,WI, , , ,VT,NH
WA,ID,MT,ND,MN,IL,MI, ,NY,MA,
OR,NV,WY,SD,IA,IN,OH,PA,NJ,CT,RI
CA,UT,CO,NE,MO,KY,WV,VA,MD,DE,
,AZ,NM,KS,AR,TN,NC,SC,DC, ,
, , ,OK,LA,MS,AL,GA, , ,
HI,AK, ,TX, , , , ,FL, , `
Insert cell
function
grid
(
)
{
const
positionById
=
new
Map
;
d3
.
csvParseRows
(
String
.
raw
.
apply
(
String
,
arguments
)
.
replace
(
/^\n|\n$/g
,
""
)
,
(
row
,
j
)
=>
{
row
.
forEach
(
(
id
,
i
)
=>
{
if
(
id
=
id
.
trim
(
)
)
{
positionById
.
set
(
id
,
[
i
,
j
]
)
;
}
}
)
;
}
)
;
return
positionById
;
}
Insert cell
gridWidth
=
d3
.
max
(
position
,
(
[
,
[
i
]
]
)
=>
i
)
+
1
Insert cell
gridHeight
=
d3
.
max
(
position
,
(
[
,
[
,
j
]
]
)
=>
j
)
+
1
Insert cell
cellSize
=
40
Insert cell
d3
=
require
(
"d3@5"
,
"d3-array@2"
)
Insert cell
import
{
swatches
}
from
"@d3/color-legend"
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.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
chart
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
color
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
senators
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
position
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
grid
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gridWidth
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
gridHeight
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
cellSize
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
d3
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML