Updateing chart values

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

Updateing chart values

Post by milze » Mon Apr 09, 2001 11:13 am

Hi,

I'm evaluating JFreeChart version 0.5.6. The tool is really cool and everything works fine, except when I try to update the chart.
After the chart has been instaniated i want to press a button and see the chart changed values updated

Coul you please help me ?

Thank you

Milze


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Paint.*;
import javax.swing.border.*;
import com.jrefinery.chart.*;
import com.jrefinery.chart.data.*;
import com.jrefinery.chart.event.*;
import com.jrefinery.chart.ui.*;
import com.jrefinery.util.ui.*;

public class chartSQL1 extends JPanel
{

protected BevelBorder border;
java.awt.Color color1;
protected Plot myPlot;
protected Axis myHorizontalAxis ;
protected Axis myVerticalAxis;
protected JPanel panel1 = new JPanel();
Number[][] data = new Integer[][] {
{new Integer(12),new Integer(19),new Integer(13),new Integer(1)},
{new Integer(15),new Integer(13),new Integer(16), new Integer(5)},
{new Integer(0),new Integer(0),new Integer(0),new Integer(0)}};

private JButton plainButton;


public chartSQL1()
{
BevelBorder border = new BevelBorder(BevelBorder.LOWERED);
this.setBorder(new TitledBorder( border, "Chart", TitledBorder.LEFT, TitledBorder.ABOVE_TOP));

CategoryDataSource myDataSource = new DefaultCategoryDataSource(data);
JFreeChart myChart = JFreeChart.createLineChart(myDataSource);
JFreeChartPanel myChartPanel = new JFreeChartPanel(myChart);
// The data must be an array of Numbers…

myChart.setAntiAlias(true);
myChartPanel.setPreferredSize(new Dimension(640, 340));

myChart.setChartBackgroundPaint(new GradientPaint(0, 0, Color.white, 1000, 0, Color.blue));

myChartPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(4, 4, 4, 4),
BorderFactory.createLineBorder(Color.darkGray, 1)));


myChart.setTitle("SQL Progress Chart");
Plot myPlot = myChart.getPlot();
Axis myHorizontalAxis = myPlot.getAxis(Plot.HORIZONTAL_AXIS);
myHorizontalAxis.setLabel("Semester");

Axis myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("Queries per day");

// cast required - CategoryDataSource provides read-only methods
DefaultCategoryDataSource d = (DefaultCategoryDataSource)myDataSource;
String[] names = new String[] {"Class", "User", ""};
d.setSeriesNames(names);

String[] categories = new String[]{"Week 1-3", "Week 4-6","Week 7-9", "Week 10-12" };
d.setCategories(categories);
panel1.add(myChartPanel);
add(panel1);


plainButton = new JButton( "Update chart" );
add( plainButton );

ButtonHandler handler = new ButtonHandler();
plainButton.addActionListener( handler );


}


public Number[][] getData()
{
return this.data;
}

protected void setData(Number[][] data)
{
this.data = data;
}


private class ButtonHandler implements ActionListener , DataSourceChangeListener
{
public void actionPerformed( ActionEvent e )
{
data = new Integer[][]
{
{new Integer(12), new Integer(19),new Integer(13), new Integer(1)},
{new Integer(15), new Integer(13),new Integer(16), new Integer(9)},
{new Integer(0), new Integer(0),new Integer(0), new Integer(0)}
};

setData(data);
}// end of innerer class with updated data


public void chartChanged(ChartChangeEvent event)
{
setData(data);
JOptionPane.showMessageDialog( null,"chart changed: ");
}

public void dataSourceChanged(com.jrefinery.chart.event.DataSourceChangeEvent e)
{
setData(data);
JOptionPane.showMessageDialog( null,"datasource changed: ");
}

} // end of class



}

David Gilbert

RE: Updateing chart values

Post by David Gilbert » Tue Apr 17, 2001 12:22 pm

Hi Milze,

I had a read through your code (but haven't compiled and run it yet). It looks like clicking the button should create a new array of data, and pass a reference to the array to the chartSQL1.setData(...) method. Then setData() stores this reference in an instance variable 'data' that belongs to the chartSQL1 object. But it doesn't update the datasource that has been assigned to the chart.

What you need to do is use your new array to create a new data source, and then call JFreeChart.setDataSource(...). This should replace the existing datasource and then the chart will be redrawn.

JFreeChart has been designed to produce static charts - the only way to update a chart (for now) is to completely replace the chart's datasource as I described above, which then causes the chart to be redrawn. But I plan to change this in the future with the addition of some dynamic datasource classes.

Hope this helps,

DG.

Locked