tripal.dashboard.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (function ($) {
  2. Drupal.behaviors.tripalDashboard = {
  3. attach: function (context, settings) {
  4. /**
  5. * Renders the graphs for the Admin Dashboard.
  6. *
  7. * id: the element id into which the charts will be drawn.
  8. * fData: The data to be rendered in graphs.
  9. */
  10. function barchart2(id, parent, data) {
  11. // Set aside 10 colors
  12. var c10 = d3.scale.category10();
  13. // Set some default margins.
  14. var m = [30, 100, 10, 120];
  15. // Set the width of the viewport to fit inside the block.
  16. var w = $(parent).width() - m[1] - m[3] - 50;
  17. // Set the height to be tall enough to read each legend and surrounding
  18. // margins.
  19. var h = (data.length * 30) - m[0] - m[2]
  20. var format = d3.format(",.0f");
  21. var x = d3.scale.linear().range([0, w]),
  22. y = d3.scale.ordinal().rangeRoundBands([0, h], .1);
  23. var xAxis = d3.svg.axis().scale(x).orient("top").tickSize(-h),
  24. yAxis = d3.svg.axis().scale(y).orient("left").tickSize(0);
  25. var svg = d3.select(id).append("svg")
  26. .attr("width", w + m[1] + m[3])
  27. .attr("height", h + m[0] + m[2])
  28. .append("g")
  29. .attr("transform", "translate(" + m[3] + "," + m[0] + ")");
  30. // Parse numbers, and sort by count.
  31. data.forEach(function(d) { d.count = +d.count; });
  32. data.sort(function(a, b) { return b.count - a.count; });
  33. // Set the scale domain.
  34. x.domain([0, d3.max(data, function(d) { return d.count; })]);
  35. y.domain(data.map(function(d) { return d.name; }));
  36. var bar = svg.selectAll("g.bar")
  37. .data(data)
  38. .enter().append("g")
  39. .attr("class", "bar")
  40. .attr("transform", function(d) { return "translate(0," + y(d.name) + ")"; });
  41. bar.append("rect")
  42. .attr("width", function(d) { return x(d.count); })
  43. .attr("height", y.rangeBand())
  44. .attr('fill', function(d, i) { return c10(i); });
  45. bar.append("text")
  46. .attr("class", "count")
  47. .attr("x", function(d) { return x(d.count); })
  48. .attr("y", y.rangeBand() / 2)
  49. .attr("dx", 50)
  50. .attr("dy", ".35em")
  51. .attr("text-anchor", "end")
  52. .text(function(d) { return format(d.count); });
  53. svg.append("g")
  54. .attr("class", "x axis")
  55. .call(xAxis);
  56. svg.append("g")
  57. .attr("class", "y axis")
  58. .call(yAxis);
  59. }
  60. // Now insert the bar chart.
  61. barchart2('#tripal-entity-type-chart', '#block-tripal-content-type-barchart', entityCountListing);
  62. }
  63. };
  64. }) (jQuery);