Notebooks 2.0 is here.
Read the preview announcement
Platform
Resources
Pricing
Sign in
Get started
Christine Langston
Workspace
Fork
Published
Class Work
By
Christine Langston
Edited
Apr 10, 2019
2 forks
6 stars
Class Work
Week 12 - Interaction and Animation: D3 Transitions, Behaviors, and Brushing
Week 11 - Intro to D3.js: Mapping Data with D3
Week 10 - Intro to D3.js: Making a Chart
Parsing API Responses
Week 5a - Web APIs
Week 4a - Exploring Data with Vega-Lite
Week 3 - Data Management with Zebras
Week 3a - Data Management
Week 2b - Advanced Javascript
Week 2a - Introduction to Javascript Part2
Week 1b - Introduction to Javascript
Week 1a - Introduction to HTML and Javascript
Week 0 - Introduction to Observable
Insert cell
Insert cell
Insert cell
//import {chart} from "@mbostock/d3-choropleth"
import
{
chart
as
choropleth
}
from
"@chagel/d3-choropleth"
Insert cell
choropleth
Insert cell
Insert cell
Insert cell
bosNeighborhoods
=
d3
.
json
(
'https://gist.githubusercontent.com/jdev42092/5c285c4a3608eb9f9864f5da27db4e49/raw/a1c33b1432ca2948f14f656cc14c7c7335f78d95/boston_neighborhoods.json'
)
Insert cell
Insert cell
width
=
700
;
Insert cell
height
=
580
;
Insert cell
{
// Create SVG
let
svg
=
d3
.
select
(
"body"
)
.
append
(
"svg"
)
.
attr
(
"width"
,
width
)
.
attr
(
"height"
,
height
)
;
// Append empty placeholder g element to the SVG
// g will contain geometry elements
let
g
=
svg
.
append
(
"g"
)
;
return
svg
.
node
(
)
;
}
Insert cell
Insert cell
bosProjection
=
d3
.
geoAlbers
(
)
.
scale
(
190000
)
.
rotate
(
[
71.057
,
0
]
)
.
center
(
[
0
,
42.313
]
)
.
translate
(
[
width
/
2
,
height
/
2
]
)
;
Insert cell
Insert cell
// Create GeoPath function that uses built-in D3 functionality to turn
// lat/lon coordinates into screen coordinates
bos_geoPath
=
d3
.
geoPath
(
)
.
projection
(
bosProjection
)
;
Insert cell
Insert cell
// your code here
mybosProjection
=
d3
.
geoEquirectangular
(
)
.
scale
(
190000
)
.
rotate
(
[
71.057
,
0
]
)
.
center
(
[
0
,
42.313
]
)
.
translate
(
[
width
/
2
,
height
/
2
]
)
;
Insert cell
mybos_geoPath
=
d3
.
geoPath
(
)
.
projection
(
mybosProjection
)
;
Insert cell
Insert cell
{
// Create SVG
let
svg
=
d3
.
select
(
DOM
.
svg
(
width
,
height
)
)
;
// Append empty placeholder g element to the SVG
// g will contain geometry elements
let
g
=
svg
.
append
(
"g"
)
;
// Classic D3... Select non-existent elements, bind the data, append the elements, and apply attributes
g
.
selectAll
(
"path"
)
.
data
(
bosNeighborhoods
.
features
)
.
enter
(
)
.
append
(
"path"
)
// before we used lines, now we are using a path allows path
.
attr
(
"fill"
,
"#ccc"
)
.
attr
(
"stroke"
,
"#333"
)
.
attr
(
"d"
,
mybos_geoPath
)
;
return
svg
.
node
(
)
;
}
Insert cell
Insert cell
rodentData
=
d3
.
json
(
'https://gist.githubusercontent.com/jdev42092/ee94f6d469d7084e8dca4e8533817e0e/raw/7cfd6c34c974d3a86ff19c6180cfa22ee9ce3946/boston_rodents.json'
)
Insert cell
Insert cell
{
// Create SVG
let
svg
=
d3
.
select
(
DOM
.
svg
(
width
,
height
)
)
;
// Append empty placeholder g element to the SVG
// g will contain geometry elements
let
g
=
svg
.
append
(
"g"
)
;
let
rodents
=
svg
.
append
(
"g"
)
;
// Classic D3... Select non-existent elements, bind the data, append the elements, and apply attributes
g
.
selectAll
(
"path"
)
.
data
(
bosNeighborhoods
.
features
)
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"fill"
,
"#ccc"
)
.
attr
(
"stroke"
,
"#333"
)
.
attr
(
"d"
,
bos_geoPath
)
;
rodents
.
selectAll
(
"path"
)
.
data
(
rodentData
.
features
)
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"fill"
,
"#900"
)
.
attr
(
"stroke"
,
"#999"
)
.
attr
(
"d"
,
bos_geoPath
)
;
return
svg
.
node
(
)
;
}
Insert cell
Insert cell
Insert cell
// tried to switch order of code
{
// Create SVG
let
svg
=
d3
.
select
(
DOM
.
svg
(
width
,
height
)
)
;
// Append empty placeholder g element to the SVG
// g will contain geometry elements
let
rodents
=
svg
.
append
(
"g"
)
;
// christine
let
g
=
svg
.
append
(
"g"
)
;
// Classic D3... Select non-existent elements, bind the data, append the elements, and apply attributes
rodents
.
selectAll
(
"path"
)
.
data
(
rodentData
.
features
)
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"fill"
,
"#900"
)
.
attr
(
"stroke"
,
"#999"
)
.
attr
(
"d"
,
bos_geoPath
)
;
g
.
selectAll
(
"path"
)
.
data
(
bosNeighborhoods
.
features
)
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"fill"
,
"#ccc"
)
.
attr
(
"stroke"
,
"#333"
)
.
attr
(
"d"
,
bos_geoPath
)
;
return
svg
.
node
(
)
;
}
Insert cell
// html`
// <style>
// .incident {
// fill : steelblue;
// stroke : none;
// opacity : 0.5;
// }
// </style>
// `
Insert cell
Insert cell
Insert cell
{
// Create SVG
let
svg
=
d3
.
select
(
DOM
.
svg
(
width
,
height
)
)
;
// Append empty placeholder g element to the SVG
// g will contain geometry elements
let
g
=
svg
.
append
(
"g"
)
;
let
rodents
=
svg
.
append
(
"g"
)
;
// Classic D3... Select non-existent elements, bind the data, append the elements, and apply attributes
g
.
selectAll
(
"path"
)
.
data
(
bosNeighborhoods
.
features
)
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"fill"
,
"#ccc"
)
.
attr
(
"stroke"
,
"#333"
)
.
attr
(
"d"
,
bos_geoPath
)
;
rodents
.
selectAll
(
"path"
)
.
data
(
rodentData
.
features
)
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"fill"
,
"steelblue"
)
.
attr
(
"stroke"
,
"none"
)
.
attr
(
"opacity"
,
0.5
)
.
attr
(
"d"
,
bos_geoPath
)
.
attr
(
"class"
,
"incident"
)
.
on
(
"mouseover"
,
d
=>
d3
.
select
(
"#label"
)
.
text
(
d
.
properties
.
LOCATION_STREET_NAME
)
)
// how to mouseover
.
on
(
"mouseout"
,
d
=>
d3
.
select
(
"#label"
)
.
text
(
"Empty Selection"
)
)
;
return
svg
.
node
(
)
;
}
Insert cell
Insert cell
{
// Create Event Handlers for mouse
function
handleMouseOver
(
d
,
i
)
{
// Add interactivity
// Use D3 to select element, change color and size
d3
.
select
(
this
)
.
attr
(
"fill"
,
"orange"
)
// .on("mouseover", d => d3.attr("fill", "red"));
}
function
handleMouseOut
(
d
,
i
)
{
// Use D3 to select element, change color back to normal
d3
.
select
(
this
)
.
attr
(
'fill'
,
"steelblue"
)
;
// .on("mouseout", d => d3.select("#label").text(d.properties.LOCATION_STREET_NAME))
}
}
Insert cell
Insert cell
Insert cell
Insert cell
// your code here
{
// Create SVG
let
svg
=
d3
.
select
(
DOM
.
svg
(
width
,
height
)
)
;
// Append empty placeholder g element to the SVG
// g will contain geometry elements
let
g
=
svg
.
append
(
"g"
)
;
let
rodents
=
svg
.
append
(
"g"
)
;
// Classic D3... Select non-existent elements, bind the data, append the elements, and apply attributes
g
.
selectAll
(
"path"
)
.
data
(
bosNeighborhoods
.
features
)
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"fill"
,
"#ccc"
)
.
attr
(
"stroke"
,
"#333"
)
.
attr
(
"d"
,
bos_geoPath
)
;
rodents
.
selectAll
(
"path"
)
.
data
(
rodentData
.
features
)
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"fill"
,
"steelblue"
)
.
attr
(
"stroke"
,
"none"
)
.
attr
(
"opacity"
,
0.5
)
.
attr
(
"d"
,
bos_geoPath
)
.
attr
(
"class"
,
"incident"
)
.
on
(
"mouseover"
,
handleMouseOver
)
// how to mouseover
.
on
(
"mouseout"
,
handleMouseOut
)
.
on
(
"click"
,
d
=>
d3
.
select
(
"#my_label"
)
.
text
(
d
.
properties
.
Source
)
)
return
svg
.
node
(
)
;
}
Insert cell
Insert cell
Insert cell
topojson
=
require
(
"topojson-client@3"
)
Insert cell
Insert cell
Insert cell
us
=
d3
.
json
(
"https://unpkg.com/us-atlas@1/us/10m.json"
)
Insert cell
Insert cell
path
=
d3
.
geoPath
(
)
;
Insert cell
Insert cell
{
let
width
=
960
;
let
height
=
600
;
// Create SVG
let
svg
=
d3
.
select
(
DOM
.
svg
(
width
,
height
)
)
;
let
g
=
svg
.
append
(
"g"
)
;
// Bind TopoJSON data
g
.
selectAll
(
"path"
)
.
data
(
topojson
.
feature
(
us
,
us
.
objects
.
counties
)
.
features
)
// Bind TopoJSON data elements
// pass through what objects you want to use -- in this case we are doing county lines
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"d"
,
path
)
.
style
(
"fill"
,
"white"
)
.
style
(
"stroke"
,
"black"
)
;
return
svg
.
node
(
)
;
}
Insert cell
Insert cell
// your code here
Insert cell
Insert cell
unemployment
=
d3
.
tsv
(
"https://gist.githubusercontent.com/jdev42092/9a314291805640a097e16e58248627eb/raw/365c199c5a173a0018608615b6823b5cdcaa6bae/unemployment.tsv"
,
(
{
id
,
rate
}
)
=>
(
{
county
:
id
,
rate
:
+
rate
}
)
)
Insert cell
Insert cell
unemp_data
=
{
// Create empty object for holding dataset
const
rateById
=
{
}
;
// Create property for each ID, give it value from rate
unemployment
.
forEach
(
d
=>
(
rateById
[
d
.
county
]
=
+
d
.
rate
)
)
;
return
rateById
;
}
Insert cell
Insert cell
Insert cell
color
=
d3
.
scaleQuantize
(
)
.
domain
(
[
1
,
10
]
)
.
range
(
d3
.
schemePurples
[
9
]
)
;
Insert cell
Insert cell
{
let
width
=
960
;
let
height
=
600
;
// Create SVG
let
svg
=
d3
.
select
(
DOM
.
svg
(
width
,
height
)
)
;
let
g
=
svg
.
append
(
"g"
)
;
// Create empty object for holding dataset
const
rateById
=
{
}
;
// Create property for each ID, give it value from rate
unemployment
.
forEach
(
d
=>
(
rateById
[
d
.
county
]
=
+
d
.
rate
)
)
;
// Bind TopoJSON data
g
.
selectAll
(
"path"
)
.
data
(
topojson
.
feature
(
us
,
us
.
objects
.
counties
)
.
features
)
// Bind TopoJSON data elements
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"d"
,
path
)
.
style
(
"fill"
,
d
=>
color
(
rateById
[
d
.
id
]
)
)
// get rate value for property matching data ID
// pass rate value to color function, return color based on domain and range
.
style
(
"stroke"
,
"black"
)
;
return
svg
.
node
(
)
;
}
Insert cell
Insert cell
{
let
width
=
960
;
let
height
=
600
;
// Create SVG
let
svg
=
d3
.
select
(
DOM
.
svg
(
width
,
height
)
)
;
let
g
=
svg
.
append
(
"g"
)
;
// Create empty object for holding dataset
const
rateById
=
{
}
;
// Create property for each ID, give it value from rate
unemployment
.
forEach
(
d
=>
(
rateById
[
d
.
county
]
=
+
d
.
rate
)
)
;
// Bind TopoJSON data
g
.
selectAll
(
"path"
)
.
data
(
topojson
.
feature
(
us
,
us
.
objects
.
counties
)
.
features
)
// Bind TopoJSON data elements
.
enter
(
)
.
append
(
"path"
)
.
attr
(
"d"
,
path
)
.
style
(
"fill"
,
d
=>
color
(
rateById
[
d
.
id
]
)
)
;
g
.
append
(
"path"
)
.
datum
(
topojson
.
mesh
(
us
,
us
.
objects
.
states
,
(
a
,
b
)
=>
a
!==
b
)
)
.
attr
(
"fill"
,
"none"
)
.
attr
(
"stroke"
,
"white"
)
.
attr
(
"d"
,
path
)
;
return
svg
.
node
(
)
;
}
Insert cell
Insert cell
Insert cell
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
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
bosNeighborhoods
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
width
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
height
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
bosProjection
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
bos_geoPath
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mybosProjection
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
mybos_geoPath
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rodentData
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
handleMouseOver
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
handleMouseOut
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
topojson
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
us
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
path
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
unemployment
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
unemp_data
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
color
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
d3
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML