languagedistgraph.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * GitPHP language distribution graph
  3. *
  4. * Display language distribution graph
  5. *
  6. * @author Christopher Han <xiphux@gmail.com>
  7. * @copyright Copyright (c) 2012 Christopher Han
  8. * @package GitPHP
  9. * @subpackage Javascript
  10. */
  11. define(["modules/geturl", "modules/getproject", "d3"],
  12. function(url, project) {
  13. var width = 600;
  14. var height = 600;
  15. var radius = 200;
  16. var innerRadius = 100;
  17. var growRadius = 20;
  18. var pie = null;
  19. var color = null;
  20. var svg = null;
  21. var arcGroup = null;
  22. var arc = null;
  23. var grownArc = null;
  24. var placeholderGroup = null;
  25. var placeholder = null;
  26. var centerGroup = null;
  27. var langLabel = null;
  28. var countLabel = null;
  29. var filesLabel = null;
  30. var pieTween = function(d, i) {
  31. var i = d3.interpolate({startAngle: 0, endAngle: 0}, {startAngle: d.startAngle, endAngle: d.endAngle});
  32. return function(t) {
  33. return arc(i(t));
  34. };
  35. };
  36. var init = function(graphContainer) {
  37. pie = d3.layout.pie().value(function(d) {
  38. return d.value;
  39. });
  40. color = d3.scale.category20();
  41. svg = d3.select(graphContainer).append("svg")
  42. .attr("width", width)
  43. .attr("height", height);
  44. placeholderGroup = svg.append("g")
  45. .attr("transform", "translate(" + (width/2) + "," + (height/2) + ")");
  46. arcGroup = svg.append("g")
  47. .attr("transform", "translate(" + (width/2) + "," + (height/2) + ")");
  48. centerGroup = svg.append("g")
  49. .attr("transform", "translate(" + (width/2) + "," + (height/2) + ")");
  50. langLabel = centerGroup.append("text")
  51. .attr("dy", -25)
  52. .attr("font-size", "16")
  53. .attr("text-anchor", "middle")
  54. .style('opacity', 0);
  55. countLabel = centerGroup.append("text")
  56. .attr("dy", 0)
  57. .attr("text-anchor", "middle")
  58. .attr("font-size", "20")
  59. .text("Loading");
  60. filesLabel = centerGroup.append("text")
  61. .attr("dy", 20)
  62. .attr("text-anchor", "middle")
  63. .attr("fill", "gray")
  64. .attr("font-size", "12")
  65. .text("files");
  66. placeholder = placeholderGroup.append("path")
  67. .attr("fill", "#EFEFEF")
  68. .attr("d", d3.svg.arc().innerRadius(innerRadius).outerRadius(radius).startAngle(0).endAngle(6.28318531)());
  69. arc = d3.svg.arc().innerRadius(innerRadius).outerRadius(radius);
  70. grownArc = d3.svg.arc().innerRadius(innerRadius).outerRadius(radius + growRadius);
  71. d3.json(url + "?p=" + project + "&a=graphdata&g=languagedist", function(data) {
  72. var dataEntries = d3.entries(data);
  73. var count = 0;
  74. if (dataEntries.length > 0) {
  75. dataEntries.forEach(function(d) {
  76. count += d.value;
  77. });
  78. }
  79. countLabel.text(count);
  80. var paths = arcGroup.selectAll("path").data(pie(dataEntries));
  81. paths.enter().append("path")
  82. .attr("stroke", "white")
  83. .attr("stroke-width", 0.5)
  84. .attr("fill", function(d, i) { return color(i); })
  85. .transition()
  86. .duration(750)
  87. .attrTween("d", pieTween)
  88. .each("end", function() {
  89. placeholderGroup.remove();
  90. });
  91. arcGroup.selectAll("path").on("mouseover", function(d) {
  92. d3.select(this).transition()
  93. .duration(250)
  94. .attr("d", grownArc);
  95. langLabel.transition().style('opacity', 1).text(d.data.key);
  96. countLabel.text(d.data.value);
  97. })
  98. .on("mouseout", function(d) {
  99. d3.select(this).transition()
  100. .duration(250)
  101. .attr("d", arc);
  102. langLabel.transition().style('opacity', 0);
  103. countLabel.text(count);
  104. });
  105. });
  106. };
  107. return {
  108. init: init
  109. };
  110. }
  111. );