Help me!!! Set auto refresh in jfreechart applet like a real

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

Help me!!! Set auto refresh in jfreechart applet like a real

Post by GeForZero » Tue Mar 04, 2003 8:13 am

I want to set the auto refresh in jfreechart applet same realtime graph.
I don't want to refresh brower but I want to refresh only applet.
Help me please!

David Gilbert

Re: Help me!!! Set auto refresh in jfreechart applet like a

Post by David Gilbert » Tue Mar 04, 2003 10:02 am

All you need to do is update the dataset and the chart will be redrawn automatically. For an example, see:

http://www.object-refinery.com/jfreechart/applet.html

The source code for this example is included in this post:

http://www.object-refinery.com/phorum-3 ... 409&t=3336

You might need to modify it a little to work with 0.9.6.

Regards,

Dave Gilbert

GeForZero

Re: Help me!!! Set auto refresh in jfreechart applet like a

Post by GeForZero » Wed Mar 05, 2003 5:29 am

I use ver 0.9.6 and I read your code but i don't clear.
First, How new Applet1.DataGenerator().start(); work?

and in the function

class DataGenerator extends Timer implements ActionListener {
/**
* Constructor.
*/
DataGenerator() {
super(100, null);
addActionListener(this);
}

public void actionPerformed(ActionEvent event) {
long f = Runtime.getRuntime().freeMemory();
long t = Runtime.getRuntime().totalMemory();
addTotalObservation(t);
addFreeObservation(f);
}

--------------------------------------------
Can you tell me about this code ?



And this is my source code.

/* ======================================
* JFreeChart : a free Java chart library
* ======================================
*
* Project Info: http://www.object-refinery.com/jfreechart/index.html
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* -----------------
* DualAxisDemo.java
* -----------------
* (C) Copyright 2002, 2003 by Simba Management Limited and Contributors.
*
* Original Author: David Gilbert (for Simba Management Limited);
* Contributor(s): -;
*
* $Id: DualAxisDemo.java,v 1.10 2003/02/05 17:15:20 mungady Exp $
*
* Changes
* -------
* 19-Nov-2002 : Version 1 (DG);
*
*/

package com.jrefinery.chart.demo;

import javax.swing.JApplet;
import javax.swing.*;
import java.sql.*;
import java.lang.String;
import java.awt.Color;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.Legend;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.ChartPanel;
import com.jrefinery.chart.axis.Axis;
import com.jrefinery.chart.axis.ValueAxis;
import com.jrefinery.chart.axis.HorizontalCategoryAxis;
import com.jrefinery.chart.axis.VerticalNumberAxis;
import com.jrefinery.chart.plot.CategoryPlot;
import com.jrefinery.chart.renderer.DefaultDrawingSupplier;
import com.jrefinery.chart.renderer.LineAndShapeRenderer;
import com.jrefinery.chart.renderer.DrawingSupplier;
import com.jrefinery.chart.renderer.VerticalBarRenderer;
import com.jrefinery.data.CategoryDataset;
import com.jrefinery.data.DefaultCategoryDataset;
import com.jrefinery.ui.ApplicationFrame;
import com.jrefinery.ui.RefineryUtilities;
import COM.ibm.db2.jdbc.net.*;

/**
* A simple demonstration application showing how to create a dual axis chart based on data
* from two {@link CategoryDataset} instances.
*
* @author David Gilbert
*/
public class DualAxisApp extends JApplet {

/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
private TimeSeries time;
public static Connection con=null;
public static Statement stmt=null;
public static String url = "jdbc:db2://eis.prac.ibm.com:6789/eiswebdb";


static{
try{
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver");
con = DriverManager.getConnection(url,"webadmin","coop");
stmt = con.createStatement();
}
catch (Exception e) {
e.printStackTrace();
}

}



public DualAxisApp() {

// super(title);

CategoryDataset dataset1 = createDataset1();

// create the chart...
JFreeChart chart = ChartFactory.createStackedVerticalBarChart(

"Yield", // chart title
"Time", // domain axis label
"% Defect", // range axis label
dataset1, // data
true, // include legend
true,
false
);

// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
DrawingSupplier supplier = new DefaultDrawingSupplier();

// set the background color for the chart...
chart.setBackgroundPaint(Color.orange);
chart.getLegend().setAnchor(Legend.SOUTH);

// get a reference to the plot for further customisation...
CategoryPlot plot = chart.getCategoryPlot();
plot.setDomainAxisLocation(Axis.BOTTOM);

// set range of stack chart
/* CategoryAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis = plot.getRangeAxis();
axis.setRange(0.0, 200.0);
*/

VerticalBarRenderer renderer1 = (VerticalBarRenderer) plot.getRenderer();
renderer1.setDrawingSupplier(supplier);
LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
renderer2.setDrawingSupplier(supplier);

//--------Create Line Chart Dataset
CategoryDataset dataset2 = createDataset2();
ValueAxis axis2 = new VerticalNumberAxis("% Yield");
plot.setSecondaryRangeAxis(axis2);
plot.setSecondaryDataset(dataset2);
plot.setSecondaryRenderer(renderer2);

// skip some labels if they overlap...
HorizontalCategoryAxis domainAxis = (HorizontalCategoryAxis) plot.getDomainAxis();
domainAxis.setSkipCategoryLabelsToFit(true);

// OPTIONAL CUSTOMISATION COMPLETED.

// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);


}

/**
* Creates a sample dataset.
*
* @return The dataset.
*/

private CategoryDataset createDataset1() {

DefaultCategoryDataset dataset = new DefaultCategoryDataset();
try{
String sql="select weekno, type, values from www.defect order by type, weekno ";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next())
{
// dataset.addValue(2.0, series1, category1);
// System.out.println(rs.getString(3));
float val = rs.getFloat("values");
String type = rs.getString("type");
String week = rs.getString("weekno");

dataset.addValue(val, type, week);
}

}
catch(SQLException ex)
{ ex.printStackTrace();
}
// rs.close();
return dataset;

}

/**
* Creates a sample dataset.
*
* @return The dataset.
*/
private CategoryDataset createDataset2() {

// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();

try{
String sql1="select weekno, values from www.yield";
ResultSet rs = stmt.executeQuery(sql1);

while(rs.next())
{
// dataset.addValue(98.0, series1, category1);
float val = rs.getFloat("values");
String cate = rs.getString("weekno");

dataset.addValue(val, "Yield", cate);
// System.out.println(rs.getString(1));
}
rs.close();
}

catch(SQLException ex)
{ ex.printStackTrace();
}

return dataset;

}

/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/

public void init() {

DualAxisApp demo = new DualAxisApp();
//demo.pack();
//RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);

}

}
/***********************************************

How can i change it? Please tell me.

Locked