Published
Edited
Jul 24, 2021
Insert cell
md`# Testing Plot with SP500 (format date)`

Insert cell
sp500 = FileAttachment("sp500.csv").csv({typed: true})
Insert cell
md` ## Convert date to correct JS objects with dateParser`
Insert cell
sp2 = sp500.map( d => (
{
date: dateParser(d.date),
price: d.price
}
)
)
Insert cell
md`### Format date accordingly, MMM day YYYY`
Insert cell
md` Refer to date format here (d3-time-format) [https://github.com/d3/d3-time-format]
`
Insert cell
dateParser = d3.timeParse("%b %d %Y")

Insert cell
md`### Get first and last array for date`
Insert cell
startDate = sp2[0].date
Insert cell
endDate = sp2[122].date
Insert cell
md`### Format X axis labels`
Insert cell
Plot.plot( {x: {type: "time", domain: [startDate, endDate]}, grid: true})
Insert cell
md` ### How to format date in month?`
Insert cell
Plot.plot({
grid: true,
width:1200,
height: 600,
y: {
label: "Price",
},
x: {
label: "Year",
},
marks: [
Plot.ruleY([0]),
Plot.dot(sp2, {x: "date", y: "price", stroke: "price"})
]
})
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.
Learn more