Posted By

whitetiger on 11/09/06


Tagged

http javascript js time image html script images web sleep


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


JavaScript - Simple Example Istogramma


 / Published in: JavaScript
 

  1. function istogramma()
  2. {
  3. /*
  4. *
  5. * Prende in input una serie di numeri, sia positivi che negativi
  6. * e crea un istogramma.
  7. */
  8.  
  9. var n = window.prompt('Numero di valori da inserire:', '');
  10.  
  11. n = parseInt(n); // rende il valore se valido Int altrimenti NaN
  12.  
  13. if(isNaN(n) == true)
  14. {
  15. document.write('<p align=\"center\"><b>Non hai selezionato nessun valore...</b></p>');
  16. document.write('<br>');
  17. }
  18. else
  19. {
  20. var num = new Array();
  21. var max = 0;
  22.  
  23. for(var i=0; i<n; i++)
  24. {
  25. var tmp = window.prompt('Inserire Valore [' + (i+1) + '/' + n + ']', '');
  26. tmp = parseFloat(tmp);
  27.  
  28. if(isNaN(tmp) == true)
  29. {
  30. n = i;
  31. break;
  32. }
  33.  
  34. num[i] = tmp;
  35.  
  36. if(max < Math.abs(num[i])) max = Math.abs(num[i]);
  37. }
  38.  
  39. for(var i=0; i<n; i++) num[i] *= 100/max;
  40.  
  41. document.write('<p align=\"center\"><b>Istogramma for JavaScript</b></p>');
  42. document.write('<br>');
  43. document.write('<table align=\"center\" border=\"0\" bgcolor=\"red\">');
  44. document.write('<tr><td>');
  45. document.write('<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"200\" bgcolor=\"black\">');
  46.  
  47. for(var j=0; j<n; j++)
  48. {
  49. document.write('<tr>');
  50.  
  51. for(var i=0; i<200; i++)
  52. {
  53. var aux = num[j];
  54.  
  55. if(num[j] == 0) document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
  56.  
  57. if(num[j] > 0 && i < 100) document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
  58. else if(num[j] > 0 && i >= 100)
  59. {
  60. if((i-100) <= aux)
  61. document.write('<td width=\"1\" bgcolor=\"green\"> </td>');
  62. else
  63. document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
  64. }
  65.  
  66. if(num[j] < 0 && i < (100+aux)) document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
  67. else if(num[j] < 0 && (i >= (100+aux) && i < 100)) document.write('<td width=\"1\" bgcolor=\"yellow\"> </td>');
  68. else if(num[j] < 0 && i >= 100) document.write('<td width=\"1\" bgcolor=\"white\"> </td>');
  69. }
  70.  
  71. document.write('</tr>');
  72. }
  73.  
  74. document.write('</table>');
  75. document.write('</td></tr>');
  76. document.write('</table>');
  77. }
  78. }
  79.  
  80.  
  81. <html>
  82. <head>
  83. <title>Disegnare un Istogramma...</title>
  84. <script src='istogramma.js' type='text/javascript'></script>
  85. </head>
  86.  
  87. <body OnLoad="istogramma();">
  88. </body>
  89. </html>

Report this snippet  

You need to login to post a comment.