Platform
Solutions
Resources
Pricing
Sign in
Sign up
Bryan Gin-ge Chen
here to learn
Workspace
Fork
Published unlisted
By
Bryan Gin-ge Chen
Edited
Feb 29, 2020
Fork of
Electricity Usage, 2019
1 star
1
Insert cell
Insert cell
Insert cell
Insert cell
viewof
upload
=
{
const
form
=
Object
.
assign
(
file
(
)
,
{
value
:
FileAttachment
(
"pge-electric-data.csv"
)
}
)
;
return
form
;
}
Insert cell
data
=
d3
.
csvParse
(
await
upload
.
text
(
)
,
parseData
)
Insert cell
parseData
=
d
=>
(
{
date
:
parseDate
(
`${
d
[
"DATE"
]
}T${
d
[
"START TIME"
]
}`
)
,
usage
:
+
d
[
"USAGE"
]
}
)
Insert cell
parseDate
=
d3
.
timeParse
(
"%Y-%m-%dT%H:%M"
)
Insert cell
dateExtent
=
d3
.
extent
(
data
,
d
=>
d
.
date
)
Insert cell
x
=
d3
.
scaleBand
(
d3
.
range
(
24
)
,
[
margin
.
left
,
width
-
margin
.
right
]
)
.
round
(
true
)
Insert cell
y
=
d3
.
scaleBand
(
d3
.
timeDays
(
...
dateExtent
)
,
[
margin
.
top
,
height
-
margin
.
bottom
]
)
.
round
(
true
)
Insert cell
color
=
{
let
[
min
,
max
]
=
d3
.
extent
(
data
,
d
=>
d
.
usage
)
;
if
(
min
<
0
)
{
max
=
Math
.
max
(
-
min
,
max
)
;
return
d3
.
scaleDiverging
(
[
-
max
,
0
,
max
]
,
t
=>
d3
.
interpolateRdBu
(
1
-
t
)
)
;
}
return
d3
.
scaleSequential
(
[
0
,
max
]
,
d3
.
interpolateReds
)
;
}
Insert cell
xAxis
=
g
=>
g
.
attr
(
"transform"
,
`translate(0,${
margin
.
top
})`
)
.
call
(
d3
.
axisTop
(
x
)
.
tickFormat
(
formatHour
)
)
.
call
(
g
=>
g
.
select
(
".domain"
)
.
remove
(
)
)
Insert cell
yAxis
=
g
=>
g
.
attr
(
"transform"
,
`translate(${
margin
.
left
},0)`
)
.
call
(
d3
.
axisLeft
(
y
)
.
tickFormat
(
formatDay
)
)
.
call
(
g
=>
g
.
select
(
".domain"
)
.
remove
(
)
)
Insert cell
formatUsage
=
d3
.
format
(
".2f"
)
Insert cell
formatDate
=
d3
.
timeFormat
(
"%B %-d, %-I %p"
)
Insert cell
formatDay
=
{
const
formatMonth
=
d3
.
timeFormat
(
"%b %-d"
)
;
const
formatDate
=
d3
.
timeFormat
(
"%-d"
)
;
return
d
=>
(
d
.
getDate
(
)
===
1
?
formatMonth
:
formatDate
)
(
d
)
;
}
Insert cell
formatHour
=
{
return
d
=>
d
===
0
?
"12 AM"
:
d
===
12
?
"12 PM"
:
(
d
%
12
)
+
""
;
}
Insert cell
width
=
954
Insert cell
height
=
margin
.
top
+
margin
.
bottom
+
(
d3
.
timeDay
.
count
(
...
dateExtent
)
+
1
)
*
10
Insert cell
margin
=
(
{
top
:
70
,
right
:
0
,
bottom
:
0
,
left
:
40
}
)
Insert cell
d3
=
require
(
"d3@5"
)
Insert cell
import
{
legend
}
from
"@d3/color-legend"
Insert cell
import
{
file
}
from
"@jashkenas/inputs"
Insert cell
One platform
to build and deploy the best data apps
Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Try it for free
Learn more
Compare fork
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
chart
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
upload
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
data
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
parseData
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
parseDate
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
dateExtent
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
x
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
y
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
xAxis
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
yAxis
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
formatUsage
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
formatDate
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
formatDay
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
formatHour
Add comment
Copy import
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
margin
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
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML