/** * Web Site Hits and TLI Sales Count Graph Generator * @author Robert John Morton * @version 27 April 2000 */ import java.awt.*; // for graphics operations (GUI) import javax.swing.*; public class tlisales extends JPanel implements Runnable { private int X = 42, // horizontal bias from left edge of applet to start of x-axis Y = 150, // vertical bias from top edge of applet to start of y-axis n = 33, // off-set from start mark of year to start of its name eg 1999 w, // weeks counter used on horizontal axis of graph weeks[] = {52,52,52,52,52,52,52}; private boolean FTT = true; // run() first-time-through flag private Thread T; // reference for a separate run() thread private Color bg1 = new Color(210,210,210), // main background colour bg2 = new Color(210,195,195), // graph background colour bg3 = new Color(180,180,180); // graticule colour private String year[] = {"1998","1999","2000","2001","2002","2003","2004"}, hits[] = {"0","20","40","60","80","100","110"}, //hits scale cb; // code base URL - where this applet's class file came from private loader ld; // instance of the data loader tlisales(int ls, String cb) { bg1 = getBackground(); setBackground(bg1); // set the applet's background colour ld = new loader(ls,cb); T = new Thread(this); // create a runt() thread T.start(); } public void paint(Graphics g) { g.setColor(bg1); g.fillRect(0,0,450,180); // clear the graph panel g.setColor(bg2); g.fillRect(X,Y-120,364,120); // clear the graph area g.setColor(Color.black); w = 0; for(int i = 0; i < weeks.length; i++) { // for each year shown g.drawString(year[i],X+13+w,Y+20); // year number g.drawLine(X+w,Y+5,X+w,Y+10); // year boundary mark g.setColor(bg3); g.drawLine(X+w,Y,X+w,Y-120); // annotation mark w += weeks[i]; // accumulated number of weeks g.setColor(Color.black); } g.setColor(bg3); g.drawLine(X+w,Y,X+w,Y-120); // annotation mark g.setColor(Color.black); g.drawLine(X+w,Y+5,X+w,Y+10); // final year boundary mark g.drawLine(X,Y+5,X+w,Y+5); // horizontal axis int z = 0; for(int i = 0; i < hits.length; i++) { // for each 10-hit graduation rightString(g,hits[i],X-10,Y+5-z); // annotation g.drawLine(X-10,Y-z,X-5,Y-z); // annotation mark g.setColor(bg3); g.drawLine(X,Y-z,X+w,Y-z); // annotation mark z += 20; // accumulated number of hits g.setColor(Color.black); } g.drawLine(X-5,Y,X-5,Y-120); // vertical axis g.drawString("Hits per week:",X-n,Y-130); g.drawString("Year:",X-n,Y+20); if(!ld.dataLoaded()) return; g.setColor(Color.blue); // set trace colour to blue int h = 0; // number of hits for a given week w = X; // initial x-bias for week number int W = 0, // number of weeks for which there is valid data H = 0; // hits total accumulator for(int i = 0; i < ld.L; i++) { // avoid displaying zero values before start date if((h = (int)ld.B[i]) > 0) { // display it as a vertical bar on chart g.drawLine(w,Y,w,Y-h); H += h; // add this week's hits to the total W++; // increment number of weeks for which there is valid data } w++; // increment the week number } g.setColor(Color.black); g.drawString("Total to Date "+H,105,20); if(W > 0) g.drawString("Weekly Average "+(H/W),225,20); } // MANAGE INTERNET DATA TRANSFERS ON A SEPARATE THREAD public void run() { while(T != null) { // while this thread exists if(ld.dataLoaded() && FTT) { // if hits data all loaded repaint(); // display the graph FTT = false; } try{Thread.sleep(250);} // sleep then continue loading catch(InterruptedException e){} } } // DRAW A RIGHT-JUSTIFIED STRING void rightString(Graphics g, String s, int x, int y) { // dimensions of characters for current font FontMetrics fm = g.getFontMetrics(); g.drawString(s,x-fm.stringWidth(s),y); // display the string } }