stats.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. function BarGraph(context) {
  2. var graph = this;
  3. this.values = [];
  4. this.labels = [];
  5. this.setLabels = function(labels){
  6. this.labels = labels;
  7. }
  8. this.setValues = function(values){
  9. this.values = values;
  10. }
  11. this.draw = function () {
  12. var values = graph.values;
  13. var barW;
  14. var barH;
  15. var barB = 1; // Border
  16. var ratio;
  17. var maxH;
  18. var max;
  19. var i;
  20. // Canvas dimensions
  21. context.canvas.width = graph.width;
  22. context.canvas.height = graph.height;
  23. // Width of each bar
  24. barW = graph.width / values.length - graph.margin * 2;
  25. maxH = graph.height - 30;
  26. // Reference height
  27. var max = 0;
  28. for (i = 0; i < values.length; i ++) {
  29. if (values[i] > max) {
  30. max = values[i];
  31. }
  32. }
  33. // Loop bars
  34. for (i = 0; i < values.length; i ++) {
  35. // Compare with the max value
  36. barH = maxH * values[i] / max;
  37. graph.height -= 15;
  38. // Value
  39. context.fillStyle = "#003300";
  40. context.font = "bold 12px sans-serif";
  41. context.textAlign = "center";
  42. context.fillText(parseInt(values[i], 10), i * graph.width / values.length + (graph.width / values.length) / 2, graph.height - barH - 2);
  43. // Bar background (border)
  44. context.fillStyle = "#003300";
  45. context.fillRect(graph.margin + i * graph.width / values.length, graph.height - barH, barW, barH);
  46. // Bar fill
  47. context.fillStyle = "#ccffcc";
  48. context.fillRect(graph.margin + i * graph.width / values.length + barB, graph.height - barH + barB, barW - barB * 2, barH - barB * 2);
  49. // Label
  50. graph.height += 15;
  51. context.fillStyle = "#003300";
  52. context.font = "10px sans-serif";
  53. context.textAlign = "center";
  54. context.fillText(graph.labels[i], i * graph.width / values.length + (graph.width / values.length) / 2, graph.height - 5);
  55. }
  56. };
  57. this.width = 450;
  58. this.height = 150;
  59. this.margin = 1;
  60. }
  61. function HBarGraph(context) {
  62. var graph = this;
  63. this.values = [];
  64. this.labels = [];
  65. this.setLabels = function(labels){
  66. this.labels = labels;
  67. }
  68. this.setValues = function(values){
  69. this.values = values;
  70. }
  71. this.draw = function () {
  72. var values = graph.values;
  73. var barW;
  74. var barH;
  75. var barB = 1; // Border
  76. var ratio;
  77. var maxH;
  78. var max;
  79. var i;
  80. // Canvas dimensions
  81. context.canvas.width = graph.width;
  82. context.canvas.height = graph.height;
  83. // Height of each bar
  84. barH = graph.height / values.length - graph.margin * 2;
  85. maxW = graph.width - 200;
  86. // Reference width
  87. var max = 0;
  88. for (i = 0; i < values.length; i ++) {
  89. if (values[i] > max) {
  90. max = values[i];
  91. }
  92. }
  93. // Loop bars
  94. for (i = 0; i < values.length; i ++) {
  95. // Compare with the max value
  96. barW = maxW * values[i] / max;
  97. // Label
  98. context.fillStyle = "#003300";
  99. context.font = "10px sans-serif";
  100. context.textAlign = "right";
  101. context.fillText(graph.labels[i], 150, barH / 2 + (graph.margin + i * graph.height / values.length));
  102. // Bar background (border)
  103. context.fillStyle = "#003300";
  104. context.fillRect(160, graph.margin + i * graph.height / values.length, barW, barH);
  105. // Bar fill
  106. context.fillStyle = "#ccffcc";
  107. context.fillRect(160 + barB, barB + (graph.margin + i * graph.height / values.length), barW - (2 * barB), barH - (2 * barB));
  108. // Value
  109. context.fillStyle = "#003300";
  110. context.font = "bold 12px sans-serif";
  111. context.textAlign = "left";
  112. context.fillText(parseInt(values[i], 10), 170 + barW, barH / 2 + (graph.margin + i * graph.height / values.length));
  113. }
  114. };
  115. this.width = 450;
  116. this.height = 250;
  117. this.margin = 1;
  118. }
  119. function PieGraph(context) {
  120. var graph = this;
  121. this.values = [];
  122. this.labels = [];
  123. this.setLabels = function(labels){
  124. this.labels = labels;
  125. }
  126. this.setValues = function(values){
  127. this.values = values;
  128. }
  129. this.draw = function () {
  130. var values = graph.values;
  131. var barW;
  132. var barH;
  133. var barB = 1; // Border
  134. var ratio;
  135. var maxH;
  136. var max;
  137. var i;
  138. // Canvas dimensions
  139. context.canvas.width = graph.width;
  140. context.canvas.height = graph.height;
  141. // Sum of values
  142. var sum = 0;
  143. for (i = 0; i < values.length; i ++) {
  144. sum = sum + values[i]
  145. }
  146. // Loop arcs
  147. var iAngle = (-0.5) * Math.PI;
  148. var hLegend = 30;
  149. for (i = 0; i < values.length; i ++) {
  150. // Calculate angle
  151. var v = (values[i] * 2 / sum) * Math.PI;
  152. // Draw arc
  153. context.fillStyle = "#00" + (3 * (i + 3)).toString().substr(-1) + (3 * (i + 3)).toString().substr(-1) + "00";
  154. context.strokeStyle = "#00" + (3 * (i + 3)).toString().substr(-1) + (3 * (i + 3)).toString().substr(-1) + "00";
  155. context.beginPath();
  156. context.arc(100, 100, 50, iAngle, iAngle + v);
  157. context.lineWidth = 80;
  158. context.stroke();
  159. // Legend
  160. var label = graph.labels[i] + " (" + (values[i] * 100 / sum).toFixed(2) + "%)";
  161. context.beginPath();
  162. context.arc(215, hLegend, 10, 0, 2 * Math.PI);
  163. context.fill();
  164. context.font = "10px sans-serif";
  165. context.textAlign = "left";
  166. context.fillText(label, 230, hLegend + 5);
  167. // Prepare for next
  168. iAngle = iAngle + v;
  169. hLegend = hLegend + 30;
  170. }
  171. };
  172. this.width = 380;
  173. this.height = 200;
  174. this.margin = 1;
  175. }