How can I refresh the Category dataset

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
ashutosh
Posts: 15
Joined: Fri Dec 08, 2006 1:43 pm

How can I refresh the Category dataset

Post by ashutosh » Wed Dec 20, 2006 2:30 pm

Hi Everyone.

I'm trying to develope a chart that will take the value for the database and display. some data is inserted in the database at some interval, so the volume of the database is increasing.

I want to show the new value in the chart which is continously changing. how can I refresh or how this chart can automaticaly get refreshed when the dataset changes.

what are the methods that I can use to do this???

my code is attached along. Please have a look and give some suggessions, where and how shall I use the DatasetChangeEvent method.


Code: Select all



import java.awt.*;
import javax.swing.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.ui.RefineryUtilities;
import org.jfree.data.general.DatasetChangeListener;
import org.jfree.data.general.DatasetChangeEvent;


public class ChartProgramTest extends JFrame
{
public String series1 = "Data";
private JPanel jContentPane = null;
private JButton jButton1 =null;
private JButton jButton2 =null;
private DefaultCategoryDataset dataset = new DefaultCategoryDataset();
private ChartPanel chartPanel = null;
static int max=0;

private CategoryDataset createDataset()
{


// row keys...
String category = "ABC";
String series1 = "DATA";

// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
Database db = new Database();
max = db.getCount();
dataset.addValue(max, series1, category);

return dataset;

}

private JFreeChart getJFreeChart(CategoryDataset dataset)
{
JFreeChart chart = ChartFactory.createBarChart(
"Data Chart", // chart title
"Companies", // domain axis label
"Value", // range axis label
createDataset(), // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
false, // tooltips?
false // URLs?
);


chart.setBackgroundPaint(Color.white);

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

plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);

// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setRange(0.0, max*1.5);

// disable bar outlines...
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(true);

BarRenderer barrenderer = (BarRenderer)plot.getRenderer();
barrenderer.setMaximumBarWidth(0.15);

// set up gradient paints for series...
GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue,
0.0f, 0.0f, new Color(0, 0, 64));
renderer.setSeriesPaint(0, gp0);

return chart;
}


private JPanel getJContentPane()
{

jContentPane = new JPanel();
jContentPane.setLayout(new FlowLayout());
jContentPane.setPreferredSize(new Dimension(150,100));
chartPanel = new ChartPanel(getJFreeChart(dataset));
chartPanel.setBounds(new java.awt.Rectangle(14,33,521,300));
chartPanel.setRefreshBuffer(true);

jButton1 = new JButton("Exit");
jButton1.setBounds(new java.awt.Rectangle(15,9,109,20));

jButton2 = new JButton("Refresh");
jButton2.setBounds(new java.awt.Rectangle(15,9,109,20));

jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
System.exit(0);
}
});

jButton2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
chartPanel.repaint();
}
});


jContentPane.add(chartPanel, null);
jContentPane.add(jButton1, null);
jContentPane.add(jButton2, null);

return jContentPane;
}

private void initialize()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(720,500);
this.setContentPane(getJContentPane());
this.setTitle("Bar Chart");
}

public static void main(String[] args) throws InterruptedException, Exception
{
ChartProgramTest cpgm = new ChartProgramTest();
cpgm.initialize();
RefineryUtilities.centerFrameOnScreen(cpgm);
cpgm.show();

}
}

[code]


Please help me !!!

-Ashutosh

demonhead
Posts: 24
Joined: Thu May 25, 2006 5:44 am

Re: How can I refresh the Category dataset

Post by demonhead » Fri Dec 22, 2006 10:17 am

ashutosh wrote:Hi Everyone.

I'm trying to develope a chart that will take the value for the database and display. some data is inserted in the database at some interval, so the volume of the database is increasing.

I want to show the new value in the chart which is continously changing. how can I refresh or how this chart can automaticaly get refreshed when the dataset changes.

what are the methods that I can use to do this???

my code is attached along. Please have a look and give some suggessions, where and how shall I use the DatasetChangeEvent method.


Code: Select all



import java.awt.*;
import javax.swing.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.ui.RefineryUtilities;
import org.jfree.data.general.DatasetChangeListener;
import org.jfree.data.general.DatasetChangeEvent;


public class ChartProgramTest extends JFrame
{
public String series1 = "Data";
private JPanel jContentPane = null;
private JButton jButton1 =null;
private JButton jButton2 =null;
private DefaultCategoryDataset dataset = new DefaultCategoryDataset();
private ChartPanel chartPanel = null;
static int max=0;

private CategoryDataset createDataset()
{


// row keys...
String category = "ABC";
String series1 = "DATA";

// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
Database db = new Database();
max = db.getCount();
dataset.addValue(max, series1, category);

return dataset;

}

private JFreeChart getJFreeChart(CategoryDataset dataset)
{
JFreeChart chart = ChartFactory.createBarChart(
"Data Chart", // chart title
"Companies", // domain axis label
"Value", // range axis label
createDataset(), // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
false, // tooltips?
false // URLs?
);


chart.setBackgroundPaint(Color.white);

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

plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);

// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setRange(0.0, max*1.5);

// disable bar outlines...
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(true);

BarRenderer barrenderer = (BarRenderer)plot.getRenderer();
barrenderer.setMaximumBarWidth(0.15);

// set up gradient paints for series...
GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue,
0.0f, 0.0f, new Color(0, 0, 64));
renderer.setSeriesPaint(0, gp0);

return chart;
}


private JPanel getJContentPane()
{

jContentPane = new JPanel();
jContentPane.setLayout(new FlowLayout());
jContentPane.setPreferredSize(new Dimension(150,100));
chartPanel = new ChartPanel(getJFreeChart(dataset));
chartPanel.setBounds(new java.awt.Rectangle(14,33,521,300));
chartPanel.setRefreshBuffer(true);

jButton1 = new JButton("Exit");
jButton1.setBounds(new java.awt.Rectangle(15,9,109,20));

jButton2 = new JButton("Refresh");
jButton2.setBounds(new java.awt.Rectangle(15,9,109,20));

jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
System.exit(0);
}
});

jButton2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
chartPanel.repaint();
}
});


jContentPane.add(chartPanel, null);
jContentPane.add(jButton1, null);
jContentPane.add(jButton2, null);

return jContentPane;
}

private void initialize()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(720,500);
this.setContentPane(getJContentPane());
this.setTitle("Bar Chart");
}

public static void main(String[] args) throws InterruptedException, Exception
{
ChartProgramTest cpgm = new ChartProgramTest();
cpgm.initialize();
RefineryUtilities.centerFrameOnScreen(cpgm);
cpgm.show();

}
}

[code]


Please help me !!!

-Ashutosh[/quote]


If u have the demo jar of jfree there is a similar application that serves your purpose only thing you might have to check out is that whether tis meant for categoryplot or XYdataset. 
If you have bought the developer guide you will also have the source code

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Dec 22, 2006 12:26 pm

The DefaultCategoryDataset will generate the DatasetChangeEvent for you, if you just update it (with the addValue() method). This should result in the chart being updated automatically, since you are displaying it in a ChartPanel.

So you just need to write the code that periodically grabs the new value from the dataset, and updates the DefaultCategoryDataset.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked