Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Começando a desenhar o gráfico</title>
  5. <meta charset="utf-8">
  6. <script src="grafico.js"></script>
  7. </head>
  8. <body>
  9. <canvas id="canvas_grafico" width="500" height="500">
  10. </canvas>
  11. <script>
  12. var canvas = document.getElementById('canvas_grafico');
  13. var context = canvas.getContext('2d');
  14.  
  15. //Criando nosso gráfico
  16. // O canto superior do gráfico deverá ficar na posição (10,10)
  17. // O eixo X terá 400 pixels de largura
  18. // O eixo Y terá 300 pixels de altura
  19. var grafico = new Grafico(context,20,20,400,300);
  20.  
  21.  
  22. var valores = [823, 231, 345, 500, 200, 850, 357, 699];
  23. grafico.colunas(valores);
  24.  
  25. //Iremos desenhar os eixos
  26. // grafico.desenhaEixos();
  27.  
  28.  
  29. </script>
  30. </body>
  31. </html>
  32.  
  33. // arquivo: grafico.js
  34. function Grafico(context, x, y, tamanhoX, tamanhoY) {
  35. //Contexto necessário para desenhar no canvas
  36. this.context = context;
  37. //Posição x e y onde o gráfico será desenhado
  38. this.x = x;
  39. this.y = y;
  40. //Tamanho do eixo X
  41. this.tamanhoX = tamanhoX;
  42. //Tamanho do eixo Y
  43. this.tamanhoY = tamanhoY;
  44.  
  45. }
  46. Grafico.prototype = {
  47. //Método responsável por desenhar o eixo X
  48. desenhaEixoX: function() {
  49. this.context.strokeStyle = "black";
  50. this.context.lineWidth = 3;
  51. this.context.lineCap = "round";
  52. this.context.beginPath();
  53. this.context.moveTo(this.x,this.y+this.tamanhoY );
  54. this.context.lineTo(this.x+this.tamanhoX,this.y+this.tamanhoY );
  55. this.context.stroke();
  56.  
  57. },
  58. //Método responsável por desenhar o eixo y
  59. desenhaEixoY: function() {
  60. this.context.strokeStyle = "black";
  61. this.context.lineWidth = 3;
  62. this.context.lineCap = "round";
  63. this.context.beginPath();
  64. this.context.moveTo(this.x,this.y );
  65. this.context.lineTo(this.x,this.y+this.tamanhoY );
  66. this.context.stroke();
  67.  
  68. },
  69. //Método responsável por desenhar os dois eixos
  70. desenhaEixos: function() {
  71. this.desenhaEixoX();
  72. this.desenhaEixoY();
  73. },
  74. //Função para fazer as colunas
  75. colunas: function(valores){
  76.  
  77.  
  78. }
  79.  
  80.  
  81.  
  82. },
  83. //Retorna o código de uma cor aleatória
  84. corRandomica: function() {
  85. var letras = '0123456789ABCDEF'.split('');
  86. var cor = '#';
  87. for (var i = 0; i < 6; i++ ) {
  88. cor += letras[Math.floor(Math.random() * 16)];
  89. }
  90. return cor;
  91. }
  92. }
  93.  
  94. <canvas id="canvas_grafico" width="500" height="500">
  95. </canvas>
  96.  
  97. // arquivo: grafico.js
  98. function Grafico(context, x, y, tamanhoX, tamanhoY) {
  99. //Contexto necessário para desenhar no canvas
  100. this.context = context;
  101. //Posição x e y onde o gráfico será desenhado
  102. this.x = x;
  103. this.y = y;
  104. //Tamanho do eixo X
  105. this.tamanhoX = tamanhoX;
  106. //Tamanho do eixo Y
  107. this.tamanhoY = tamanhoY;
  108. this.colunas = [];
  109.  
  110. }
  111. Grafico.prototype = {
  112. //Método responsável por desenhar o eixo X
  113. desenhaEixoX: function() {
  114. this.context.strokeStyle = "black";
  115. this.context.lineWidth = 3;
  116. this.context.lineCap = "round";
  117. this.context.beginPath();
  118. this.context.moveTo(this.x,this.y+this.tamanhoY );
  119. this.context.lineTo(this.x+this.tamanhoX,this.y+this.tamanhoY );
  120. this.context.stroke();
  121.  
  122. },
  123. //Método responsável por desenhar o eixo y
  124. desenhaEixoY: function() {
  125. this.context.strokeStyle = "black";
  126. this.context.lineWidth = 3;
  127. this.context.lineCap = "round";
  128. this.context.beginPath();
  129. this.context.moveTo(this.x,this.y );
  130. this.context.lineTo(this.x,this.y+this.tamanhoY );
  131. this.context.stroke();
  132.  
  133. },
  134. //Método responsável por desenhar os dois eixos
  135. desenhaEixos: function() {
  136. this.desenhaEixoX();
  137. this.desenhaEixoY();
  138. },
  139. desenhaColunas: function() {
  140. var x = 0;
  141. var total_colunas = this.colunas.length;
  142. var w = this.tamanhoX / total_colunas;
  143. for (var i in this.colunas) {
  144. var val = this.colunas[i];
  145. var color = this.corRandomica();
  146. this.context.fillStyle = color;
  147. this.context.fillRect(x,this.tamanhoY - val,w,val);
  148. x += w;
  149. }
  150. },
  151. setColunas: function(valores){
  152. for (var i in valores) {
  153. this.colunas.push(valores[i] * this.tamanhoY / 1000);
  154. }
  155. },
  156. //Retorna o código de uma cor aleatória
  157. corRandomica: function() {
  158. var letras = '0123456789ABCDEF'.split('');
  159. var cor = '#';
  160. for (var i = 0; i < 6; i++ ) {
  161. cor += letras[Math.floor(Math.random() * 16)];
  162. }
  163. return cor;
  164. }
  165. }
  166.  
  167. var canvas = document.getElementById('canvas_grafico');
  168. var context = canvas.getContext('2d');
  169.  
  170. //Criando nosso gráfico
  171. var grafico = new Grafico(context,0,0,500,500);
  172.  
  173. var valores = [823, 231, 345, 500, 200, 850, 357, 699];
  174. grafico.setColunas(valores);
  175. grafico.desenhaColunas();
  176. grafico.desenhaEixos();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement