tripalFeature.adminChart.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. Drupal.behaviors.tripalFeature_adminSummaryChart = {
  2. attach: function (context, settings) {
  3. // Set-up the dimensions for our chart canvas.
  4. // Note: these are adjusted below so think of these are your minimum size.
  5. var margin = {top: 20, right: 50, bottom: 20, left: 100},
  6. fullWidth = document.getElementById('tripal-feature-admin-summary').offsetWidth,
  7. width = fullWidth - margin.left - margin.right,
  8. height = 300 - margin.top - margin.bottom;
  9. var color = d3.scale.ordinal()
  10. .range(["#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6","#6a3d9a","#ffff99","#b15928"]);
  11. var formatNum = d3.format("0,000");
  12. // The data was parsed and saved into tripalFeature.admin.summary
  13. // in the preprocess function for this template.
  14. if (Drupal.settings.tripalFeature.admin.summary) {
  15. // Determine the max number of characters in both the type name
  16. // and the total number of features per bar for use in width/magin adjustments.
  17. var maxTypeLength = 0;
  18. var maxTotalLength = 0;
  19. var numBars = Drupal.settings.tripalFeature.admin.summary.length;
  20. for(var i=0; i < numBars; i++){
  21. var element = Drupal.settings.tripalFeature.admin.summary[i];
  22. if(element.name.length > maxTypeLength){
  23. maxTypeLength = element.name.length;
  24. }
  25. if(element.total_features.length > maxTotalLength){
  26. maxTotalLength = element.total_features.length;
  27. }
  28. }
  29. // Ensure a minimum in case something goes wrong...
  30. if (maxTotalLength < 4) { maxTotalLength = 4; }
  31. if (maxTypeLength < 10) { maxTypeLength = 10; }
  32. // Adjust our bottom margin based on the length of type names in the data.
  33. // Assume 4px/character based on the slope of the label.
  34. xAxisHeight = maxTypeLength * 3;
  35. margin.bottom = xAxisHeight + 25;
  36. // Adjust the width of the chart based on the number or bars (types)
  37. // and the length of the bar totals which need to fit on the top of the bar.
  38. // Assume 9px/character since it's not rotated.
  39. if ((width + margin.left + margin.right) < (numBars * (maxTotalLength * 9))) {
  40. width = numBars * (maxTotalLength * 9);
  41. }
  42. // Determine the best place for the legend. Default to top since that
  43. // will for sure not cause conflict... even though it looks better
  44. // on the right ;).
  45. // Logic: If the difference between the max & min bar heights is greater
  46. // than 1/2 the chart height (max bar height) then there "should"
  47. // be room for the chart nested on the right.
  48. minBarHeight = d3.min(Drupal.settings.tripalFeature.admin.summary, function(d,i) { return d.total_features; });
  49. barHeightDifference = Drupal.settings.tripalFeature.admin.maxBarHeight - minBarHeight;
  50. if (barHeightDifference >= Drupal.settings.tripalFeature.admin.maxBarHeight/2) {
  51. Drupal.settings.tripalFeature.admin.legendPosition = 'right';
  52. }
  53. // Also if we need to put the legend along the top we need to
  54. // increase the top margin.
  55. if (Drupal.settings.tripalFeature.admin.legendPosition == 'top') {
  56. // Draw a top legend in the margin.
  57. var columnWidth = d3.max(Drupal.settings.tripalFeature.admin.organisms, function(d,i) {return d.length;}) * 10;
  58. var maxNumColumns = Math.round(width / columnWidth);
  59. var numRows = Math.ceil(Drupal.settings.tripalFeature.admin.organisms.length / maxNumColumns);
  60. var idealNumColumns = Math.round(Drupal.settings.tripalFeature.admin.organisms.length / numRows);
  61. var legendMargin = {
  62. left: (width - (idealNumColumns * columnWidth))/2,
  63. right: (width - (idealNumColumns * columnWidth))/2,
  64. bottom: 25
  65. };
  66. margin.top = margin.top + (numRows * 20) + legendMargin.bottom;
  67. }
  68. // Set-up the scales of the chart.
  69. var x0 = d3.scale.ordinal()
  70. .rangeRoundBands([0, width], .1);
  71. var x1 = d3.scale.ordinal();
  72. var y = d3.scale.linear()
  73. .range([height, 0]);
  74. // Now set-up the axis functions.
  75. var xAxis = d3.svg.axis()
  76. .scale(x0)
  77. .orient('bottom');
  78. var yAxis = d3.svg.axis()
  79. .scale(y)
  80. .orient('left')
  81. .ticks(10, '');
  82. // Create our chart canvas.
  83. var svg = d3.select('#tripal-feature-admin-summary-chart').append('svg')
  84. .attr('width', width + margin.left + margin.right)
  85. .attr('height', height + margin.top + margin.bottom)
  86. .append('g')
  87. .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  88. // map the data to the x & y axis' of our chart.
  89. data = Drupal.settings.tripalFeature.admin.summary;
  90. x0.domain(data.map(function(d) { return d.name; }));
  91. x1.domain(Drupal.settings.tripalFeature.admin.organisms).rangeRoundBands([0, x0.rangeBand()]);
  92. y.domain([0, Drupal.settings.tripalFeature.admin.maxBarHeight]);
  93. // Create the x-axis.
  94. var xaxis = svg.append('g')
  95. .attr('class', 'x axis')
  96. .attr('transform', 'translate(0,' + height + ')')
  97. .call(xAxis);
  98. xaxis.selectAll("text")
  99. .style("text-anchor", "end")
  100. .attr("dx", "-.8em")
  101. .attr("dy", ".15em")
  102. .attr("transform", function(d) { return "rotate(-25)"; });
  103. // Label the x-axis.
  104. xaxis.append('g')
  105. .attr('class', 'axis-label')
  106. .attr('transform', 'translate(' + width/2 + ',' + xAxisHeight + ')')
  107. .append('text')
  108. .attr('font-size', '16px')
  109. .attr('dy', '.71em')
  110. .style('text-anchor', 'middle')
  111. .text('Types of Features');
  112. // Create the y-axis.
  113. var yaxis = svg.append('g')
  114. .attr('class', 'y axis')
  115. .call(yAxis);
  116. // Label the y-axis.
  117. yaxis.append('g')
  118. .attr('class', 'axis-label')
  119. .attr('transform', 'translate(-70,' + height/2 + ')')
  120. .append('text')
  121. .attr('transform', 'rotate(-90)')
  122. .attr('font-size', '16px')
  123. .attr('dy', '.71em')
  124. .style('text-anchor', 'middle')
  125. .text('Total Number of Features');
  126. // Add a g element to contain each set of bars (1 per type).
  127. var type = svg.selectAll(".type")
  128. .data(data)
  129. .enter().append("g")
  130. .attr("class", "g")
  131. .attr("transform", function(d) { return "translate(" + x0(d.name) + ",0)"; });
  132. // Now add the bars :)
  133. // Keep in mind some processing was done in the preprocess function to
  134. // generate the bars array based on the organisms array
  135. // and pre-calculated the y0 & y1 used here.
  136. type.selectAll("rect")
  137. .data(function(d) { return d.bars; })
  138. .enter().append("rect")
  139. .attr("width", x0.rangeBand())
  140. .attr("y", function(d) { return y(d.y1); })
  141. .attr("height", function(d) { return y(d.y0) - y(d.y1); })
  142. .style("fill", function(d) { return color(d.name); })
  143. .append("svg:title")
  144. .text(function(d) { return formatNum(d.y1 - d.y0); });
  145. // Add the total to the top of the bar.
  146. svg.selectAll("g.bar-totals")
  147. .data(data)
  148. .enter().append('g')
  149. .classed('bar-totals', true)
  150. .append("text")
  151. .attr("class", "bar-label")
  152. .attr("text-anchor", "middle")
  153. .attr('font-size', '10px')
  154. .attr("x", function(d) { return x0(d.name) + x0.rangeBand()/2; })
  155. .attr("y", function(d) { return y(d.total_features) -5; })
  156. .text(function(d) { return formatNum(d.total_features); });
  157. // Legend
  158. //---------
  159. // NOTE: we would prefer to have the legend overlapping the chart on the
  160. // right side but this could be a problem if you have a similar count
  161. // for all features (legend overlaps bars) or a large number of types
  162. // (legend gets pushed out of the viewable screen area). In these cases
  163. // switch to a bottom legend.
  164. if (Drupal.settings.tripalFeature.admin.legendPosition == 'top') {
  165. // Draw a bottom legend in the margin.
  166. var legend = svg.append('g')
  167. .classed('legend', true)
  168. .attr('transform', 'translate(' + (legendMargin.left - 20) + ',-' + margin.top + ')');
  169. var legendItem = legend.selectAll('g')
  170. .data(Drupal.settings.tripalFeature.admin.organisms.slice().reverse())
  171. .enter().append("g")
  172. .attr("class", "legend-item")
  173. .attr("transform", function(d,i) {
  174. xOff = (i % idealNumColumns) * columnWidth;
  175. yOff = Math.floor(i / idealNumColumns) * 25;
  176. return "translate(" + xOff + "," + yOff + ")"
  177. });
  178. legendItem.append("rect")
  179. //.attr("x", width - 18)
  180. .attr("width", 18)
  181. .attr("height", 18)
  182. .style("fill", color);
  183. legendItem.append("text")
  184. .attr("x", 24)
  185. .attr("y", 9)
  186. .attr("dy", ".35em")
  187. .style("text-anchor", "start")
  188. .attr('font-style','italic')
  189. .text(function(d) { return d; });
  190. }
  191. else {
  192. // Draw a right inset legend.
  193. var legend = svg.append('g')
  194. .classed('legend', true)
  195. .attr('transform', 'translate(0,-' + margin.top + ')');
  196. var legendItem = legend.selectAll('g')
  197. .data(Drupal.settings.tripalFeature.admin.organisms.slice().reverse())
  198. .enter().append("g")
  199. .attr("class", "legend")
  200. .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
  201. legendItem.append("rect")
  202. .attr("x", width - 18)
  203. .attr("width", 18)
  204. .attr("height", 18)
  205. .style("fill", color);
  206. legendItem.append("text")
  207. .attr("x", width - 24)
  208. .attr("y", 9)
  209. .attr("dy", ".35em")
  210. .style("text-anchor", "end")
  211. .attr('font-style','italic')
  212. .text(function(d) { return d; });
  213. }
  214. }
  215. }
  216. };