gliba 09.02.2010 12:48
Cоздаем файл "data.txt" с данными типа
China;1286; India;1049 USA;290 Indonesia;234 Brazil;182
Создаем файл "histo.htm" со скриптом, где добавляем новые команды
var fso = new ActiveXObject( "Scripting.FileSystemObject" );
и read_data() для чтения массивов из файла.
var sInputFile = "c:\\myscripts\\data.txt"; //!!двойные обратные слеши!!
<script type="text/javascript"> //red, orange, yellow, green, blue, violet, cyan, gray var arrCol8 = new Array( "#ff0000", "#ffaa00", "#ffff00", "#00ff00", "#00aaff", "#0000bb", "#aa00aa", "#cccccc" );
function drawGraphHoriz( arrText, arrValue, width, border ) { var i, j; if( "" + width == "undefined" ) width = 400; if( "" + border == "undefined" ) border = 1; var nCol = arrCol8.length; var len = arrText.length; var dMax = 0, iWidthSpan; for( i = 0; i < arrValue.length; i++ ) { if( dMax < arrValue[ i ] ) dMax = arrValue[ i ]; }
document.writeln( "<table border='" + border + "'>" ); for( i = 0; i < arrText.length; i++ ) { j = i % nCol; iWidthSpan = Math.ceil( ( arrValue[ i ] * width ) / dMax ); document.writeln( "<tr>" ); document.writeln( "<td>" + arrText[ i ] + "</td>" ); document.writeln( "<td><div style='background-color:" + arrCol8[ j ] + "; width:" + iWidthSpan + "'> " + arrValue[ i ] + "</div></td>" ); document.writeln( "</tr>" ); } document.writeln( "</table>" ); }
function drawGraphVert( arrText, arrValue, height, border, width ) { var i, j; if( "" + height == "undefined" ) height = 400; if( "" + border == "undefined" ) border = 1; if( "" + width == "undefined" ) width = 0; var nCol = arrCol8.length; var len = arrText.length; var dMax = 0, iHeightSpan; for( i = 0; i < arrValue.length; i++ ) { if( dMax < arrValue[ i ] ) dMax = arrValue[ i ]; }
document.writeln( "<table border='" + border + "'>" ); document.writeln( "<tr>" ); for( i = 0; i < arrText.length; i++ ) { j = i % nCol; iHeightSpan = Math.ceil( ( arrValue[ i ] * height ) / dMax ); document.writeln( "<td align='center' valign='bottom'><div style='background-color:" + arrCol8[ j ] + "; width:30px; height:" + iHeightSpan + "px'> " + arrValue[ i ] + "</div></td>" ); } document.writeln( "</tr>" ); document.writeln( "<tr>" ); for( i = 0; i < arrText.length; i++ ) { document.writeln( "<td" ); if( width > 0 ) document.writeln( " width='" + width + "'" ); document.writeln( " align='center'>" + arrText[ i ] + "</td>" ); } document.writeln( "</tr>" ); document.writeln( "</table>" ); }
function read_data( sInputFile ) { var ForReading = 1; var f = fso.OpenTextFile( sInputFile, ForReading ); while (!f.AtEndOfStream) { var r = f.ReadLine(); var arr = r.split( ";" ); if( arr.length >= 2 ) { arrText[ arrText.length ] = arr[ 0 ]; arrValue[ arrValue.length ] = 1 * arr[ 1 ]; } } // for( var i = 0; i < arrText.length; i++ ) // document.write ( i + " " + arrText[ i ] + "," + arrValue[ i ] + "<br />" ); } var arrText = new Array(); var arrValue = new Array();
var sInputFile = "c:\\myscripts\\data.txt"; var fso = new ActiveXObject( "Scripting.FileSystemObject" ); read_data( sInputFile ); drawGraphVert( arrText, arrValue, 200, 0, 60 ); drawGraphHoriz( arrText, arrValue, 600, 0 );
</script>
|