Public
Edited
Dec 15
1 fork
Importers
10 stars
Insert cell
Insert cell
Plot.plot({
style: "overflow: visible;",
y: { grid: true },
marks: [
Plot.ruleY([0]),
Plot.lineY(stocks, { x: "Date", y: "Close", stroke: "Symbol" }),
Plot.text(
stocks,
Plot.selectLast({
x: "Date",
y: "Close",
z: "Symbol",
text: "Symbol",
textAnchor: "start",
dx: 3
})
)
]
})
Insert cell
stocks = [
["AAPL", await FileAttachment("aapl.csv").csv({ typed: true })],
["AMZN", await FileAttachment("amzn.csv").csv({ typed: true })],
["GOOG", await FileAttachment("goog.csv").csv({ typed: true })],
["IBM", await FileAttachment("ibm.csv").csv({ typed: true })]
].flatMap(([Symbol, values]) => values.map((d) => ({ Symbol, ...d })))
Insert cell
Insert cell
Inputs.table(stocks)
Insert cell
Insert cell
Insert cell
Inputs.table(stocks_rotated)
Insert cell
Insert cell
Plot.plot({
style: "overflow: visible;",
y: { grid: true },
marks: [
Plot.ruleY([0]),
Plot.lineY(stocks_rotated, { x: "Date", y: "AAPL", stroke: "red" }),
Plot.lineY(stocks_rotated, { x: "Date", y: "GOOG", stroke: "blue" }),
Plot.lineY(stocks_rotated, { x: "Date", y: "IBM", stroke: "green" }),
Plot.lineY(stocks_rotated, { x: "Date", y: "AMZN", stroke: "orange" })
]
})
Insert cell
Insert cell
/**
* Data is your data, which is assumed to be an array of objects,
* and valueColumns is a list of property names which contain values.
* This will automatically create a _new row_ for each valueColumn,
* and new properties called value & key.
*
* So, say, if you have a table with 5 different data series in it,
* this'll result in 5x the rows of the original because it'll have a row
* per series at each point in time.
*/
function dataRotate(data, valueColumns) {
return data.flatMap((row) => {
const otherColumns = Object.fromEntries(
Object.entries(row).filter(([k, v]) => !valueColumns.includes(k))
);
return valueColumns.map((col) => {
return {
value: row[col],
key: col,
...otherColumns
};
});
});
}
Insert cell
stocks_unrotated = dataRotate(stocks_rotated, ["AAPL", "AMZN", "GOOG", "IBM"])
Insert cell
Inputs.table(stocks_unrotated)
Insert cell
Insert cell
Plot.plot({
style: "overflow: visible;",
y: { grid: true },
marks: [
Plot.ruleY([0]),
Plot.lineY(stocks_unrotated, { x: "Date", y: "value", stroke: "key" }),
Plot.text(
stocks_unrotated,
Plot.selectLast({
x: "Date",
y: "value",
z: "key",
text: "key",
textAnchor: "start",
dx: 3
})
)
]
})
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.
Learn more