tripalFeature.adminChart.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: 100, 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. // Create our chart canvas.
  38. var svg = d3.select('#tripal-feature-admin-summary-chart').append('svg')
  39. .attr('width', width + margin.left + margin.right)
  40. .attr('height', height + margin.top + margin.bottom)
  41. .append('g')
  42. .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  43. // The data was parsed and saved into tripalFeature.admin.summary
  44. // in the preprocess function for this template.
  45. if (Drupal.settings.tripalFeature.admin.summary) {
  46. // map the data to the x & y axis' of our chart.
  47. data = Drupal.settings.tripalFeature.admin.summary;
  48. x0.domain(data.map(function(d) { return d.name; }));
  49. x1.domain(Drupal.settings.tripalFeature.admin.organisms).rangeRoundBands([0, x0.rangeBand()]);
  50. y.domain([0, Drupal.settings.tripalFeature.admin.maxBarHeight]);
  51. // Create the x-axis.
  52. var xaxis = svg.append('g')
  53. .attr('class', 'x axis')
  54. .attr('transform', 'translate(0,' + height + ')')
  55. .call(xAxis);
  56. // Wrap the scientific names so they fit better.
  57. xaxis.selectAll(".tick text")
  58. .call(wrap, x0.rangeBand());
  59. // Label the x-axis.
  60. xaxis.append('g')
  61. .attr('class', 'axis-label')
  62. .attr('transform', 'translate(' + width/2 + ',60)')
  63. .append('text')
  64. .attr('font-size', '16px')
  65. .attr('dy', '.71em')
  66. .style('text-anchor', 'middle')
  67. .text('Types of Features');
  68. // Create the y-axis.
  69. var yaxis = svg.append('g')
  70. .attr('class', 'y axis')
  71. .call(yAxis);
  72. // Label the y-axis.
  73. yaxis.append('g')
  74. .attr('class', 'axis-label')
  75. .attr('transform', 'translate(-70,' + height/2 + ')')
  76. .append('text')
  77. .attr('transform', 'rotate(-90)')
  78. .attr('font-size', '16px')
  79. .attr('dy', '.71em')
  80. .style('text-anchor', 'middle')
  81. .text('Total Number of Features');
  82. // Add a g element to contain each set of bars (1 per type).
  83. var type = svg.selectAll(".type")
  84. .data(data)
  85. .enter().append("g")
  86. .attr("class", "g")
  87. .attr("transform", function(d) { return "translate(" + x0(d.name) + ",0)"; });
  88. // Now add the bars :)
  89. // Keep in mind some processing was done in the preprocess function to
  90. // generate the bars array based on the organisms array
  91. // and pre-calculated the y0 & y1 used here.
  92. type.selectAll("rect")
  93. .data(function(d) { return d.bars; })
  94. .enter().append("rect")
  95. .attr("width", x0.rangeBand())
  96. .attr("y", function(d) { return y(d.y1); })
  97. .attr("height", function(d) { return y(d.y0) - y(d.y1); })
  98. .style("fill", function(d) { return color(d.name); })
  99. .append("svg:title")
  100. .text(function(d) { return formatNum(d.y1 - d.y0); });
  101. // Add the total to the top of the bar.
  102. svg.selectAll("g.bar-totals")
  103. .data(data)
  104. .enter().append('g')
  105. .classed('bar-totals', true)
  106. .append("text")
  107. .attr("class", "bar-label")
  108. .attr("text-anchor", "middle")
  109. .attr("x", function(d) { return x0(d.name) + x0.rangeBand()/2; })
  110. .attr("y", function(d) { return y(d.total_features) -5; })
  111. .text(function(d) { return formatNum(d.total_features); });
  112. // Finally add in a simple legend.
  113. var legend = svg.selectAll(".legend")
  114. .data(Drupal.settings.tripalFeature.admin.organisms.slice().reverse())
  115. .enter().append("g")
  116. .attr("class", "legend")
  117. .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
  118. legend.append("rect")
  119. .attr("x", width - 18)
  120. .attr("width", 18)
  121. .attr("height", 18)
  122. .style("fill", color);
  123. legend.append("text")
  124. .attr("x", width - 24)
  125. .attr("y", 9)
  126. .attr("dy", ".35em")
  127. .style("text-anchor", "end")
  128. .attr('font-style','italic')
  129. .text(function(d) { return d; });
  130. // Add a small blurb mentioning this is from an mview and you should update ;).
  131. d3.selectAll('#tripal-feature-admin-summary-figure-desc')
  132. .html(Drupal.settings.tripalFeature.admin.figureDesc);
  133. function wrap(text, width) {
  134. text.each(function() {
  135. var text = d3.select(this),
  136. words = text.text().split(/[\s_]+/).reverse(),
  137. word,
  138. lineNumber = 0,
  139. lineHeight = 1.1, // ems
  140. y = text.attr("y"),
  141. dy = parseFloat(text.attr("dy")),
  142. tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
  143. while (word = words.pop()) {
  144. tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
  145. }
  146. });
  147. }
  148. }
  149. }
  150. };