organism-bubble-plot.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* d3js graph of phylotree organisms
  2. selector : jquery selector for div to insert svg content
  3. data : json data
  4. options : { fill : color function for each node,
  5. width : svg width & height,
  6. height : svg height }
  7. */
  8. function organismBubblePlot(selector, data, options) {
  9. /* Flatten the tree structure to sum organisms. the bubble layout
  10. * expects a name property, but the organismColor function expects
  11. * the abbreviation property to exist as well. */
  12. var organisms = {};
  13. var countOrganisms = function(node, i, arr) {
  14. if (node.fo_organism_id) {
  15. if (!organisms[node.fo_organism_id]) {
  16. // Copy only the organisms related metadata, discarding feature info.
  17. organisms[node.fo_organism_id] = {
  18. 'organism_id' : node.fo_organism_id,
  19. 'name' : node.fo_abbreviation,
  20. 'abbreviation' : node.fo_abbreviation,
  21. 'genus' : node.fo_genus,
  22. 'species' : node.fo_species,
  23. 'common_name' : node.fo_common_name,
  24. 'organism_node_id' : node.fo_organism_node_id,
  25. 'value' : 1 // count
  26. };
  27. }
  28. else {
  29. organisms[node.fo_organism_id]['value']++;
  30. }
  31. }
  32. if(node.children) {
  33. node.children.forEach(countOrganisms);
  34. }
  35. }
  36. data.children.forEach(countOrganisms);
  37. // flatten counts hash into an array
  38. var organismsArr = [];
  39. for (var organismId in organisms) {
  40. organismsArr.push( organisms[organismId] );
  41. }
  42. options = options || {};
  43. var fill = options.fill || function(node) {
  44. return 'cyan';
  45. };
  46. var nodeMouseOver = options.nodeMouseOver || function(d) {};
  47. var nodeMouseOut = options.nodeMouseOut || function(d) {};
  48. var nodeMouseDown = options.nodeMouseDown || function(d) {};
  49. var w = options.width || d3.select(selector).style('width') || d3.select(selector).attr('width'),
  50. h = options.height || d3.select(selector).style('height') || d3.select(selector).attr('height'),
  51. w = parseInt(w),
  52. h = parseInt(h);
  53. var format = d3.format(",d");
  54. var bubble = d3.layout.pack()
  55. .sort(null)
  56. .size([w, h]);
  57. var vis = d3.select(selector).append("svg:svg")
  58. .attr("width", w +'px')
  59. .attr("height", h +'px')
  60. .attr("class", "bubble");
  61. var node = vis.selectAll("g.node")
  62. .data(bubble.nodes( { 'children': organismsArr } )
  63. .filter(function(d) { return !d.children; }))
  64. .enter().append("svg:g")
  65. .attr("class", "node")
  66. .on('click', nodeMouseDown)
  67. .on('mouseover', nodeMouseOver)
  68. .on('mouseout', nodeMouseOut)
  69. .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
  70. node.append("svg:title")
  71. .text(function(d) { return d.name + ": " + format(d.value); });
  72. node.append("svg:circle")
  73. .attr("r", function(d) { return d.r; })
  74. .style("fill", function(d) { return fill(d); });
  75. node.append("svg:text")
  76. .attr("text-anchor", "middle")
  77. .attr('font-size', '90%')
  78. .attr("dy", ".3em")
  79. .text(function(d) {
  80. return d.name + ' (' + d.value +')';
  81. });
  82. d3.select(self.frameElement).style("height", h + "px");
  83. }