/* * FINANCIALS - Financial graphing applet * February 2005, Harley H. Puthuff * Copyright ©2003-5, Loan Advocate.com */ import java.awt.*; import java.awt.event.*; import java.applet.*; import harley.*; /* For Applet viewers: */ public class financials extends Applet implements ActionListener, ItemListener, MouseListener { // background color values: private final Rgb background = new Rgb("#FFFFBB"); private final Rgb text = new Rgb("#000000"); private final Rgb border = new Rgb("#0000B0"); private final Rgb plotBackground = new Rgb("#FFFFFF"); private final Rgb gridLines = new Rgb("#F0F0F0"); private final Rgb unpressed = new Rgb("#DCD9C8"); private final Rgb hilite = new Rgb(Rgb.WHITE); private final Rgb label = new Rgb("#ECE9D8"); // plotting colors: private final Rgb plot1 = new Rgb(Rgb.RED); private final Rgb plot2 = new Rgb(Rgb.GREEN); private final Rgb plot3 = new Rgb(Rgb.BLUE); private final Rgb plot4 = new Rgb(Rgb.OLIVE); private final Rgb plot5 = new Rgb(Rgb.PURPLE); private final Rgb plot6 = new Rgb(Rgb.NAVY); // plotting values: public Plot plots[] = { new Plot("Prime",Prime.description,plot1.toColor(),Prime.data,Prime.month,Prime.year), new Plot("COFI",COFI.description,plot2.toColor(),COFI.data,COFI.month,COFI.year,1.0,5.0,0.05), new Plot("LIBOR 6mo",LIBOR.description,plot3.toColor(),LIBOR.data,LIBOR.month,LIBOR.year), new Plot("T-Bill 1yr",Tbill.description,plot4.toColor(),Tbill.data,Tbill.month,Tbill.year), new Plot("12 MTA",MTA.description,plot6.toColor(),MTA.data,MTA.month,MTA.year), // new Plot("30yr Bond",Bond.description,plot5.toColor(),Bond.data,Bond.month,Bond.year), }; // display objects: private String version = new String("Version: 2-2005"); private String copyright = new String("©2003-5 LoanAdvocate.com"); private String showData = new String("Show Data"); private String showGraph = new String("Show Graph"); private String dashes = new String(" ---------- "); private Font basicFont = new Font("Dialog",Font.BOLD,12); private Font marginFont = new Font("Dialog",Font.PLAIN,12); private Font dataFont = new Font("Monospaced",Font.PLAIN,12); private Label margins = new Label("Margin:",Label.RIGHT); private Label averages = new Label("Average:",Label.RIGHT); private TextField password = new TextField(); private Button mode = new Button(showData); private TextArea dataList = new TextArea(); private Choice years = new Choice(); public TextArea comments = new TextArea("",1,1,TextArea.SCROLLBARS_NONE); private Mouser mouse; // working variables, etc: private int xIntervals; // number of x-axis plot points private Dimension appWindow; // applet window size private Area workArea; // main work area specs. private Area legendArea; // place to show graph legend public Area plotArea; // place to actually plot points private int xIwidth; // width of each x-axis interval private double yLowValue; // low index value private double yHighValue; // high index value private double ySpanValue; // high - low private int firstElement; // first element in table(s) to show private int firstYear; // what year that element represents private int firstMonth; // what month that element represents // initialize our applet instance: public void init() { super.init(); this.setBackground(background.toColor()); this.setForeground(text.toColor()); setFont(basicFont); setLayout(null); buildYearsChoice(); buildComponents(); getNewPlots(); // 2004--> plotting data getFirstPlot(); // determine first one to show mouse = new Mouser(this); // add mouse monitor } // build a choice list for years to be shown (default=10 yrs): private void buildYearsChoice() { int i; years.add("1 year"); for (i=2; i < 31; i++) years.add("" + i + " years"); years.select(9); // 10 years xIntervals = 120; // 10 years } // setup, position & size components: private void buildComponents() { int xs=60,xb=76,xg=4; // x values for button array int ys=4,yb=18,yg=0; // y values for button array int i,x,y; Plot p; x = xs - 56; y = ys + yb + yg; add(margins); margins.setBounds(x,y,56,yb); margins.setFont(marginFont); margins.setForeground(border.toColor()); add(averages); averages.setBounds(x,(y+yb+yg+2),56,yb); averages.setFont(marginFont); averages.setForeground(border.toColor()); for (i=0; i < plots.length; i++) { p = plots[i]; x = xs + (i * (xb + xg)); add(p.button); p.button.setBounds(x,ys,xb,yb); p.button.setBackground(unpressed.toColor()); p.button.addActionListener(this); p.button.addMouseListener(this); add(p.combo); p.combo.setBounds(x,(ys+yb+yg+2),xb,yb); p.combo.setFont(marginFont); p.combo.addItemListener(this); add(p.text); p.text.setBounds(x,(ys+(2*(yb+yg+2))),(xb-1),yb); p.text.setBackground(label.toColor()); p.text.setFont(marginFont); p.text.setAlignment(Label.RIGHT); } add(password); add(mode); add(years); add(dataList); add(comments); x = xs + (5 * (xb + xg)) + (4 * xg); y = ys; password.setBounds(x,y,xb,yb); password.setFont(marginFont); mode.setBounds(x,(ys+yb+yg+2),xb,yb); mode.addActionListener(this); years.setBounds(x,(ys+yb+yg+2+yb+yg+2),xb,yb); years.addItemListener(this); dataList.setBounds(3,66,570,330); dataList.setFont(dataFont); dataList.setEditable(true); dataList.setVisible(false); comments.setBounds(100,80,400,200); comments.setFont(marginFont); comments.setEditable(false); comments.setVisible(false); } // obtain new plotting data from applet parameters: private void getNewPlots() { for (int i=0; i < plots.length; i++) { Plot p = plots[i]; String parameter = getParameter(p.name); if (parameter != null) p.extend(parameter); } } // obtain first plot to show: private void getFirstPlot() { String parameter = getParameter("show"); if (parameter == null) return; for (int i=0; i < plots.length; i++) { Plot p = plots[i]; if (p.name.equalsIgnoreCase(parameter)) { p.visible = true; break; } } } // handle mouse events: public void mouseClicked(MouseEvent me) {} // mouse clicked public void mouseEntered(MouseEvent me) {} // mouse entered public void mouseExited(MouseEvent me) {} // mouse exited public void mousePressed(MouseEvent me) {} // mouse pressed public void mouseReleased(MouseEvent me) {} // mouse released /* public void mouseEntered(MouseEvent me) { // mouse entered a component boolean visible = comments.isVisible(); int ix,x=me.getX(),y=me.getY(); for (ix=0; ix < plots.length; ix++) if (me.getSource() == plots[ix].button) { comments.setText(plots[ix].description); comments.setVisible(true); return; } } public void mouseExited(MouseEvent me) { // mouse exited a component comments.setVisible(false); } */ // handle button-click events: public void actionPerformed(ActionEvent event) { String label = event.getActionCommand(); if (label.equals(showData) || label.equals(showGraph)) { String pass = password.getText(); if (! pass.equalsIgnoreCase("gates")) return; mode.setLabel(label.equals(showData) ? showGraph : showData); } else for (int i=0; i < plots.length; i++) { Plot p = plots[i]; if (p.name.equals(label)) p.visible = (! p.visible); } repaint(); } // handle select-menu changes: public void itemStateChanged(ItemEvent event) { repaint(); } // paint the applet window: public void paint(Graphics g) { mouse.off(); calculateScales(); adjustComponents(); showBorder(g); showCopyright(g); if (mode.getLabel().equals(showGraph)) paintData(g); else paintGraph(g); super.paint(g); showStatus("Click buttons to Add/Remove Indices"); if (mode.getLabel().equals(showData)) mouse.on(); } private void adjustComponents() { // set colors for unpressed/depressed for (int i=0; i < plots.length; i++) { Plot p = plots[i]; if (p.visible) { p.button.setBackground(p.color); p.button.setForeground(hilite.toColor()); p.text.setText("" + p.average); } else { p.button.setBackground(unpressed.toColor()); p.button.setForeground(text.toColor()); p.combo.select(0); p.text.setText(""); } } dataList.setVisible(false); comments.setVisible(false); } private void calculateScales() { // calculate scales,areas,etc.: int x,y,pcount; Plot p; appWindow = getSize(); workArea = new Area(3,44,appWindow.width-4,appWindow.height-14); plotArea = new Area(workArea.beginX+57,workArea.beginY+22, workArea.endX-8,workArea.endY-20); // calculate the scale of the x-axis xIntervals = (years.getSelectedIndex() + 1) * 12; xIwidth = plotArea.width / xIntervals; firstElement = plots[0].values.length - xIntervals; x = (plots[0].year * 12) + (plots[0].month -1 ) + firstElement; firstYear = (x / 12); firstMonth = (x % 12) + 1; // calculate the scale of the y-axis for (yLowValue=0.0,yHighValue=0.0,pcount=0,x=0; x < plots.length; x++) { p = plots[x]; if (!p.visible) continue; p.analyze(firstElement); if ((yLowValue == 0.0) || (p.low < yLowValue)) yLowValue = p.low; if ((yHighValue == 0.0) || (p.high > yHighValue)) yHighValue = p.high; pcount++; } if (pcount == 0) {yLowValue = 0.0; yHighValue = 10.0;} yLowValue = Math.floor(yLowValue); yHighValue = Math.ceil(yHighValue); ySpanValue = yHighValue - yLowValue; // recalculate plotting area to accomodate even intervals plotArea.width = xIwidth * xIntervals + 1; plotArea.endX = plotArea.beginX + plotArea.width - 1; } private void showBorder(Graphics g) { // display border Color old = g.getColor(); g.setColor(border.toColor()); g.drawRect(0,0,appWindow.width-1,appWindow.height-1); g.setColor(old); } private void showCopyright(Graphics g) { // display copyright message FontMetrics fm; int width,depth; Font oldFont = g.getFont(); Color oldColor = g.getColor(); g.setFont(new Font("SansSerif",Font.PLAIN,10)); g.setColor(border.toColor()); fm = g.getFontMetrics(); width = fm.stringWidth(copyright); depth = fm.getDescent(); g.drawString(version,1,appWindow.height-depth); g.drawString(copyright,appWindow.width-width-1,appWindow.height-depth); g.setFont(oldFont); g.setColor(oldColor); } private void paintData(Graphics g) { // display data list int i,j,count,first,cursor; String abuffer,bbuffer; dataList.setText(""); abuffer = new String(Helper.align("Date")); bbuffer = new String(dashes); for (count=0,first=-1,i=0; i < plots.length; i++) if (plots[i].visible) { abuffer += Helper.align(plots[i].name); bbuffer += dashes; if (first == -1) first = i; ++count; } abuffer += ("\n" + bbuffer + "\n"); if (count > 0) for (cursor=firstElement; cursor < plots[first].values.length; cursor++) { bbuffer = Helper.align(plots[first].date(cursor)); for (i=first; i < plots.length; i++) if (plots[i].visible) bbuffer += Helper.align(plots[i].at(cursor)); abuffer += (bbuffer + "\n"); } dataList.setText(abuffer); dataList.setCaretPosition(0); dataList.setVisible(true); } private void paintGraph(Graphics g) { // display graphic panel showPlottingArea(g); for (int i=0; i < plots.length; i++) if (plots[i].visible) showPlot(g,plots[i]); } private void showPlottingArea(Graphics g) { // display plotting area FontMetrics fm; Color holdColor; int i,j,x,y,z,mo,yr; double yvalue,xd,yd,zd; String text; Font saveFont = g.getFont(); Color saveColor = g.getColor(); g.setFont(new Font("SansSerif",Font.PLAIN,10)); fm = g.getFontMetrics(); g.setColor(plotBackground.toColor()); g.fillRect(plotArea.beginX,plotArea.beginY,plotArea.width,plotArea.depth); g.setColor(Color.black); g.drawLine(plotArea.beginX,plotArea.beginY,plotArea.beginX,plotArea.endY); g.drawLine(plotArea.beginX,plotArea.endY,plotArea.endX,plotArea.endY); for (x=plotArea.beginX,mo=firstMonth,yr=firstYear; x <= plotArea.endX; x+=xIwidth) { y = (mo == 1) ? 6 : 3; if ((mo == 1) || (xIntervals <= 120)) g.drawLine(x,plotArea.endY,x,plotArea.endY+y); if (mo == 1) { text = "" + yr; if (xIntervals > 120) text = text.substring(2,4); y = x - (fm.stringWidth(text) / 2) + 1; g.drawString(text,y,(plotArea.endY+4+fm.getHeight())); if (x > plotArea.beginX) { holdColor = g.getColor(); g.setColor(gridLines.toColor()); g.drawLine(x,plotArea.beginY,x,plotArea.endY-1); g.setColor(holdColor); } } if (++mo > 12) {++yr; mo=1;} } for (yvalue=yLowValue; yvalue <= yHighValue; yvalue+=1.0) { y = (int)(plotArea.endY - Math.round(((yvalue - yLowValue) / ySpanValue) * plotArea.depth)); g.drawLine(plotArea.beginX-4,y,plotArea.beginX,y); text = "" + yvalue; x = fm.stringWidth(text); g.drawString(text,(plotArea.beginX-6-x),(y+fm.getDescent())); if (yvalue > yLowValue) { holdColor = g.getColor(); g.setColor(gridLines.toColor()); g.drawLine(plotArea.beginX+1,y,plotArea.endX,y); g.setColor(holdColor); } } g.setFont(saveFont); g.setColor(saveColor); } private void showPlot(Graphics g,Plot p) { // display one plot Color saveColor; int px,x,y,lastx,lasty; double value,lastValue; saveColor = g.getColor(); g.setColor(p.color); lastx = 0; lasty = 0; lastValue = 0.0; for (px=firstElement; px < p.values.length; px++) { value = p.at(px); x = plotArea.beginX + ((px - firstElement) * xIwidth); y = (int)(plotArea.endY - Math.round(((value - yLowValue) / ySpanValue) * plotArea.depth)); if (((lastx | lasty) > 0) && (lastValue > 0.0) && (value > 0.0)) g.drawLine(lastx,lasty,x,y); lastx = x; lasty = y; lastValue = value; if ((xIntervals <= 120) && (value > 0.0)) mouse.addPlot(x,y,value); } g.setColor(saveColor); } }