import java.net.*; import java.sql.*; import java.io.*; import java.awt.*; import java.text.*; import java.util.*; import java.applet.*; // The Entry class is used to store data output from the Servlet. // This class is used so that all the information can be stored // and sorted in one place rather than splitting between // different vectors. class Entry { double Value; double Pos; double Pos2; double xPos; public Entry() { } public Entry(double Value, double Pos) { this.Value = Value; this.Pos = Pos; } public double getPos() { return Pos; } public double getPos2() { return Pos2; } public double getValue() { return Value; } public double getxPos() { return xPos; } public void setValue(double Value) { this.Value = Value; } public void setPos(double Pos) { this.Pos = Pos; } public void setPos2(double Pos2) { this.Pos2 = Pos2; } public void setxPos(double xPos) { this.xPos = xPos; } } public class graph extends Applet { String Symbol = (String)null; String Type = (String)null; String SysName = (String)null; String Width = (String)null; String Height = (String)null; Vector Days = new Vector(); // Define extreme thresholds for max and min so that they will // be caught during the max/min checks. double max = 0; double Step3 = 0; double Step2 = 0; double Step1 = 0; double min = 99999999; double yrange = 0; int ysteps = 0; double yjump = 0; double scale; double tscale; double xPos; double xJump; double prevVal; int Div; boolean hasData = true; DecimalFormat numFormat = new DecimalFormat("#0.00"); DecimalFormat tFormat = new DecimalFormat("00"); public void init() { Symbol = getParameter("Symbol"); Type = getParameter("Type"); Width = getParameter("Width"); Height = getParameter("Height"); // Get they currently set Exchange name. try { SysName = getSysName(); } catch (Exception e) { e.printStackTrace(); } // Retrieve Exchange data. try { Days = new Vector(); getData(); } catch (Exception e) { e.printStackTrace(); } if (Days.size() == 1) { hasData = false; } // If there is no data or for some reason the range is messed // up, set max / min values to 0 so it lookes better // when displayed. if ((min == 99999999) || (max == 0)) { hasData = false; } if (!hasData) { max = 0.00; min = 0.00; } // If the data hasn't change we have to force some sort of a range. if (max == min && hasData) { max = max + 1; min = min - 1; if (min < 0) { min = 0; } } // We need whole dollar amounts divisible by 5 on the drawing. // Find the closest whole dollar above max and below min then // test it for division by 5. If the min is less than 5, set // the min to 0. // We want to force a difference of 5 so see if max and min are // already whole dollar amounts divisible by 5. If so, change them // by 1. if ((max % 5) == 0) { max++; } if ((min % 5) == 0) { min--; if (min < 0) { min = 0; } } int cVal = 0; Double toInt = new Double(max); cVal = toInt.intValue(); if (cVal < max) { cVal++; } max = cVal; toInt = new Double(min); cVal = toInt.intValue(); if (cVal > min) { cVal--; } min = cVal; while ((max%5) != 0) { max++; } if (min > 5) { while ((min%5) != 0) { min--; } } else { min = 0; } yrange = (max-min) / 5; scale = 93 / (max-min); } public void paint (Graphics g) { String thisDate = (String)null; String thisTime = (String)null; int t = 0; double j = 34; java.util.Date lastDate = new java.util.Date(); java.util.Date lastTime = new java.util.Date(); int hour = lastTime.getHours(); int minute = lastTime.getMinutes(); // Set the format for time and date DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); thisDate = dateFormat.format(lastDate); thisTime = tFormat.format(hour) + ":" + tFormat.format(minute); // Set up the graph colors. Most of these aren't used. Color tan = new Color(250, 235, 215); Color lightgray = new Color(220, 220, 220); Color darkgray = new Color(169, 169, 169); Color ivory = new Color(255, 255, 240); Color smoke = new Color(239, 239, 239); Color slateblue = new Color(106, 90, 205); Color sky = new Color(0, 91, 251); Color darkblue = new Color(0, 0, 139); Color nasBlue = new Color(40, 100, 188); Color nasGray = new Color(102, 102, 102); Color nasLightGray = new Color(187, 187, 187); Color lightblue = new Color(162, 188, 221); Color medgray = new Color(179, 179, 179); Color gold = new Color(255, 215, 0); Color khaki = new Color(240, 230, 140); Color BSEBlue = new Color(0, 0, 204); // Draw outer border g.setColor(smoke); g.fillRect(0, 0, 300, 171); // Draw top bar g.setColor(medgray); g.fillRect(3, 3, 293, 17); // Draw side data container g.setColor(nasGray); g.fillRect(3, 21, 30, 119); // Draw graph container g.setColor(Color.white); g.fillRect(33, 21, 263, 115); // Draw bottom data container g.setColor(nasGray); g.fillRect(3, 116, 293, 25); // Draw identifying container g.setColor(medgray); g.fillRect(3, 142, 293, 26); Font thisFont = new Font("Helvetica", (int)Font.BOLD, 12); g.setFont(thisFont); g.setColor(smoke); // Prints the top data line if (Symbol != null) { if (Symbol.equals("SYS")) { g.drawString("Today", 145, 16); } else { g.drawString(Symbol, 145, 16); } } else { g.drawString("Today", 145, 16); } thisFont = new Font("Helvetica", (int)Font.PLAIN, 10); g.setFont(thisFont); g.drawString(thisDate, 7, 15); g.drawString(thisTime, 265, 15); // Print the Values range String pMax = ""; String pStep4 = ""; String pStep3 = ""; String pStep2 = ""; String pStep1 = ""; String pMin = ""; yjump = (113-33) / 5; pStep4 = pStep4.valueOf(numFormat.format((max - yrange))); pStep3 = pStep3.valueOf(numFormat.format((max - (yrange * 2)))); pStep2 = pStep2.valueOf(numFormat.format((max - (yrange * 3)))); pStep1 = pStep1.valueOf(numFormat.format((max - (yrange * 4)))); pMax = pMax.valueOf(numFormat.format(max)); pMin = pMin.valueOf(numFormat.format(min)); g.drawString(pMax, 4, 33); g.drawString(pStep4, 4, (int)(33 + yjump)); g.drawString(pStep3, 4, (int)(33 + (yjump * 2))); g.drawString(pStep2, 4, (int)(33 + (yjump * 3))); g.drawString(pStep1, 4, (int)(33 + (yjump * 4))); g.drawString(pMin, 4, 113); if (pMax.equals("0.00")) { hasData = false; } // This prints the time scale on the X axis. The values // are set based on how many days are to be displayed. if (Type.equals("1")) { Div = 1; g.drawString("0:00", 34, 132); g.drawString("6:00", 91, 132); g.drawString("12:00", 148, 132); g.drawString("18:00", 205, 132); g.drawString("24:00", 263, 132); } else if (Type.equals("2")) { Div = 7; g.drawString("Sunday", 34, 132); g.drawString("Wednesday", 130, 132); g.drawString("Saturday", 240, 132); } else if (Type.equals("3")) { java.util.Date thistDate = new java.util.Date(); int month = 0; int days = 0; month = thistDate.getMonth() + 1; if (month == 1) { days = 31; } else if (month == 2) { if (!testLeapYear()) { days = 28; } else { days = 29; } } else if (month == 3) { days = 31; } else if (month == 4) { days = 30; } else if (month == 5) { days = 31; } else if (month == 6) { days = 30; } else if (month == 7) { days = 31; } else if (month == 8) { days = 31; } else if (month == 9) { days = 30; } else if (month == 10) { days = 31; } else if (month == 11) { days = 30; } else if (month == 12) { days = 31; } Div = days; String datePrint = month + "/1"; g.drawString(datePrint, 34, 132); datePrint = month + "/15"; g.drawString(datePrint, 130, 132); datePrint = month + "/" + days; g.drawString(datePrint, 263, 132); } else if (Type.equals("4")) { if (!testLeapYear()) { Div = 365; } else { Div = 366; } g.drawString("January", 34, 132); g.drawString("December", 240, 132); } // Print the bottom identifier bar. This just displays what // sort of graph is being output. thisFont = new Font("Helvetica", (int)Font.PLAIN, 12); g.setFont(thisFont); g.setColor(smoke); if (Type.equals("1")) { g.drawString("Today on the " + SysName, 26, 160); } else if (Type.equals("2")) { g.drawString("This Week on the " + SysName, 23, 160); } else if (Type.equals("3")) { g.drawString("This Month on the " + SysName, 24, 160); } else if (Type.equals("4")) { g.drawString("This Year on the " + SysName, 24, 160); } // Draw grid lines g.setColor(smoke); for (int i = 33 ; i < 296 ; i=i+12) { g.drawLine(i, 21, i, 115); } for (int i = 115 ; i > 22 ; i=i-10) { g.drawLine(33, i, 296, i); } g.setColor(BSEBlue); // Plot the data if (hasData) { for (int i = 1 ; i < Days.size() ; i++) { // Retrieve the entries. fillPolygon seems to produce a much // fuller line than drawLine so loop through the data, getting // two entries at a time to use on fillPolygon. Entry thisEnt = (Entry)Days.elementAt(i); Entry prevEnt = (Entry)Days.elementAt(i-1); double thisVal = thisEnt.getValue(); double prevVal = prevEnt.getValue(); double thisPos = thisEnt.getxPos(); double prevPos = prevEnt.getxPos(); int xVals[] = {((int)thisPos), ((int)prevPos), ((int)+prevPos), ((int)thisPos)}; // Plotting on the Y axis is done by starting at the bottom point // and subtracting upwards. // fillPolygon leaves gaps at times, go back over with drawLine // just to make sure everything is filled in. // This is cheating but all the numbers are hard-coded so it // will work. If the data is the same through the whole period, // all we want is to display the one value across the timeframe. // Hard-code drawing the line straight down the middle. // int yVals[] = {(int)((115+(min*scale))-(thisVal*scale)), (int)((115+(min*scale))-(prevVal*scale)), (int)((115+(min*scale))-(prevVal*scale)-3), (int)((115+(min*scale))-(thisVal*scale)-3)}; // g.fillPolygon(xVals, yVals, 4); g.drawLine(((int)thisPos), ((int)((115+(min*scale))-((thisVal*scale)))), ((int)prevPos), ((int)((115+(min*scale))-((prevVal*scale))))); } } else { thisFont = new Font("Helvetica", (int)Font.PLAIN, 20); g.setFont(thisFont); g.drawString("No Data", 120, 70); } } public void getData() throws MalformedURLException, IOException { URL newUrl = null; BufferedReader in = null; try { newUrl = new URL("http://www.isip.msstate.edu:8080/isip/servlet/isip.java.bullyse.bullydb.History?Task=getData&Symbol=" + Symbol + "&Type=" + Type); in = new BufferedReader(new InputStreamReader(newUrl.openStream())); } catch (Exception e) { e.printStackTrace(); } String inputline = (String)null; double Value = 0.00; double Divs = 0; String readTime = (String)null; Vector thisDay = new Vector(); Double DoubVal = null; Entry thisEnt = new Entry(); double chkVal = 0; if (Type.equals("1") || Type.equals("4")) { // Type 1 and 4 each have 3 things output while ((inputline = in.readLine()) != null) { thisEnt = new Entry(); DoubVal = DoubVal.valueOf(inputline); thisEnt.setValue(DoubVal.doubleValue()); chkVal = DoubVal.doubleValue(); inputline = in.readLine(); DoubVal = DoubVal.valueOf(inputline); thisEnt.setPos(DoubVal.doubleValue()); inputline = in.readLine(); DoubVal = DoubVal.valueOf(inputline); thisEnt.setPos2(DoubVal.doubleValue()); double xPos = getxPos(thisEnt); thisEnt.setxPos(xPos); // Check to see if the Value is Max or Min. if (chkVal > max) { if ((max < min) && (max > 0)) { min = max; } max = chkVal; } else if (chkVal < min) { min = chkVal; } Days.addElement((Entry)thisEnt); } } else { while ((inputline = in.readLine()) != null) { thisEnt = new Entry(); DoubVal = DoubVal.valueOf(inputline); thisEnt.setValue(DoubVal.doubleValue()); chkVal = DoubVal.doubleValue(); inputline = in.readLine(); DoubVal = DoubVal.valueOf(inputline); thisEnt.setPos(DoubVal.doubleValue()); double xPos = getxPos(thisEnt); thisEnt.setxPos(xPos); // Check to see if the Value is Max or Min. if (chkVal > max) { if ((max < min) && (max > 0)) { min = max; } max = chkVal; } else if (chkVal < min) { min = chkVal; } Days.addElement(thisEnt); } } } public String getSysName() throws IOException { URL newUrl = null; BufferedReader in = null; try { newUrl = new URL("http://www.isip.msstate.edu:8080/isip/servlet/isip.java.bullyse.bullydb.History?Task=getName"); in = new BufferedReader(new InputStreamReader(newUrl.openStream())); } catch (Exception e) { e.printStackTrace(); } return in.readLine(); } // This method checks to see if the current year is a leap year public boolean testLeapYear() { boolean isLeapYear = false; java.util.Date thisYear = new java.util.Date(); int getYear = thisYear.getYear() + 1900; if (((getYear%4) == 0) && (!((getYear%100) == 0) || ((getYear%400) == 0))) { isLeapYear = true; } else { isLeapYear = false; } return isLeapYear; } public double getxPos(Entry thisEnt) { double Divs = 0; // All the data has been read in, now figure the X coordinates for each // data point. // How many divisions are there? if (Type.equals("1")) { Divs = 24; } else if (Type.equals("2")) { Divs = 7; } else if (Type.equals("3")) { java.util.Date thistDate = new java.util.Date(); int month = 0; int days = 0; month = thistDate.getMonth() + 1; if (month == 1) { Divs = 31; } else if (month == 2) { if (!testLeapYear()) { Divs = 28; } else { Divs = 29; } } else if (month == 3) { Divs = 31; } else if (month == 4) { Divs = 30; } else if (month == 5) { Divs = 31; } else if (month == 6) { Divs = 30; } else if (month == 7) { Divs = 31; } else if (month == 8) { Divs = 31; } else if (month == 9) { Divs = 30; } else if (month == 10) { Divs = 31; } else if (month == 11) { Divs = 30; } else if (month == 12) { Divs = 31; } } else if (Type.equals("4")) { if (testLeapYear()) { Divs = 366; } else { Divs = 365; } } // Determine the area each division has double Area = 260 / Divs; // Each Type has a different number of divisions, so there will be a // different way of doing each. if (Type.equals("1")) { double minArea = Area / 60; thisEnt.setxPos((thisEnt.getPos() * Area) + (thisEnt.getPos2() * minArea)+34); } else if (Type.equals("2") || Type.equals("3")) { thisEnt.setxPos((thisEnt.getPos() * Area) + 34); } else if (Type.equals("4")) { double month = 0; double days = 0; double tDays = 0; month = thisEnt.getPos(); days = thisEnt.getPos2(); if (testLeapYear()) { tDays++; } // Convert from day/month to just day_of_year if (month == 1) { tDays += days; } else if (month == 2) { tDays += 31 + days; } else if (month == 3) { tDays += 59 + days; } else if (month == 4) { tDays += 90 + days; } else if (month == 5) { tDays += 120 + days; } else if (month == 6) { tDays += 151 + days; } else if (month == 7) { tDays += 181 + days; } else if (month == 8) { tDays += 212 + days; } else if (month == 9) { tDays += 243 + days; } else if (month == 10) { tDays += 273 + days; } else if (month == 11) { tDays += 304 + days; } else if (month == 12) { tDays += 334 + days; } thisEnt.setxPos((tDays * Area) + 35); } return thisEnt.getxPos(); } }