ZK 5.0 and FlashChart Print E-mail
Spread the news
Delicious
Furl it!
Spurl
NewsVine
Reddit
YahooMyWeb
Technorati
Digg
User Rating: / 0
PoorBest 
Written by gbowerman   
Monday, 11 January 2010
It's been a while since I've worked with the ZK programming language (or Rich Internet Application framework) so it was nice to get an opportunity to play around with the latest release. To ease myself back in gently I wrote a simple letter frequency analyzer to help with solving ciphers (note this post is not Informix-related for a change)..
 
The current release of ZK is 5.0 RC2, and I notice it is now split up into a free community edition, and commercial professional and enterprise edtions. The free edition no longer includes JFreeChart but fortunately it does include FlashChart which provides basic charting capability and is very easy to use.  (Click on the image to view it full size):
 
 
image  
The program is only about 40 lines so all the code can be self-contained in a single .zul file to be deployed under an app server such as Tomcat.
 
First I'll write the presentation layer which consists of a text box, a button and a chart:
 

  
      Put your sample text here
  

  
Next comes the Java code that is executed when the "Calculate" button is clicked. This reads the text in the "targetText" texbox, counts the frequency of different letters and feeds it into a chart which is associated with the "chart" FlashChart:
 
CategoryModel model = new SimpleCategoryModel();
// update chart per letter
void processString(String targetString)
{   
    int[] alphabet = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int total = 0;
    int targetLen = targetString.length();
    char[] targetArray = targetString.toCharArray();
    for (int i = 0; i < targetLen; i++)
    {
        // count each letter and ignore other characters
        if (Character.isLetter(targetArray[i]))
        {
            int ch = Character.toUpperCase(targetArray[i]) - 0x41;
            alphabet[ch]++;
            total++;
        }
    }
    model.clear();   
    for (char i = 'A'; i <= 'Z'; i++)
    {
        // send each letter and its frequency percentage to the chart
        Double freq = new Double(alphabet[i - 0x41]);
        model.setValue(i.toString(), i.toString(), (freq / targetLen) * 100);
    }
    chart.setModel(model);

// process current text
processString(targetText.value);
]]>

That's it. In my case I created a "freq" directory under my Tomcat webapps path, copied the above code in as index.zul and created a WEB-INF sub-directory as per the ZK Quick Start Guide.
 
Overall I like the look and feel of ZK 5, it is smoother, faster and slicker looking than earlier editions.  

Author: gbowerman

 

 

Copyright © 2010. All rights reserved. Copyright




Discuss this article on the forums. (0 posts)


 
< Prev   Next >
Main Menu
Home
Forum
Directory
Links
Search
Newsflash
IIUG Insider - August 2010
Read more...
 
New Informix 11.5.XC7 and OAT version 2.28 are now avaiable in the Member Area. Download now!
Read more...
 
Ads
Login Form





Lost Password?
No account yet? Register