tripalFeature.adminChart.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. Drupal.behaviors.tripalFeature_adminSummaryChart = {
  2. attach: function (context, settings) {
  3. // First add the container after the view header.
  4. var container = d3.select('#tripal-feature-admin-summary');
  5. if (container.empty) {
  6. container = d3.select('.view-header').append('div')
  7. .attr('id', 'tripal-feature-admin-summary')
  8. .classed('tripal-admin-summary',true);
  9. }
  10. // Set-up the dimensions for our chart canvas.
  11. var margin = {top: 20, right: 20, bottom: 100, left: 100},
  12. width = 960 - margin.left - margin.right,
  13. height = 500 - margin.top - margin.bottom;
  14. var color = d3.scale.ordinal()
  15. .range(["#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6","#6a3d9a","#ffff99","#b15928"]);
  16. var formatNum = d3.format("0,000");
  17. // Set-up the scales of the chart.
  18. var x0 = d3.scale.ordinal()
  19. .rangeRoundBands([0, width], .1);
  20. var x1 = d3.scale.ordinal();
  21. var y = d3.scale.linear()
  22. .range([height, 0]);
  23. // Now set-up the axis functions.
  24. var xAxis = d3.svg.axis()
  25. .scale(x0)
  26. .orient('bottom');
  27. var yAxis = d3.svg.axis()
  28. .scale(y)
  29. .orient('left')
  30. .ticks(10, '');
  31. // Create our chart canvas.
  32. var svg = d3.select('#tripal-feature-admin-summary').append('svg')
  33. .attr('width', width + margin.left + margin.right)
  34. .attr('height', height + margin.top + margin.bottom)
  35. .append('g')
  36. .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  37. // The data was parsed and saved into tripalFeature.admin.summary
  38. // in the preprocess function for this template.
  39. if (Drupal.settings.tripalFeature.admin.summary) {
  40. // map the data to the x & y axis' of our chart.
  41. data = Drupal.settings.tripalFeature.admin.summary;
  42. x0.domain(data.map(function(d) { return d.name; }));
  43. x1.domain(Drupal.settings.tripalFeature.admin.organisms).rangeRoundBands([0, x0.rangeBand()]);
  44. //y.domain([0, d3.max(data, function(d) { return d3.max(d.organisms, function(d) { return d.value; }); })]);
  45. y.domain([0, d3.max(data, function(d) { return d.total_features; })]);
  46. // Create the x-axis.
  47. var xaxis = svg.append('g')
  48. .attr('class', 'x axis')
  49. .attr('transform', 'translate(0,' + height + ')')
  50. .call(xAxis);
  51. // Wrap the scientific names so they fit better.
  52. xaxis.selectAll(".tick text")
  53. .call(wrap, x0.rangeBand());
  54. // Label the x-axis.
  55. xaxis.append('g')
  56. .attr('class', 'axis-label')
  57. .attr('transform', 'translate(' + width/2 + ',60)')
  58. .append('text')
  59. .attr('font-size', '16px')
  60. .attr('dy', '.71em')
  61. .style('text-anchor', 'middle')
  62. .text('Types of Features');
  63. // Create the y-axis.
  64. var yaxis = svg.append('g')
  65. .attr('class', 'y axis')
  66. .call(yAxis);
  67. // Label the y-axis.
  68. yaxis.append('g')
  69. .attr('class', 'axis-label')
  70. .attr('transform', 'translate(-70,' + height/2 + ')')
  71. .append('text')
  72. .attr('transform', 'rotate(-90)')
  73. .attr('font-size', '16px')
  74. .attr('dy', '.71em')
  75. .style('text-anchor', 'middle')
  76. .text('Total Number of Features');
  77. // Add a g element to contain each set of bars (1 per type).
  78. var type = svg.selectAll(".type")
  79. .data(data)
  80. .enter().append("g")
  81. .attr("class", "g")
  82. .attr("transform", function(d) { return "translate(" + x0(d.name) + ",0)"; });
  83. // Now add the bars :)
  84. // Keep in mind some processing was done in the preprocess function to
  85. // generate the bars array based on the organisms array
  86. // and pre-calculated the y0 & y1 used here.
  87. type.selectAll("rect")
  88. .data(function(d) { return d.bars; })
  89. .enter().append("rect")
  90. .attr("width", x0.rangeBand())
  91. .attr("y", function(d) { return y(d.y1); })
  92. .attr("height", function(d) { return y(d.y0) - y(d.y1); })
  93. .style("fill", function(d) { return color(d.name); })
  94. .append("svg:title")
  95. .text(function(d) { return formatNum(d.y1 - d.y0); });
  96. // Add the total to the top of the bar.
  97. svg.selectAll("g.bar-totals")
  98. .data(data)
  99. .enter().append('g')
  100. .classed('bar-totals', true)
  101. .append("text")
  102. .attr("class", "bar-label")
  103. .attr("text-anchor", "middle")
  104. .attr("x", function(d) { return x0(d.name) + x0.rangeBand()/2; })
  105. .attr("y", function(d) { return y(d.total_features) -5; })
  106. .text(function(d) { return formatNum(d.total_features); });
  107. // Finally add in a simple legend.
  108. var legend = svg.selectAll(".legend")
  109. .data(Drupal.settings.tripalFeature.admin.organisms.slice().reverse())
  110. .enter().append("g")
  111. .attr("class", "legend")
  112. .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
  113. legend.append("rect")
  114. .attr("x", width - 18)
  115. .attr("width", 18)
  116. .attr("height", 18)
  117. .style("fill", color);
  118. legend.append("text")
  119. .attr("x", width - 24)
  120. .attr("y", 9)
  121. .attr("dy", ".35em")
  122. .style("text-anchor", "end")
  123. .attr('font-style','italic')
  124. .text(function(d) { return d; });
  125. // Add a small blurb mentioning this is from an mview and you should update ;).
  126. var blurb = svg.append('g')
  127. .classed('figure-legend', true)
  128. .attr("transform", function(d, i) { return "translate(" + (width - 18) + "," + (height + 50) + ")"; });
  129. blurb.append("svg:a")
  130. .attr("xlink:href", Drupal.settings.tripalFeature.admin.mviewUrl)
  131. .append('text')
  132. .attr('font-style','italic')
  133. .style("fill", '#7F7F7F')
  134. .style("font-size","10px")
  135. .style("text-anchor", "end")
  136. .text("Update Materialized View");
  137. blurb.append('text')
  138. .attr('x', 0)
  139. .attr('y', 20)
  140. .attr('font-style','italic')
  141. .style("fill", '#7F7F7F')
  142. .style("font-size","10px")
  143. .style("text-anchor", "end")
  144. .text('Updated on ' + Drupal.settings.tripalFeature.admin.mviewLastUpdate);
  145. function wrap(text, width) {
  146. text.each(function() {
  147. var text = d3.select(this),
  148. words = text.text().split(/[\s_]+/).reverse(),
  149. word,
  150. lineNumber = 0,
  151. lineHeight = 1.1, // ems
  152. y = text.attr("y"),
  153. dy = parseFloat(text.attr("dy")),
  154. tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
  155. while (word = words.pop()) {
  156. tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
  157. }
  158. });
  159. }
  160. }
  161. }
  162. };