Dynamic Chart : Plot Area Extension

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Karthick

Dynamic Chart : Plot Area Extension

Post by Karthick » Fri Dec 27, 2002 2:29 pm

Hi,


Using JFREECHART API , I developed an timeSeries graph. It worked fine. Then I updated model data. i.e. I kept adding items in model data at periodic intervals say 1 or 3 secs. Even view has got updated properly. But view has updated with in the default drawable area (say 600* 400). So when I keep on adding data , it appeared in a cramped state. Is there is a possibity to incorporate an Scroll Pane. So that chart area gets extended and user should be able to view the graph entirely i.e. meaning that chart area should get grow and viewport should be shifted to latest plotted region. And graph has to maintain uniform distance between grids.

Herewith I have attached my source code.


import com.jrefinery.data.*;
import com.jrefinery.chart.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;


public class MyTimeChart extends TimerTask
{
TimeSeriesCollection model = null;
long period = 0;
Plot pl = null;
BasicTimeSeries s1;
BasicTimeSeries s2;
public MyTimeChart (String[] args)
{
// create a chart...

model = createDataset();


JFreeChart chart = ChartFactory.createTimeSeriesChart("Karthick Sample Line Chart", "CATEORY","AXIS", model, true);

pl = chart.getPlot();
DateAxis xax = (DateAxis)(((XYPlot)pl).getHorizontalValueAxis());
xax.setTickUnit( new DateUnit(Calendar.SECOND,5));
// create and display a frame...


JFrame frame = new JFrame();
ChartPanel chartPanel = new ChartPanel(chart,500,300,400,200,5000,500,true, true, true, true, true, true );
chartPanel.setMouseZoomable(true,true);

frame.getContentPane().add(new JScrollPane(chartPanel));
frame.setSize(700,200);
frame.setVisible(true);

try
{
int seconds = Integer.parseInt(args[0]);
period = seconds * 1000;
}
catch(Exception e)
{
e.printStackTrace();
}
schedule();
}

int counter = 0;
public TimeSeriesCollection createDataset() {

s1 = new BasicTimeSeries("L&G European Index Trust", Second.class);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 181.8);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 167.3);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 153.8);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 167.6);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 158.8);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 148.3);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 153.9);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 142.7);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 123.2);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 131.8);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 139.6);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 142.9);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 138.7);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 137.3);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 143.9);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 139.8);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))) , 137.0);
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 132.8);

counter = 0;

s2 = new BasicTimeSeries("L&G UK Index Trust", Second.class);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 181.8);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 129.6);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 123.2);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 117.2);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 124.1);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 122.6);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 119.2);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 116.5);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 112.7);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 101.5);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 106.1);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 110.3);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 111.7);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 111.0);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 109.6);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 113.2);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 111.6);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 108.8);
s2.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), 101.6);

TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);

return dataset;

}

private void schedule()
{
java.util.Timer timer = new java.util.Timer();
timer.schedule(this,0,period);

}

int inc = 10;
boolean test = true;
public void run()
{
Random rd =new Random();
System.out.println("SCHEDULED");
try
{
s1.add(new Second(new Date(System.currentTimeMillis() + ((++counter)*1000))), rd.nextDouble()*150);
s2.add(new Second(new Date(System.currentTimeMillis() + ((counter)*1000)+500)), rd.nextDouble()*150);

}
catch(Exception e)
{
e.printStackTrace();
}

}

public static void main(String[] args)
{
new MyTimeChart(args);
}
}// MyTimeChart



Thanks & regards,
S.Karthick

David Gilbert

Re: Dynamic Chart : Plot Area Extension

Post by David Gilbert » Fri Jan 03, 2003 5:59 pm

One of the design principles in JFreeChart is that it should try to draw a
reasonable looking chart in whatever space is allocated (so for example, in
most of the demos you can resize the frame and the chart will redraw
accordingly). There is no concept of a preferred size for the chart, which
would be required to have the chart panel grow inside a JScrollPane.

But I think you could do this outside of JFreeChart, as long as you have some
way to calculate the width you want for the ChartPanel (based on the number
of data items for the chart). Just set the preferred size for the ChartPanel
in your own code.

If I get some spare time, I'll look into whether it might be possible to get
the JFreeChart class to communicate with the ChartPanel class to automate
this...

Regards,

Dave Gilbert

Locked