Create a Project based on P2P/PubSub with OODB and DataMining - Graphics

Joe

Thành viên VIP
21/1/13
3,007
1,335
113
(Continue of HERE)

Create a Project based on P2P/PubSub with OODB and DataMining - Graphics

The art of DataMining is also the art of Design of Data Structure and the Extraction of Information details from the mundane data. The Graphics Presentation is only one of the best techniques to make the data as information comprehensible. The clearer the data are presented, the more understanding the data become. If the Data Structure is too complex it just complicates the extraction of information details. In some case the data are either irretrievable or just partially extractable. As said: Data Mining is the art of an Artist, an excellent developer to be exact, who begins with a Data Structure Design and accomplishes the mundane data with an artistic Data Presentation.

In THIS session I have shown you how Graphics can be created in JAVA. Either as an extension of AWT Canvas or as an extension SWING JComponent, and how they differ from each other. SWING GUI elements are mostly the extensions of JComponent. Why mostly? Well, JDialog bases for example on Dialog, an AWT element. From this viewpoint JComponent is not only the most preferred Graphics base, but also has an undeniable advantage of stability. No flickering. No double buffering. Nothing necessary to enhance, but just ready for use.

If you compile and run the Canvas Graphics mentioned in the link the displaying Graphics works naturally, but it flickers intermittently as if there was some delay or an overlapping of multiple Graphics. However, the Graphics features are common and the same. Except the connecting to GUI events (e.g. KeyListerner, MouseListener, ActionListener, etc.). Because SWING GUI elements (such as JButton, JTextArea, etc.) are the extensions of JComponent the work with SWING GUI Graphics is easier and more comfortable than than with AWT Canvas. I am a man of simplicity. I always hate every complicated unnecessary. And because of simplicity the Graphics used in the Boutique package are all JComponent Graphics.

The Statistics Graphics is derived from JButton.
Java:
// Joe Nartca (C)
public class Statistics extends JButton {
  public Statistics(Parms P, String uID) {
    this.uID = uID;
    width = 810; height = 490;
    setPreferredSize(new Dimension(width, height));
    //
    online = total = 0;
    cash = new HashMap<>();
    salMap = new HashMap<>();
    credit = new HashMap<>();
    salList = new ArrayList<>();
    pMethod = new ArrayList<>();
    cardLst = new ArrayList<>();
    soldItems = new ArrayList<>();
    ArrayList<java.util.List<String>> salItems = new ArrayList<>();
    String[] keys = P.oodb.getKeys(P.dbInvoice);
    for (String key : keys) {
      Invoice I = (Invoice)P.oodb.readLock(P.dbInvoice, key);
      if (uID != null && !I.getOwner().equals(uID)) continue;
      salItems.add(I.getSalesList());
      if (I.isCustomer()) ++online;
      salList.add(I.getSales());
      key = I.getPaying();
      if (cash.containsKey(key)) {
        Integer i = cash.get(key)+1;
        cash.replace(key, i);
      } else {
        cash.put(key, 1);
        pMethod.add(key);
      }
      key = I.getCard().toLowerCase();
      int p =  key.indexOf(",");
      if (p < 0) p = key.length();
      key = key.substring(0, p);
      if (credit.containsKey(key)) {
        Integer i = credit.get(key)+1;
        credit.replace(key, i);
      } else {
        credit.put(key, 1);
        cardLst.add(key);
      }
      ++total;
    }
    max = 0; // sold items
    for (int l = 0, x = salItems.size(); l < x; ++l) {
      java.util.List<String> L = salItems.get(l);
      java.util.List<Integer> I = salList.get(l);
      for (int i = 0, mx = L.size(); i < mx; ++i) {
        Integer X = I.get(i);
        String S = L.get(i);
        if (salMap.containsKey(S)) {
          Integer Y = salMap.get(S)+X;
          salMap.replace(S, Y);
        } else {
          salMap.put(S, X);
          soldItems.add(S);
        }
        max += X;
      }
    }
  }
  public void draw( ) {
    if (soldItems.size() == 0) return;
    pBuf = createImage(width, height);
    Graphics2D g = (Graphics2D) pBuf.getGraphics();
    // drawing NOW....
    g.setColor(Color.black);
    g.fillRect(0, 0, width, height);
    g.setColor(Color.white);
    g.setFont(new Font("Arial", Font.BOLD,15));
    String X = uID == null?"":" for "+uID;
    g.drawString("STATISTICS"+X, 310-X.length(), 15);
    g.drawString("Total Sold Items: "+max, 10, 50);
    g.drawString("Paying Method", 660, 60);
    g.drawString("Cash / Card", 660, 210);
    g.drawString("NUMBER OF DIFF. ITEMS / SALE", 10, 430);
    g.setFont(new Font("veranda", Font.PLAIN,13));
    // Items Pie chart
    int px = 10, py = 70, e = 0, n;
    int arc = (int)Math.ceil(((float)360)/max), a = 0, mx = 0;
    for (String S : soldItems) {
      n = salMap.get(S);
      g.setColor(colors[e]);
      g.drawString(String.format("%d %s (%3.2f ",n,S,(100f*n)/max)+"%)", px, py);
      g.fillArc(230, 30, 250, 250, a, n*arc);
      a += n*arc;
      py += 20;
      ++e;
    }
    // Paying mode Pie chart
    px = 660; py = 80; e = 6; a = 0;
    arc = (int)Math.ceil(((float)360)/total);
    for (String S : pMethod) {
      n = cash.get(S);
      g.setColor(colors[e]);
      g.drawString(String.format("%3.2f", (100f*n)/total)+"% "+S, px, py);
      g.fillArc(498, 20, 150, 150, a, n*arc);
      py += 20;
      a += n*arc;
      ++e;
    }
    // Card Pie chart
    px = 660; py = 230; e = 0; a = 0;
    arc = (int)Math.ceil(((float)360)/total);
    for (String S : cardLst) {
      n = credit.get(S);
      g.setColor(colors[e]);
      g.drawString(String.format("%3.2f", (100f*n)/total)+"% "+S, px, py);
      g.fillArc(498, 190, 150, 150, a, n*arc);
      py += 20;
      a += n*arc;
      ++e;
    }
    // Direct Sales / online Customers
    arc = (int)Math.ceil(((float)360)/total);
    float f = 100*((float)online)/total;
    if (online < total) {
      g.setColor(colors[3]);
      g.fillArc(530, 360, 100, 100, 0, 360);
      g.drawString(String.format("%3.2f", (100f-f))+"% Direct Sale", 660, 430);
    }
    if (online > 0) {
      g.setColor(colors[8]);
      g.drawString(String.format("%3.2f", f)+"% Online", 660, 410);
      g.fillArc(530, 360, 100, 100, 0, online * arc);
    }
    // Sales activities
    e = -1;
    px = 10; py = 400;
    int ox = 10, oy = 400;
    g.setColor(Color.red);
    for (java.util.List<Integer> iL:salList) {
      py = 400;
      n = iL.size();
      if (n > 1) py -= n*5;
      g.fillOval(px, py-3, 6, 6);
      g.drawString(""+n, px, py+15);
      if (e >= 0) g.drawLine(ox, oy, px, py);
      ox = px; oy = py;
      px += 30;
      e = n;
    }
    repaint( );
  }
  // required by repaint()
  public void paint(Graphics g) {
    g.drawImage(pBuf, 0, 0, null);
  }
  private String uID;
  private Image pBuf;
  private int width, height, max, online, total;
  private ArrayList<java.util.List<Integer>> salList;
  private ArrayList<String> soldItems, pMethod, cardLst;
  private HashMap<String, Integer> salMap, cash, credit;
  private Color[] colors = new Color[] { Color.pink, Color.white, Color.blue, Color.cyan, Color.orange,
                                         Color.lightGray, Color.magenta, Color.green, Color.red, Color.gray };
}
In the Constructor data for all relevant details are extracted from OODB Invoice. Finally the draw() method builds the Graphics from these extracted data. The Statistics Graphics is invoked by the AccountPanel as following (AccountTab.java):
Java:
      ...
1     JButton stat = new JButton("STATISTICS");
2     stat.setPreferredSize(new java.awt.Dimension(100, 50));
      stat.addActionListener(ec -> {
        atxt.setBackground(java.awt.Color.WHITE);
3       int row = P.tables[3].getSelectedRow();
4       String uID = null;
5       if (row >= 0) {
6         String date = (String)P.tables[3].getValueAt(row, 0);
7         uID = ((Invoice)P.oodb.read(P.dbInvoice, date)).getOwner();
        }
8       Statistics statistics = new Statistics(P, uID);
9       (new Show((uID == null?"GENERAL STATISTICS":"STATSTICS OF "+uID), statistics)).setVisible(true);
10      P.tables[3].clearSelection();
11      statistics.draw();
      });
      ...
  • Line 3..7: try to get the UserID. If null, it's the general statistics. Otherwise it's the statistics of the chosen UserID which is derived from the Invoice OODB with the selected key "date".
  • Line 8: create Object Statistics (here: an extension of JButton)
  • Line 9: invoke Show with Statistics (see Show HERE).
  • line 10: start to draw the Graphics (charts, pies, etc.)

That's all. Have fun, folk!

Joe Nartca.
PS. I am away for a very long time...Hopefully Condongjava.com won't be deluged under spams of all types of different advertising scums...

Incl. CDJ.zip with the following content:

Directories:
  1. Boutique (main)
  2. Customer (Boutique for customer)
  3. JFX (the JfxODBServer)
  4. Log_node1 (the logs for Node 1)
  5. OODB_Node1 (the OODB)
  6. SWING (the JODBServer)
How to install?
1) download and unzip to a directory of your choice.
2) set the global CLASSPATH to include all JAR files. For example the directory Boutique Dir_Boutique.png
Example: on Windows 10
Code:
set CLASSPATH= .; C:\links\Boutique\JFX\JfxODBServer.jar;C:\links\Boutique\JFX\joodb.jar;C:\links\Boutique\SWING\JODBServer.jar;C:\links\Boutique\SWING\mvc.jar;C:\links\Boutique\SWING\joodb.jar;%CLASSPATH%
Move to directory SWING or JFX and start the Server as following
Code:
// the batch file
C:\links\Boutique\SWING>g1
// or
javaw -jar -Xms2048M JODBServer.jar odbConfig_Node1.txt
SWING_SERVER.png
SWING OODB Server (JODBServer.jar)

If you prefer JavaFX you repeat the same procedure with JFX.
JFX_SERVER.png
JFX ODB Server (JfxODBServer.jar)

As Online client:
3) Open another CMD window of directory Customer and start "javaw -jar -Xms1048M online.jar" or double-click at online.jar in the directory customer
4) login with UserID: bobby or billy, Password: bobby or billy (case sensitive)

As Provider: same procedure with the following personalities (case sensitive):
- userID: ceo, password: chief. ceo is the superuser who can start and shutdown the OODB Server, or add a new user to the staff. Note: the online clients access the OODB with userID: customer and Password: customer. Superuser ceo can change them, but the source Boutique.java of Customer must be changed arnd recompiled, too. Some pre-installed UserIDs with Passwords
  • userID: jane, password: jane. Salesperson
  • userID: elli, password: elli. Order staff
  • userID: beth, password: beth. Billing staff
  • userID: sarah, password: sarah. Accounting staff
  • userID: david, password: david. HR man
Note: the images of jane, elli, beth, sarah are the celebs public pictures on the WEB. Sorry for using your beautiful pictures for my educational purposes. Tons of thanks! Joe.

If you let me do it, I'll understand it
said an ancient great man from China. So, try to build yourself the Boutique project and you could learn a lot. More than you could imagine. The build.bat and cpy.bat of Customer Directory files could give you some inspiration.
 

Attachments

Sửa lần cuối: