PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/assets/javascripts/graphs/stat_graph_contributors_graph.js

https://gitlab.com/the-undefined/gitlab-ce
JavaScript | 278 lines | 224 code | 51 blank | 3 comment | 13 complexity | 8821c6507120bd69cfb96463f535f210 MD5 | raw file
  1. /* eslint-disable */
  2. /*= require d3 */
  3. (function() {
  4. var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  5. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  6. hasProp = {}.hasOwnProperty;
  7. this.ContributorsGraph = (function() {
  8. function ContributorsGraph() {}
  9. ContributorsGraph.prototype.MARGIN = {
  10. top: 20,
  11. right: 20,
  12. bottom: 30,
  13. left: 50
  14. };
  15. ContributorsGraph.prototype.x_domain = null;
  16. ContributorsGraph.prototype.y_domain = null;
  17. ContributorsGraph.prototype.dates = [];
  18. ContributorsGraph.set_x_domain = function(data) {
  19. return ContributorsGraph.prototype.x_domain = data;
  20. };
  21. ContributorsGraph.set_y_domain = function(data) {
  22. return ContributorsGraph.prototype.y_domain = [
  23. 0, d3.max(data, function(d) {
  24. return d.commits = d.commits || d.additions || d.deletions;
  25. })
  26. ];
  27. };
  28. ContributorsGraph.init_x_domain = function(data) {
  29. return ContributorsGraph.prototype.x_domain = d3.extent(data, function(d) {
  30. return d.date;
  31. });
  32. };
  33. ContributorsGraph.init_y_domain = function(data) {
  34. return ContributorsGraph.prototype.y_domain = [
  35. 0, d3.max(data, function(d) {
  36. return d.commits = d.commits || d.additions || d.deletions;
  37. })
  38. ];
  39. };
  40. ContributorsGraph.init_domain = function(data) {
  41. ContributorsGraph.init_x_domain(data);
  42. return ContributorsGraph.init_y_domain(data);
  43. };
  44. ContributorsGraph.set_dates = function(data) {
  45. return ContributorsGraph.prototype.dates = data;
  46. };
  47. ContributorsGraph.prototype.set_x_domain = function() {
  48. return this.x.domain(this.x_domain);
  49. };
  50. ContributorsGraph.prototype.set_y_domain = function() {
  51. return this.y.domain(this.y_domain);
  52. };
  53. ContributorsGraph.prototype.set_domain = function() {
  54. this.set_x_domain();
  55. return this.set_y_domain();
  56. };
  57. ContributorsGraph.prototype.create_scale = function(width, height) {
  58. this.x = d3.time.scale().range([0, width]).clamp(true);
  59. return this.y = d3.scale.linear().range([height, 0]).nice();
  60. };
  61. ContributorsGraph.prototype.draw_x_axis = function() {
  62. return this.svg.append("g").attr("class", "x axis").attr("transform", "translate(0, " + this.height + ")").call(this.x_axis);
  63. };
  64. ContributorsGraph.prototype.draw_y_axis = function() {
  65. return this.svg.append("g").attr("class", "y axis").call(this.y_axis);
  66. };
  67. ContributorsGraph.prototype.set_data = function(data) {
  68. return this.data = data;
  69. };
  70. return ContributorsGraph;
  71. })();
  72. this.ContributorsMasterGraph = (function(superClass) {
  73. extend(ContributorsMasterGraph, superClass);
  74. function ContributorsMasterGraph(data1) {
  75. this.data = data1;
  76. this.update_content = bind(this.update_content, this);
  77. this.width = $('.content').width() - 70;
  78. this.height = 200;
  79. this.x = null;
  80. this.y = null;
  81. this.x_axis = null;
  82. this.y_axis = null;
  83. this.area = null;
  84. this.svg = null;
  85. this.brush = null;
  86. this.x_max_domain = null;
  87. }
  88. ContributorsMasterGraph.prototype.process_dates = function(data) {
  89. var dates;
  90. dates = this.get_dates(data);
  91. this.parse_dates(data);
  92. return ContributorsGraph.set_dates(dates);
  93. };
  94. ContributorsMasterGraph.prototype.get_dates = function(data) {
  95. return _.pluck(data, 'date');
  96. };
  97. ContributorsMasterGraph.prototype.parse_dates = function(data) {
  98. var parseDate;
  99. parseDate = d3.time.format("%Y-%m-%d").parse;
  100. return data.forEach(function(d) {
  101. return d.date = parseDate(d.date);
  102. });
  103. };
  104. ContributorsMasterGraph.prototype.create_scale = function() {
  105. return ContributorsMasterGraph.__super__.create_scale.call(this, this.width, this.height);
  106. };
  107. ContributorsMasterGraph.prototype.create_axes = function() {
  108. this.x_axis = d3.svg.axis().scale(this.x).orient("bottom");
  109. return this.y_axis = d3.svg.axis().scale(this.y).orient("left").ticks(5);
  110. };
  111. ContributorsMasterGraph.prototype.create_svg = function() {
  112. return this.svg = d3.select("#contributors-master").append("svg").attr("width", this.width + this.MARGIN.left + this.MARGIN.right).attr("height", this.height + this.MARGIN.top + this.MARGIN.bottom).attr("class", "tint-box").append("g").attr("transform", "translate(" + this.MARGIN.left + "," + this.MARGIN.top + ")");
  113. };
  114. ContributorsMasterGraph.prototype.create_area = function(x, y) {
  115. return this.area = d3.svg.area().x(function(d) {
  116. return x(d.date);
  117. }).y0(this.height).y1(function(d) {
  118. d.commits = d.commits || d.additions || d.deletions;
  119. return y(d.commits);
  120. }).interpolate("basis");
  121. };
  122. ContributorsMasterGraph.prototype.create_brush = function() {
  123. return this.brush = d3.svg.brush().x(this.x).on("brushend", this.update_content);
  124. };
  125. ContributorsMasterGraph.prototype.draw_path = function(data) {
  126. return this.svg.append("path").datum(data).attr("class", "area").attr("d", this.area);
  127. };
  128. ContributorsMasterGraph.prototype.add_brush = function() {
  129. return this.svg.append("g").attr("class", "selection").call(this.brush).selectAll("rect").attr("height", this.height);
  130. };
  131. ContributorsMasterGraph.prototype.update_content = function() {
  132. ContributorsGraph.set_x_domain(this.brush.empty() ? this.x_max_domain : this.brush.extent());
  133. return $("#brush_change").trigger('change');
  134. };
  135. ContributorsMasterGraph.prototype.draw = function() {
  136. this.process_dates(this.data);
  137. this.create_scale();
  138. this.create_axes();
  139. ContributorsGraph.init_domain(this.data);
  140. this.x_max_domain = this.x_domain;
  141. this.set_domain();
  142. this.create_area(this.x, this.y);
  143. this.create_svg();
  144. this.create_brush();
  145. this.draw_path(this.data);
  146. this.draw_x_axis();
  147. this.draw_y_axis();
  148. return this.add_brush();
  149. };
  150. ContributorsMasterGraph.prototype.redraw = function() {
  151. this.process_dates(this.data);
  152. ContributorsGraph.set_y_domain(this.data);
  153. this.set_y_domain();
  154. this.svg.select("path").datum(this.data);
  155. this.svg.select("path").attr("d", this.area);
  156. return this.svg.select(".y.axis").call(this.y_axis);
  157. };
  158. return ContributorsMasterGraph;
  159. })(ContributorsGraph);
  160. this.ContributorsAuthorGraph = (function(superClass) {
  161. extend(ContributorsAuthorGraph, superClass);
  162. function ContributorsAuthorGraph(data1) {
  163. this.data = data1;
  164. // Don't split graph size in half for mobile devices.
  165. if ($(window).width() < 768) {
  166. this.width = $('.content').width() - 80;
  167. } else {
  168. this.width = ($('.content').width() / 2) - 100;
  169. }
  170. this.height = 200;
  171. this.x = null;
  172. this.y = null;
  173. this.x_axis = null;
  174. this.y_axis = null;
  175. this.area = null;
  176. this.svg = null;
  177. this.list_item = null;
  178. }
  179. ContributorsAuthorGraph.prototype.create_scale = function() {
  180. return ContributorsAuthorGraph.__super__.create_scale.call(this, this.width, this.height);
  181. };
  182. ContributorsAuthorGraph.prototype.create_axes = function() {
  183. this.x_axis = d3.svg.axis().scale(this.x).orient("bottom").ticks(8);
  184. return this.y_axis = d3.svg.axis().scale(this.y).orient("left").ticks(5);
  185. };
  186. ContributorsAuthorGraph.prototype.create_area = function(x, y) {
  187. return this.area = d3.svg.area().x(function(d) {
  188. var parseDate;
  189. parseDate = d3.time.format("%Y-%m-%d").parse;
  190. return x(parseDate(d));
  191. }).y0(this.height).y1((function(_this) {
  192. return function(d) {
  193. if (_this.data[d] != null) {
  194. return y(_this.data[d]);
  195. } else {
  196. return y(0);
  197. }
  198. };
  199. })(this)).interpolate("basis");
  200. };
  201. ContributorsAuthorGraph.prototype.create_svg = function() {
  202. this.list_item = d3.selectAll(".person")[0].pop();
  203. return this.svg = d3.select(this.list_item).append("svg").attr("width", this.width + this.MARGIN.left + this.MARGIN.right).attr("height", this.height + this.MARGIN.top + this.MARGIN.bottom).attr("class", "spark").append("g").attr("transform", "translate(" + this.MARGIN.left + "," + this.MARGIN.top + ")");
  204. };
  205. ContributorsAuthorGraph.prototype.draw_path = function(data) {
  206. return this.svg.append("path").datum(data).attr("class", "area-contributor").attr("d", this.area);
  207. };
  208. ContributorsAuthorGraph.prototype.draw = function() {
  209. this.create_scale();
  210. this.create_axes();
  211. this.set_domain();
  212. this.create_area(this.x, this.y);
  213. this.create_svg();
  214. this.draw_path(this.dates);
  215. this.draw_x_axis();
  216. return this.draw_y_axis();
  217. };
  218. ContributorsAuthorGraph.prototype.redraw = function() {
  219. this.set_domain();
  220. this.svg.select("path").datum(this.dates);
  221. this.svg.select("path").attr("d", this.area);
  222. this.svg.select(".x.axis").call(this.x_axis);
  223. return this.svg.select(".y.axis").call(this.y_axis);
  224. };
  225. return ContributorsAuthorGraph;
  226. })(ContributorsGraph);
  227. }).call(this);