tripalFeature.adminChart.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. container.append('div')
  10. .attr('id', 'tripal-feature-admin-summary-chart')
  11. .classed('tripal-admin-chart',true);
  12. container.append('div')
  13. .attr('id', 'tripal-feature-admin-summary-figure-desc')
  14. .classed('tripal-admin-figure-desc',true);
  15. }
  16. // Set-up the dimensions for our chart canvas.
  17. // Note: these are adjusted below so think of these are your minimum size.
  18. var margin = {top: 20, right: 20, bottom: 140, left: 100},
  19. width = 960 - margin.left - margin.right,
  20. height = 500 - margin.top - margin.bottom;
  21. var color = d3.scale.ordinal()
  22. .range(["#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99","#e31a1c","#fdbf6f","#ff7f00","#cab2d6","#6a3d9a","#ffff99","#b15928"]);
  23. var formatNum = d3.format("0,000");
  24. // The data was parsed and saved into tripalFeature.admin.summary
  25. // in the preprocess function for this template.
  26. if (Drupal.settings.tripalFeature.admin.summary) {
  27. // Determine the max number of characters in both the type name
  28. // and the total number of features per bar for use in width/magin adjustments.
  29. var maxTypeLength = 0;
  30. var maxTotalLength = 0;
  31. var numBars = Drupal.settings.tripalFeature.admin.summary.length;
  32. for(var i=0; i < numBars; i++){
  33. var element = Drupal.settings.tripalFeature.admin.summary[i];
  34. if(element.name.length > maxTypeLength){
  35. maxTypeLength = element.name.length;
  36. }
  37. if(element.total_features.length > maxTotalLength){
  38. maxTotalLength = element.total_features.length;
  39. }
  40. }
  41. // Ensure a minimum in case something goes wrong...
  42. if (maxTotalLength < 4) { maxTotalLength = 4; }
  43. if (maxTypeLength < 10) { maxTypeLength = 10; }
  44. // Adjust our bottom margin based on the length of type names in the data.
  45. // Assume 4px/character based on the slope of the label.
  46. margin.bottom = maxTypeLength * 4;
  47. // Adjust the width of the chart based on the number or bars (types)
  48. // and the length of the bar totals which need to fit on the top of the bar.
  49. // Assume 9px/character since it's not rotated.
  50. width = numBars * (maxTotalLength * 9);
  51. // Set-up the scales of the chart.
  52. var x0 = d3.scale.ordinal()
  53. .rangeRoundBands([0, width], .1);
  54. var x1 = d3.scale.ordinal();
  55. var y = d3.scale.linear()
  56. .range([height, 0]);
  57. // Now set-up the axis functions.
  58. var xAxis = d3.svg.axis()
  59. .scale(x0)
  60. .orient('bottom');
  61. var yAxis = d3.svg.axis()
  62. .scale(y)
  63. .orient('left')
  64. .ticks(10, '');
  65. // Create our chart canvas.
  66. var svg = d3.select('#tripal-feature-admin-summary-chart').append('svg')
  67. .attr('width', width + margin.left + margin.right)
  68. .attr('height', height + margin.top + margin.bottom)
  69. .append('g')
  70. .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  71. // map the data to the x & y axis' of our chart.
  72. data = Drupal.settings.tripalFeature.admin.summary;
  73. x0.domain(data.map(function(d) { return d.name; }));
  74. x1.domain(Drupal.settings.tripalFeature.admin.organisms).rangeRoundBands([0, x0.rangeBand()]);
  75. y.domain([0, Drupal.settings.tripalFeature.admin.maxBarHeight]);
  76. // Create the x-axis.
  77. var xaxis = svg.append('g')
  78. .attr('class', 'x axis')
  79. .attr('transform', 'translate(0,' + height + ')')
  80. .call(xAxis);
  81. xaxis.selectAll("text")
  82. .style("text-anchor", "end")
  83. .attr("dx", "-.8em")
  84. .attr("dy", ".15em")
  85. .attr("transform", function(d) { return "rotate(-25)"; });
  86. // Label the x-axis.
  87. xaxis.append('g')
  88. .attr('class', 'axis-label')
  89. .attr('transform', 'translate(' + width/2 + ',' + (margin.bottom - 20) + ')')
  90. .append('text')
  91. .attr('font-size', '16px')
  92. .attr('dy', '.71em')
  93. .style('text-anchor', 'middle')
  94. .text('Types of Features');
  95. // Create the y-axis.
  96. var yaxis = svg.append('g')
  97. .attr('class', 'y axis')
  98. .call(yAxis);
  99. // Label the y-axis.
  100. yaxis.append('g')
  101. .attr('class', 'axis-label')
  102. .attr('transform', 'translate(-70,' + height/2 + ')')
  103. .append('text')
  104. .attr('transform', 'rotate(-90)')
  105. .attr('font-size', '16px')
  106. .attr('dy', '.71em')
  107. .style('text-anchor', 'middle')
  108. .text('Total Number of Features');
  109. // Add a g element to contain each set of bars (1 per type).
  110. var type = svg.selectAll(".type")
  111. .data(data)
  112. .enter().append("g")
  113. .attr("class", "g")
  114. .attr("transform", function(d) { return "translate(" + x0(d.name) + ",0)"; });
  115. // Now add the bars :)
  116. // Keep in mind some processing was done in the preprocess function to
  117. // generate the bars array based on the organisms array
  118. // and pre-calculated the y0 & y1 used here.
  119. type.selectAll("rect")
  120. .data(function(d) { return d.bars; })
  121. .enter().append("rect")
  122. .attr("width", x0.rangeBand())
  123. .attr("y", function(d) { return y(d.y1); })
  124. .attr("height", function(d) { return y(d.y0) - y(d.y1); })
  125. .style("fill", function(d) { return color(d.name); })
  126. .append("svg:title")
  127. .text(function(d) { return formatNum(d.y1 - d.y0); });
  128. // Add the total to the top of the bar.
  129. svg.selectAll("g.bar-totals")
  130. .data(data)
  131. .enter().append('g')
  132. .classed('bar-totals', true)
  133. .append("text")
  134. .attr("class", "bar-label")
  135. .attr("text-anchor", "middle")
  136. .attr('font-size', '10px')
  137. .attr("x", function(d) { return x0(d.name) + x0.rangeBand()/2; })
  138. .attr("y", function(d) { return y(d.total_features) -5; })
  139. .text(function(d) { return formatNum(d.total_features); });
  140. // Finally add in a simple legend.
  141. var legend = svg.selectAll(".legend")
  142. .data(Drupal.settings.tripalFeature.admin.organisms.slice().reverse())
  143. .enter().append("g")
  144. .attr("class", "legend")
  145. .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
  146. legend.append("rect")
  147. .attr("x", width - 18)
  148. .attr("width", 18)
  149. .attr("height", 18)
  150. .style("fill", color);
  151. legend.append("text")
  152. .attr("x", width - 24)
  153. .attr("y", 9)
  154. .attr("dy", ".35em")
  155. .style("text-anchor", "end")
  156. .attr('font-style','italic')
  157. .text(function(d) { return d; });
  158. // Add a small blurb mentioning this is from an mview and you should update ;).
  159. d3.selectAll('#tripal-feature-admin-summary-figure-desc')
  160. .html(Drupal.settings.tripalFeature.admin.figureDesc);
  161. }
  162. }
  163. };