/* * Main.java * * Created on March 27, 2007, 5:53 PM */ package spldata; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.Ellipse2D; import java.io.File; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.CharBuffer; import java.nio.channels.FileChannel; import java.util.Arrays; import java.util.Comparator; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JPanel { int numTop = 10; public Category[] cs = new Category[numTop]; //tracking top 20 categories int biggest = 0; public Category[] fillCategories() { Category[] cats = new Category[numTop]; int i = 0; //cats[i++] = new Category(800, "782", "Vocal Music"); //cats[i++] = new Category(759, "741", "Drawing & Drawings"); //cats[i++] = new Category(807, "789", "Instrumental Music"); //cats[i++] = new Category(799, "781", "Musical Forms"); //cats[i++] = new Category(659, "641", "Food & Drink"); //cats[i++] = new Category(802, "784", "Instrumental Ensembles"); cats[i++] = new Category(631, "613", "Promotion of Health"); cats[i++] = new Category(416, "398", "Folklore"); cats[i++] = new Category(935, "917", "North America"); cats[i++] = new Category(814, "796", "Athletics"); cats[i++] = new Category(809, "791", "Public Performances"); cats[i++] = new Category(991, "973", "US History"); cats[i++] = new Category(764, "746", "Textile Arts"); cats[i++] = new Category(932, "914", "Europe"); cats[i++] = new Category(634, "616", "Diseases"); cats[i++] = new Category(803, "785", "Chamber Music"); return cats; } public void setCategoryDeweyData(Category c) { switch(c.index) { case 807: c.deweyNum = "789"; c.deweyTitle = "Instrumental Music"; break; case 799: c.deweyNum = "781"; c.deweyTitle = "Musical Forms"; break; case 659: c.deweyNum = "641"; c.deweyTitle = "Food & Drink"; break; case 802: c.deweyNum = "784"; c.deweyTitle = "Instrumental Ensembles"; break; case 631: c.deweyNum = "613"; c.deweyTitle = "Promotion of Health"; break; case 416: c.deweyNum = "398"; c.deweyTitle = "Folklore"; break; case 935: c.deweyNum = "917"; c.deweyTitle = "North America"; break; case 814: c.deweyNum = "796"; c.deweyTitle = "Athletics"; break; case 991: c.deweyNum = "973"; c.deweyTitle = "US History"; break; case 932: c.deweyNum = "914"; c.deweyTitle = "Europe"; break; case 809: c.deweyNum = "791"; c.deweyTitle = "Public Performances"; break; default: break; } return; } public boolean useCateogryIndex(int idx) { if (idx == 800 || idx == 759 || idx == 807 || idx == 799 || idx == 659 || idx == 802 || idx == 631 || idx == 416 || idx == 935 || idx == 814 || idx == 809 || idx == 991 || idx == 764 || idx == 932 || idx == 634 || idx == 803 ) { return true; } return false; } public Category[] gatherWeekData(CharBuffer ibuf, Category[] cats) { for (int hour = 0; hour < 168; hour++) { System.out.println("\thour " + hour); for (int cell = 0; cell < 1017; cell++) { char b = ibuf.get((hour * 3000) + cell + 4); { //System.out.println("b("+cell+") = " + (int)b); if (cats[cell] == null) { cats[cell] = new Category(cell); } if (useCateogryIndex(cell)) { cats[cell].count += b; } } } /* for (int cell = 0; cell < 1017; cell++) { System.out.println("cell["+cell+"].index("+cats[cell].index+" = " + cats[cell].count); } */ } return cats; } public Category[] gatherYearData(CharBuffer ibuf, Category[] cats) { for (int day = 0; day < 365; day++) { System.out.println("\tday " + day); for (int cell = 0; cell < 1017; cell++) { char b = ibuf.get((day * 3000) + cell + 4); { //System.out.println("b("+cell+") = " + (int)b); if (cats[cell] == null) { cats[cell] = new Category(cell); } if (cell > 17) { cats[cell].count += b; } } } /* for (int cell = 0; cell < 1017; cell++) { System.out.println("cell["+cell+"].index("+cats[cell].index+" = " + cats[cell].count); } */ } return cats; } public Category[] averageData(Category[] cats, int divisor) { for (int cell = 0; cell < cats.length; cell++) { System.out.println("\tcell["+cell+"].index("+cats[cell].deweyTitle+") = " + cats[cell].count); for (int hour = 0; hour < 24; hour++) { cats[cell].hours[hour] /= (17 * 7); if (cats[cell].hours[hour] > biggest) { biggest = cats[cell].hours[hour]; } System.out.println("hour " + hour + ": " + cats[cell].hours[hour]); } } return cats; } public void sortCategoriesByRankForHour(Category[] cats, final int hour) { Arrays.sort(cats, new Comparator() { public int compare(Object a, Object b) { Category c1 = (Category)a; Category c2 = (Category)b; // System.out.println("c1 (index = " + c1.index + " count = " // + c1.count + ")> vs vs 9) //9am { g2.drawLine(prevx, prevy, x, y); } prevx = x; prevy = y; } } /* useRank! */ /* for (int ii = 0; ii < cs.length; ii++) { System.out.println("ii = " + ii); int y; if (useRank) { int rank = cs[ii].ranksByHour[8]; int pix = (rank) * dataSliceHeight; //double percent = (double)cs[ii].hours[i] / (double)biggest; //int pix = (int) (percent * dataHeight); y = topInset + pix; } else { y = topInset + (dataSliceHeight * ii); } System.out.println(" BBB ii = " + ii); System.out.println("paint 1 : " + cs[ii].deweyTitle + " rank for 8am : " + cs[ii]); g2.setColor(getColorFromNum(ii)); //g2.fillRect(0, y - 6, leftInset - 30, dataSliceHeight); g2.fillRect(0, y - circlesize, leftInset - 30, circlesize * 2 ); //g2.drawRect(0, y - 6, leftInset - 30, dataSliceHeight); //g2.fillRect(getWidth() - 25, y - circlesize, getWidth() - 5, circlesize * 2 ); g2.setColor(Color.BLACK); System.out.println(" CCC ii = " + ii); g2.drawString(cs[ii].deweyNum + ": " + cs[ii].deweyTitle, 5, y + circlesize - 4); if (useRank) { int spix = (ii) * dataSliceHeight; y = topInset + spix; g2.drawString("" + (ii + 1), getWidth() - 25, y + circlesize - 4); } } */ if (!useRank) { for (int ii = 0; ii < cs.length; ii++) { g2.setColor(getColorFromNum(ii)); int x = 10; int y = topInset + 20 + (20 * ii); Ellipse2D.Double e = new Ellipse2D.Double(x,y,circlesize*2,circlesize*2); g2.fill(e); g2.setColor(Color.BLACK); g2.drawString(cs[ii].deweyNum + ": " + cs[ii].deweyTitle, 30, y + circlesize + 5); } g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); g2.setColor(Color.BLACK); int inc = 100; for (int i = 0; i < biggest + 100; i+=100) { int pix = (int) (((double)dataHeight / (double)biggest ) * (double)i); g2.drawString("" + i, getWidth() - 45, topInset + dataHeight - pix + 5); } } for (int i = 1; i <= 12; i++) { int x = leftInset + (i * dataSliceWidth) - 8; g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); //String month = getMonthStringFromNum(i); String hour = getHourStringFromNum(i); g2.setColor(Color.BLACK); g2.drawString(hour, x - 3, getHeight() - bottomInset + 25); } g2.setFont(new Font("Arial", Font.BOLD, 24)); if (useRank) { g2.drawString("Average Rank of Category by Hour: January 2007 through April 2007", 30,45); } else { //g2.drawString("Average Num Items Checked-out per Hour: January 2007 through April 2007", 30,45); g2.drawString("Total Num Items Checked-out per Hour: January 2007 through April 2007", 30,45); } System.out.println("biggest = " + biggest); } public static void main(String[] args) { JFrame jf = new JFrame(); jf.setSize(new Dimension(400,300)); jf.setLocation(0, 0); // jf.setVisible(true); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("exit..."); System.exit(0); } }); jf.addComponentListener(new ComponentListener() { public void componentHidden(ComponentEvent e) {} public void componentMoved(ComponentEvent e) { //Main.makeBox = true; //RectangleFloat.makeRectangles = true; } public void componentResized(ComponentEvent e) { //Main.makeBox = true; //RectangleFloat.makeRectangles = true; } public void componentShown(ComponentEvent e) { } }); JPanel mainPanel = new Main("data/week-0102.dat", "week"); jf.add(mainPanel); jf.setVisible(true); } } class Category { public int rank = 0; public int count = 0; public int index = 0; public int[] ranksByHour = new int[24]; public int[] hours = new int[24]; public int[] months = new int[12]; public String deweyNum = "na"; public String deweyTitle = "na"; public Category(int index) { this.index = index; } public Category(int index, String deweyNum, String deweyTitle) { this.index = index; this.deweyNum = deweyNum; this.deweyTitle = deweyTitle; } }