tripalFeature.adminChart.js 6.4 KB

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