Published
Edited
Jun 11, 2020
Insert cell
Insert cell
import {
geo_data,
user_data,
similar_tracks
} from '@info474/last-fm-data-source-group-12'
Insert cell
geo_data
Insert cell
md`Which artists appears on the most top 50 list (by country)?`
Insert cell
artists = {
let artists = new Set();
geo_data.map(geo => {
geo.artists.map(artist => {
artists.add({ artist: artist.artist_name, geo: geo.country });
// let curr_artist = artists.find(d, => d.name == artist.artist_name);

// if(curr_artist) {
// curr_artist.countries = curr_artist.countries.push(geo.country)
// }
});
});
let count =
[...artists].ma
return artists;
}
Insert cell
[...artists]
Insert cell
d3 = require("d3")
Insert cell
by_artist = d3
.nest()
.key(d => d.artist)
.entries([...artists])
.sort((a, b) => b.values.length - a.values.length)
.filter((d, i) => i < 50)
Insert cell
chart = {
const svg = d3
.create("svg")
.attr("height", 500)
.attr("width", width);

svg
.selectAll(".artist")
.data(by_artist, d => d.key)
.join("rect")
.on("mouseenter", function(d) {
let text = "";
d.values.forEach(dd => (text = text + " " + dd.geo));
const pos = d3.mouse(this);
tip.attr("transform", `translate(${pos[0]}, ${pos[1] - 10})`);
tipText.text(d.key + " " + d.values.length + ' countries');
const { x, y, width: w, height: h } = tipText.node().getBBox();
tipRect
.attr("width", w)
.attr("height", h)
.attr("x", x)
.attr("y", y)
.attr("fill", "rgb(0, 255, 245)");
})
.on("mousemove", function(d) {
const pos = d3.mouse(this);
tip.attr("transform", `translate(${pos[0]}, ${pos[1] - 10})`);
})
.on("mouseout", function(d) {
// tip.text("");
})
.call(drawRect);

// Add a tooltip
const tip = svg.append("g").style("pointer-events", "none");
const tipRect = tip.append("rect");
const tipText = tip.append("text").style("text-anchor", "middle");

return svg.node();
}
Insert cell
drawRect = ele => {
ele
.attr("x", xScale(0))
.attr("width", d => xScale(d.values.length))
.style("opacity", 1)
.attr("height", yScale.bandwidth())
.attr("y", d => yScale(d.key));
}
Insert cell
xScale = d3
.scaleLinear()
.domain([0, d3.max(by_artist, d => d.values.length)])
.range([200, width])
Insert cell
xScale(d3.max(by_artist, d => d.values.length))
Insert cell
yScale = d3
.scaleBand()
.domain(by_artist.map(d => d.key))
.range([0, 500])
.paddingInner(.1)
Insert cell
yScale("Coldplay")
Insert cell
by_artist.map(d => d.key)
Insert cell
html`<style>
// rect:hover {
// fill:red;
// }
</style>`
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