Graphs and tables for your Spotify account.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.4 KiB

  1. /**
  2. * Draws the artist count graph as a bubble chart, and appends it the a designated parent element
  3. * @param artistData: the artist counts data as an array of objects, of the format {'name': artist name, 'num_songs': 50}
  4. * @param parentElem: the DOM element to append the artist graph to (as a string)
  5. */
  6. function drawArtistGraph(artistData, parentElem) {
  7. let margin = {top: 20, right: 30, bottom: 30, left: 40};
  8. let width = 960 - margin.right - margin.left;
  9. let height = 540 - margin.top - margin.bottom;
  10. let color = d3.scaleOrdinal(d3.schemeCategory10);
  11. let bubble = d3.pack(artistData)
  12. .size([width, height])
  13. .padding(1.5);
  14. let svg = d3.select(parentElem)
  15. .append("svg")
  16. .attr("width", width + margin.right + margin.left)
  17. .attr("height", height + margin.top + margin.bottom)
  18. .attr("class", "bubble");
  19. let nodes = d3.hierarchy(artistData)
  20. .sum(function(d) { return d.num_songs; });
  21. let node = svg.selectAll(".node")
  22. .data(bubble(nodes).descendants())
  23. .enter()
  24. .filter(function(d) {
  25. return !d.children;
  26. })
  27. .append("g")
  28. .attr("class", "node")
  29. .attr("transform", function(d) {
  30. return "translate(" + d.x + "," + d.y + ")";
  31. });
  32. node.append("title")
  33. .text(function(d) {
  34. return `${d.name}: ${d.num_songs}`;
  35. });
  36. node.append("circle")
  37. .attr("r", function(d) {
  38. return d.r;
  39. })
  40. .style("fill", function(d,i) {
  41. return color(i);
  42. });
  43. // artist name text
  44. node.append("text")
  45. .attr("dy", ".2em")
  46. .style("text-anchor", "middle")
  47. .text(function(d) {
  48. return d.data.name.substring(0, d.r / 3);
  49. })
  50. .attr("font-family", "sans-serif")
  51. .attr("font-size", function(d){
  52. return d.r/5;
  53. })
  54. .attr("fill", "white");
  55. // artist song count text
  56. node.append("text")
  57. .attr("dy", "1.3em")
  58. .style("text-anchor", "middle")
  59. .text(function(d) {
  60. return d.data.num_songs;
  61. })
  62. .attr("font-family", "Gill Sans", "Gill Sans MT")
  63. .attr("font-size", function(d){
  64. return d.r/5;
  65. })
  66. .attr("fill", "white");
  67. d3.select(self.frameElement)
  68. .style("height", height + "px");
  69. }