home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2001 August
/
PCWorld_2001-08_cd.bin
/
Komunikace
/
sambar
/
_setup.1
/
BARCHART.JS
< prev
next >
Wrap
Text File
|
1998-08-07
|
17KB
|
555 lines
<SCRIPT Language="JavaScript">
<!--
////////////////////////////////////////////////////////////////////////
// Bar Chart v4.0 - 7/4/98
// Copyright (C) 1998 Michael Brownlow
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// For questions and comments, please email the author at:
// starten@atdot.org
// http://ctelcom.net/mike/java/barchart/
// http://ctelcom.net/mike/
////////////////////////////////////////////////////////////////////////
// Chart constructor:
/////////////////////
// title : Title for graph
// hlabel : Label for horizontal axis
// vlabel : Label for vertical axis
// orientation : "horizontal" or "vertical"
// hsize : length of horizontal axis (left and right)
// vsize : length of vertical axis (up and down)
// barsize : width/length of bar as a percentage of the axis length
// it is on divided by the number of bars
// tickimg : image to use for scale tick
// baseimg : image to make base of graph
// ticks : approximate # of ticks to show
function Chart (
title,
hlabel,
vlabel,
orientation,
hsize,
vsize,
barsize,
tickimg,
baseimg,
ticks
) {
// User defined
this.title = title;
this.hlabel = hlabel;
this.vlabel = vlabel;
this.orientation = orientation;
this.hsize = hsize;
this.vsize = vsize;
this.barsize = barsize;
this.tickimg = tickimg;
this.baseimg = baseimg;
this.ticks = ticks;
// System defined
this.scale = new Array();
this.data = new Array();
this.scalesize = 0;
this.labelsize = 0;
this.scaledelta = 0;
}
// Data constructor:
////////////////////
// label : the label to show for the bar
// num : the number to use (can be int or float)
// img : the image to use for the bar
// url : the url for the bar to link to ("-1" for no link)
function Data (label, num, img, url) {
this.label = label;
this.num = num;
this.img = img;
this.url = url;
}
///////////////////////////////////////////////
// Special definitions
var border = 0;
///////////////////////////////////////////////
// __chart : chart to plot
// popup : 1 or 0
// preinit : if you already have a window to draw to specify it
// here, otherwise use "__default"
function doChart(__chart,popup,preinit) {
if(popup == 1) {
if(preinit != "__default") {
drawChart(__chart,preinit);
} else {
__popup = open("","__popup","width=400,height=300");
__popup.document.write("<BODY Bgcolor=#FFFFFF>\n");
drawChart(__chart,__popup);
__popup.document.close();
}
} else {
drawChart(__chart,window);
}
}
///////////////////////////////////////////////
function initChart(__chart,__win) {
var max = __chart.data[0].num;
var min = __chart.data[0].num;
for(var i=0; i<(__chart.data.length); i++) {
if(max <= __chart.data[i].num)
max = __chart.data[i].num;
if(min >= __chart.data[i].num)
min = __chart.data[i].num;
}
computeAutoScale(max,min,__chart);
if(__chart.orientation == "vertical") {
__chart.labelsize = __chart.hsize / __chart.data.length
} else {
__chart.labelsize = __chart.vsize / __chart.data.length
}
__chart.barsize = (__chart.barsize / 100) * __chart.labelsize;
}
function drawChart(__chart,__win) {
initChart(__chart,__win);
__win.document.write(
"<TABLE Border=" + border + " Cellpadding=0 Cellspacing=0><TR>"
);
doTitle(__chart,__win);
__win.document.write("</TR><TR>");
doVlabel(__chart,__win);
doTopL(__chart,__win);
doTopR(__chart,__win);
__win.document.write("</TR><TR><TD></TD>");
doBotL(__chart,__win);
doBotR(__chart,__win);
__win.document.write("</TR><TR>");
doHlabel(__chart,__win);
__win.document.write("</TR></TABLE>");
doFooter(__chart,__win);
}
// Finds decent scaling for an array of numbers
function computeAutoScale(max,min,__chart) {
if(max == min) {
max+=1;
min-=1;
}
var ticks = __chart.ticks;
range = nicenum(max - min, 0);
d = nicenum(range/(ticks - 1), 1);
graphmin = Math.floor(min/d)*d;
if(graphmin>0) graphmin = 0;
graphmax = Math.ceil(max/d)*d;
if(graphmax<0) graphmax = 0;
nfrac = Math.max( -Math.floor(Math.log(d)/Math.E), 0);
i = 0;
for (x=graphmin; x < (graphmax+0.5*d); x+=d) {
__chart.scale[i] = doRound(x,nfrac,"after");
i++;
}
__chart.scaledelta = d;
// Make the chart stay an apprx size
if(__chart.orientation == "vertical")
__chart.scalesize = doRound(__chart.vsize / __chart.scale.length,
0, "after");
else
__chart.scalesize = doRound(__chart.hsize / __chart.scale.length,
0, "after");
}
// Finds a nice number
function nicenum(x,round) {
var expt = Math.floor(Math.log(x)/Math.E);
var f = x/Math.pow(10,expt);
var nf;
if(round == 1) {
if (f< 1.5) nf = 1.;
else if (f<3) nf = 2.;
else if (f<7) nf = 5.;
else nf = 10.;
} else {
if (f<= 1) nf = 1.;
else if (f<=2) nf = 2.;
else if (f<=5) nf = 5.;
else nf = 10.;
}
return nf*Math.pow(10, expt);
}
// Rounds 'num' to 'p' places 'after'/'before' the decimal point
function doRound(num,p,point) {
p = Math.pow(10,p);
if(point == "after")
num = Math.round(parseFloat(num)*(p))/(p);
else
num = Math.round(parseFloat(num)/(p))*(p);
return num;
}
function doTitle(__chart,__win) {
__win.document.write(
"<TD></TD><TD></TD><TD Align=center>" +
"<TABLE Width=" + __chart.hsize + " Border=" + border +
" Cellpadding=3 Cellspacing=0><TR>" +
"<TD Align=center><FONT Size=4><B>" + __chart.title + "</B></FONT></TD>" +
"</TR></TABLE></TD>"
);
}
function doHlabel(__chart,__win) {
__win.document.write(
"<TD></TD><TD></TD><TD Align=center>" +
"<TABLE Width=" + __chart.hsize + " Border=" + border +
" Cellpadding=3 Cellspacing=0><TR>" +
"<TD Align=center><B>" + __chart.hlabel + "</B></TD>" +
"</TR></TABLE></TD>"
);
}
function doVlabel(__chart,__win) {
__win.document.write(
"<TD Valign=middle>" +
"<TABLE Height=" + __chart.vsize + " Border=" + border +
" Cellpadding=3 Cellspacing=0><TR>" +
"<TD Valign=middle><B>" + __chart.vlabel + "</B></TD>" +
"</TR></TABLE></TD>"
);
}
function doTopL(__chart,__win) {
__win.document.write("<TD>");
if(__chart.orientation == "vertical") {
__win.document.write(
"<TABLE Border=" + border + " Cellpadding=0 Cellspacing=0>"
);
for(var i=__chart.scale.length-1; i>=0; i--) {
var scaledeltas = doRound((__chart.scalesize-1)/2,0,'after');
__win.document.write(
"<TR><TD Height=" + __chart.scalesize + " Align=right>" +
"<FONT Size=2>" + __chart.scale[i] + "</FONT></TD>" +
"<TD Height=" + __chart.scalesize + ">" +
"<TABLE Border=" + border + " Cellpadding=0 Cellspacing=0>" +
"<TR><TD Height=" + scaledeltas + " Align=right><IMG Src=" +
__chart.baseimg + " Height=" + scaledeltas + " Width=1></TD></TR>" +
"<TR><TD Height=1><IMG Src=" + __chart.tickimg + " Height=1></TD>" +
"</TR><TR><TD Height=" + scaledeltas + " Align=right><IMG Src=" +
__chart.baseimg + " Height=" + (scaledeltas+1) + " Width=1></TD></TR>" +
"</TABLE>" +
"</TD></TR>"
);
}
__win.document.write("</TABLE>");
} else {
__win.document.write(
"<TABLE Border=" + border + " Cellpadding=0 Cellspacing=0>"
);
for(var i=0; i<__chart.data.length; i++) {
if(i==0) {
__win.document.write(
"<TR><TD Align=right Height=" + __chart.labelsize + "><FONT Size=2>" +
__chart.data[i].label + "</FONT></TD>" +
"<TD Rowspan=" + (__chart.data.length) + "><IMG Src=" +
__chart.baseimg + " Width=1 Height=" + __chart.vsize + "></TD></TR>"
);
} else {
__win.document.write(
"<TR><TD Align=right Height=" + __chart.labelsize + "><FONT Size=2>" +
__chart.data[i].label + "</FONT></TD></TR>"
);
}
}
__win.document.write("</TABLE>");
}
__win.document.write("</TD>");
}
function doTopR(__chart,__win) {
// if(height>(__chart.scalesize*__chart.scale.length)) height=1;
var lessthanzero = 0;
var morethanzero = 0;
for(i=0; i<__chart.scale.length; i++) {
if(__chart.scale[i] < 0)
lessthanzero++;
else if(__chart.scale[i] > 0)
morethanzero++;
}
lessthanzero = doRound(lessthanzero * __chart.scalesize,0,'after');
if(lessthanzero) lessthanzero += doRound(__chart.scalesize/2,0,'after');
lessthanzero += 1;
morethanzero = doRound(morethanzero * __chart.scalesize,0,'after');
if(morethanzero) morethanzero += doRound(__chart.scalesize/2,0,'after');
morethanzero -= 1;
if(__chart.orientation == "vertical") {
__win.document.write("<TD Height=" + __chart.vsize + ">");
__win.document.write(
"<TABLE Height=" + __chart.vsize +
" Border=" + border + " Cellpadding=0 Cellspacing=0>"
);
__win.document.write("<TR>");
for(var i=0; i<__chart.data.length; i++) {
var height =
doRound((Math.abs(__chart.data[i].num)*__chart.scalesize)/
__chart.scaledelta,0,'after');
if(height<1) height = 1;
if(__chart.data[i].num<0) {
__win.document.write(
"<TD Height=" + __chart.vsize + " Valign=bottom Align=center Width=" +
__chart.labelsize + ">"
);
__win.document.write(
"<TABLE Height=" + lessthanzero + " Border=" + border +
" Cellpadding=0 Cellspacing=0><TR>"
);
if(__chart.data[i].url != "-1") {
__win.document.write(
"<TD Valign=top Height=" + lessthanzero + "><A Href=" +
__chart.data[i].url + "><IMG Border=0 " +
"Src=" + __chart.data[i].img + " Height=" + height + " Width=" +
__chart.barsize + "></A></TD>"
);
} else {
__win.document.write(
"<TD Valign=top Height=" + lessthanzero + "><IMG Border=0 " +
"Src=" + __chart.data[i].img + " Height=" + height + " Width=" +
__chart.barsize + "></TD>"
);
}
__win.document.write("</TR></TABLE>");
__win.document.write("</TD>");
} else {
__win.document.write(
"<TD Height=" + __chart.vsize + " Valign=top Align=center Width=" +
__chart.labelsize + ">"
);
__win.document.write(
"<TABLE Height=" + morethanzero + " Border=" + border +
" Cellpadding=0 Cellspacing=0><TR>"
);
if(__chart.data[i].url != "-1") {
__win.document.write(
"<TD Valign=bottom Height=" + morethanzero + "><A Href=" +
__chart.data[i].url + "><IMG Border=0 " +
"Src=" + __chart.data[i].img + " Height=" + height + " Width=" +
__chart.barsize + "></A></TD>"
);
} else {
__win.document.write(
"<TD Valign=bottom Height=" + morethanzero + "><IMG Border=0 " +
"Src=" + __chart.data[i].img + " Height=" + height + " Width=" +
__chart.barsize + "></TD>"
);
}
__win.document.write("</TR></TABLE>");
__win.document.write("</TD>");
}
}
__win.document.write("</TR>");
__win.document.write("</TABLE>");
} else {
__win.document.write("<TD Width=" + __chart.hsize + ">");
__win.document.write(
"<TABLE Width=" + __chart.hsize +
" Border=" + border + " Cellpadding=0 Cellspacing=0>"
);
for(var i=0; i<__chart.data.length; i++) {
var width =
doRound((Math.abs(__chart.data[i].num)*__chart.scalesize)/
__chart.scaledelta,0,'after');
if(width<1.0) width = 1;
if(__chart.data[i].num<0) {
__win.document.write(
"<TR><TD Width=" + __chart.hsize + " Valign=center Align=left" +
" Height=" + __chart.labelsize + ">"
);
__win.document.write(
"<TABLE Width=" + lessthanzero + " Border=" + border +
" Cellpadding=0 Cellspacing=0><TR>"
);
if(__chart.data[i].url != "-1") {
__win.document.write(
"<TD Align=right Width=" + lessthanzero + "><A Href=" +
__chart.data[i].url + "><IMG Border=0 " +
"Src=" + __chart.data[i].img + " Width=" + width + " Height=" +
__chart.barsize + "></A></TD>"
);
} else {
__win.document.write(
"<TD Align=right Width=" + lessthanzero + "><IMG Border=0 " +
"Src=" + __chart.data[i].img + " Width=" + width + " Height=" +
__chart.barsize + "></TD>"
);
}
__win.document.write("</TR></TABLE>");
__win.document.write("</TD></TR>");
} else {
__win.document.write(
"<TR><TD Width=" + __chart.hsize + " Valign=center Align=right" +
" Height=" + __chart.labelsize + ">"
);
__win.document.write(
"<TABLE Width=" + morethanzero + " Border=" + border +
" Cellpadding=0 Cellspacing=0><TR>"
);
if(__chart.data[i].url != "-1") {
__win.document.write(
"<TD Align=left Width=" + morethanzero + "><A Href=" +
__chart.data[i].url + "><IMG Border=0 " +
"Src=" + __chart.data[i].img + " Width=" + width + " Height=" +
__chart.barsize + "></A></TD>"
);
} else {
__win.document.write(
"<TD Align=left Width=" + morethanzero + "><IMG Border=0 " +
"Src=" + __chart.data[i].img + " Width=" + width + " Height=" +
__chart.barsize + "></TD>"
);
}
__win.document.write("</TR></TABLE>");
__win.document.write("</TD></TR>");
}
}
__win.document.write("</TABLE>");
}
__win.document.write("</TD>");
}
function doBotL(__chart,__win) {
__win.document.write("<TD Valign=top Align=right>");
__win.document.write("<IMG Src=" + __chart.baseimg + " Height=1 Width=1>");
__win.document.write("</TD>");
}
function doBotR(__chart,__win) {
__win.document.write("<TD Align=center>");
if(__chart.orientation == "vertical") {
__win.document.write(
"<TABLE Width=" + __chart.hsize + " Border=" + border +
" Cellpadding=0 Cellspacing=0>"
);
__win.document.write("<TR>");
__win.document.write(
"<TD><IMG Src=" + __chart.baseimg +
" Height=1 Width=" + __chart.hsize + "></TD>"
);
__win.document.write("</TR>");
__win.document.write(
"<TR><TD><TABLE Width=" + __chart.hsize + " Border=" + border +
"Cellpadding=0 Cellspacing=0><TR>"
);
for(var i=0; i<__chart.data.length; i++) {
__win.document.write(
"<TD Valign=top Align=center Width=" + __chart.labelsize + "><FONT " +
"Size=2>" + __chart.data[i].label + "</FONT></TD>"
);
}
__win.document.write("</TR></TABLE></TD></TR>");
__win.document.write("</TABLE>");
} else {
__win.document.write(
"<TABLE Border=" + border + " Cellpadding=0 Cellspacing=0>"
);
__win.document.write("<TR>");
for(var i=0; i<__chart.scale.length; i++) {
var scaledeltas = doRound((__chart.scalesize-1)/2,0,'after');
__win.document.write(
"<TD Valign=top Align=center Width=" + __chart.scalesize + ">" +
"<TABLE Border=" + border + " Cellpadding=0 Cellspacing=0><TR>" +
"<TD Width=" + scaledeltas + " Valign=top><IMG Src=" + __chart.baseimg +
" Width=" + scaledeltas + " Height=1></TD>" +
"<TD Width=1><IMG Src=" + __chart.tickimg + " Width=1></TD>" +
"<TD Witdh=" + scaledeltas + " Valign=top><IMG Src=" + __chart.baseimg +
" Width=" + scaledeltas + " Height=1></TD></TR></TABLE>" +
"</TD>"
);
}
__win.document.write("</TR>");
__win.document.write("<TR>");
for(var i=0; i<__chart.scale.length; i++) {
__win.document.write(
"<TD Valign=top Align=center Width=" + __chart.scalesize + "><FONT " +
"Size=2>" + __chart.scale[i] + "</FONT></TD>"
);
}
__win.document.write("</TR>");
__win.document.write("</TABLE>");
}
__win.document.write("</TD>");
}
function doFooter(__chart,__win) {
}
// -->
</SCRIPT>