Sunday, February 22, 2009

How to make scrolling real-time plot?

Scrolling plot is a special plot with x axis parameter whose value always increases. For example, time series chart. Making a scrolling plot using JFreeChart is fairly easy because you can set a value range for the x axis and setAutoRange to true. Also, setup the how much you want the plot to scroll when the range out of range. For example:

ValueAxis xaxis = new NumberAxis();
xaxis.setRange(0, 100);
xaxis.setAutoRange(true);
xaxis.setFixedAutoRange(100);

// Note: when axis is set to use fixed auto range, the
// margin value is not used in JFreeChart. Here, we borrow
// the variable to let plot to shifting buffer.
xaxis.setLowerMargin(0.1); // 10% margin
xaxis.setUpperMargin(0.1);

Then, periodically watch the value on x axis and adjust the range accordingly. I have modified jfreechart-1.0.12 by adding real time plotting capability and created examples for both realtime xy plot and scrolling plot under the org.jfree.chart.demo. You can give the updated package jfreechart-1.0.12rt.zip a try. You can also check the similar post I made at jfree forum to follow the discussion.

The demos are bare minimal. Since Jfreechart is very powerful, you can add some decorated pieces, such as: grid, background image, etc fancy stuff for fun and also verify there shouldn't have any noticeable performance deterioration.


Friday, February 20, 2009

View Android Source Code in Eclipse

Recently I was working with my friend from Hitachi software in Japan about our patent invention VSF during our time at U of M. I was very impressed that he has made a comercial product, anyWarp for BREW,based on that patent. This reminds me about Android and I started working on it since then.

Android is awesome but its SDK doesn't boundle source jar file. I guess Google just wants to place some mental challenges on us because the instruction of getting source code and making it work inside Eclipse is hard to follow. Well, I found some posts on this issues but I have to combined them together to get it work (unfortunately, none of them works for my case individually). I have a Windows XP (sp3) PC and a Redhat Linux machine (RHEL WS4). Here is my procedure to save people's time:
1. Download Android SDK and install it.
2. Install Git on the Linux machine. (current version is: 1.6.1.3)
3. Goto Linux machine to get the Android source code:
$ mkdir Android; cd Android
$ git clone git://git.source.android.com/platform/frameworks/base android-api
$ cd android-api; git checkout
4. zip all java files under android-api directory but preserve the directory structure.
$ find . -type f -iname "*.java" -print | xargs zip android_src.zip -
I have uploaded android_src.zip (ver 1.0r2) onto my googlepages site. You can skip previous step 2 to step 4 if you use source I built.
5. copy over the created android_src.zip into the Android SDK folder on Windows. Just put it side-by-side with android.jar
6. Inside Eclipse, let's assume you have already loaded Android sample projects correctly and you have android_src.zip source code already. But the Android Eclipse plugin doesn't allow to modify the Java source attachment of android.jar in the Android Library. See Eric Burke's post for detail. But, I just want to do a little different than his approach because it doesn't work for me when I follow his instruction on creating sources folder.
7. Follow the steps:
a. Select the project on Eclipse and right click mouse and choose Properties.


b. Select the Android Library and hit "Remove" to remove it.
c. Hit "Add External JARs..." and select android.jar from Android SDK installation folder. Now, android.jar is treated as the normal jar file.
d. Now, you can go ahead to attach android_src.zip to android.jar just like any other java source attachments for jar files.
8. I have tested it and it works great. Also, if multiple Android projects exist inside workspace, the step 7 only needs to be done for only one project and others just work immediately without any change.

Sunday, February 1, 2009

How to make JFreeChart support realtime charting?

JFreeChart is probably the best Java chart library I have ever used. It has lots of excellent features. Unfortunately, it doesn't support realtime charting. In many cases, Real-time charting is a very important feature to have. For example, my previous data acquisition project needs data refreshing rate at least 10 times per second. It is also very important for any financial chart, for example: intra-day stock chart.

Well, it is not that hard to get real-time capability in JFreeChart. I have successfully made a relatively small change to get it work. The performance problem in JFreeChart is that it completely repaints chart for each update, including, chart border, title, Axis, etc. User tends to simply add point into the dataset whenever a new point is available. This causes JFreeChart redraw all those points. The idea is very simple: Just let JFreeChart to use buffer to draw point incrementally. For any real time point, only the draw the latest points (2 points).

I have posted a detailed description along with demo source code at JFreeChart