Published
Edited
Nov 14, 2019
2 stars
Insert cell
Insert cell
viewof raw = file()
Insert cell
raw.text()
Insert cell
parsed = d3.csvParse(await raw.text())
Insert cell
processed = {
const sorted = parsed
.filter(
d =>
d.Artist !== "" &&
d.DateAcquired !== "" &&
!/^(Unkn Graph Art|Unknown |Various |Album-)/.test(d.Artist)
)
.map(({ Artist, Department, DateAcquired }) => ({
name: Artist.split(",")[0],
category: Department,
date: DateAcquired.substr(0, 4) + '-01-01' // year only
}))
.sort((a, b) => (a.date > b.date ? 1 : a.date < b.date ? -1 : 0));

let year = sorted[0].date;
const topArtists = new Set();
const artistMap = new Map();
const recordTop = () => {
const top20 = Array.from(artistMap.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 20);
top20.forEach(([artist]) => topArtists.add(artist));
};
for (const d of sorted) {
if (d.date !== year) {
recordTop();
year = d.date;
}
artistMap.set(d.name, (artistMap.get(d.name) || 0) + 1);
}
recordTop();

return { topArtists, topItems: sorted.filter(d => topArtists.has(d.name)) };
}
Insert cell
computed = {
const { topArtists, topItems } = processed;
const results = [];
let year = topItems[0].date;
const artistMap = new Map();
const recordValues = () => {
for (const artist of topArtists.values()) {
const list = artistMap.get(artist);
results.push({
name: artist,
category: list && list[0].category,
value: list ? list.length : 0,
date: year
});
}
};
for (const d of topItems) {
if (year !== d.date) {
recordValues();
year = d.date;
}
if (!artistMap.has(d.name)) artistMap.set(d.name, []);
artistMap.get(d.name).push(d);
}
recordValues();
return results;
}
Insert cell
csv = `date,name,value,category
${computed.map(d => "" + [d.date, d.name, d.value, d.category]).join("\n")}`
Insert cell
d3 = require("d3@5")
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.
Learn more