tripalFeature.adminChart.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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("text.bar")
  98. .data(data)
  99. .enter().append("text")
  100. .attr("class", "bar-label")
  101. .attr("text-anchor", "middle")
  102. .attr("x", function(d) { return x0(d.name) + x0.rangeBand()/2; })
  103. .attr("y", function(d) { return y(d.total_features) -5; })
  104. .text(function(d) { return formatNum(d.total_features); });
  105. // Finally add in a simple legend.
  106. var legend = svg.selectAll(".legend")
  107. .data(Drupal.settings.tripalFeature.admin.organisms.slice().reverse())
  108. .enter().append("g")
  109. .attr("class", "legend")
  110. .attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
  111. legend.append("rect")
  112. .attr("x", width - 18)
  113. .attr("width", 18)
  114. .attr("height", 18)
  115. .style("fill", color);
  116. legend.append("text")
  117. .attr("x", width - 24)
  118. .attr("y", 9)
  119. .attr("dy", ".35em")
  120. .style("text-anchor", "end")
  121. .attr('font-style','italic')
  122. .text(function(d) { return d; });
  123. function wrap(text, width) {
  124. text.each(function() {
  125. var text = d3.select(this),
  126. words = text.text().split(/[\s_]+/).reverse(),
  127. word,
  128. lineNumber = 0,
  129. lineHeight = 1.1, // ems
  130. y = text.attr("y"),
  131. dy = parseFloat(text.attr("dy")),
  132. tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
  133. while (word = words.pop()) {
  134. tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
  135. }
  136. });
  137. }
  138. }
  139. }
  140. };