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.


2 comments:

  1. I saw a few examples of your realtime samples. They are very good. I have got a feeling that you are a Genius with JFreeCharts and Java. But I have a problem. I need to draw a Map that would look like a XY Plot of small squares in place of pixels or dots. Now I could understand from your examples that it is possible to rescale the Graph when scrolled. But my necessity is to draw the map according to the values generated by a Java Program and rescale it as soon as the value generated is out of the shown range of the graph and then plot it, by using small square of a particular Color which also depends on the values Generated by the Java Program. Now I am going to ask for a thing which to me is too much but I have no way out. My project submission is within a few days before which I have to get this ready. So can you please write a Java Code that would plot a map using small squares (A Grid Map) of a particular Color depending on the value generated in realtime as I requested by taking the values from a Java Program (or I may embed your code itself in the main generating program, as you say me be good). Please mail me back to my mail ID with such a code with all its description so that I can understand within two Days (As I have no Time left and you are my Last Hope) and also tell me what to do next. Please do help me and I would remain indebted to you. My Mail ID is:- pradiptapb@gmail.com. Thanking you in advance.

    ReplyDelete
  2. One Point:- I do not want the mouse to Interfere, that is the Mouse would have no function othere than scrolling the Vertical and Horizontal ScrollBars Manually to see the parts of the map.

    ReplyDelete