Problems with Updating XYLineChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jotun
Posts: 4
Joined: Mon Oct 17, 2005 2:42 pm

Problems with Updating XYLineChart

Post by jotun » Mon Oct 17, 2005 2:50 pm

Hi,

First at all, i'm german and my english isnt that good. i read several threads but they didnt help me out.

My problem is that my xylinechart don't update probably. After clcking on the Test-Button, the new value only appears when you click on the chart (?) and the y-axis doesnt extend automaticly. Maybe the dataset update is not correct ?

Here the code:

Code: Select all

package Example;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Test4 extends JFrame {

    private JPanel jContentPane = null;
    private JButton jButton = null;
    private ChartPanel chartPanel = null;
    XYSeriesCollection dataset = new XYSeriesCollection();
    private JButton getJButton() {
        if (jButton == null) {
            jButton = new JButton();
            jButton.setBounds(new java.awt.Rectangle(15,9,109,20));
            jButton.setText("Test");
            jButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    
                    dataset.getSeries(0).getDataItem(3).setY(400);
                    chartPanel.firePropertyChange("a",true,true);
                }
            });
        }
        return jButton;
    }

    private XYSeriesCollection createDataset() {
        
        XYSeries series = new XYSeries("Series 1");
        series.add(1, 0);
        series.add(2, 0);
        series.add(3, 0);
        series.add(4, 0);
        series.add(5, 0);
        series.add(6, 0);
        series.add(7, 0);
        series.add(8, 0);
        series.add(9, 0);
        series.add(10, 0);
        series.add(11, 0);
        series.add(12, 0);
        series.add(13, 0);
        series.add(14, 300);
        series.add(15, 0);
        series.add(16, 0);
        series.add(17, 0);
        series.add(18, 0);
        series.add(19, 0);
        series.add(20, 0);
        series.add(21, 0);
        series.add(22, 0);
        series.add(24, 0);

        dataset.addSeries(series);        
        return dataset;
    }
    
    private JFreeChart getJFreeChart() {
    
        JFreeChart chart = ChartFactory.createXYLineChart(
            "XY Line Chart Test3", // chart title
            "Stunde", // x axis label
            "Daten", // y axis label
            (XYDataset)createDataset(), // data
            PlotOrientation.VERTICAL,
            true, // include legend
            true, // tooltips
            false // urls
        );       
        
        XYPlot plot = (XYPlot) chart.getPlot();
        //plot.setDataset(1,dataset2);
        XYItemRenderer r = plot.getRenderer();
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
        renderer.setDefaultShapesVisible(true);
        renderer.setDefaultShapesFilled(true); 
        chart.getXYPlot().getDomainAxis().setRange(0,23);
        return chart;
    }

    
    private ChartPanel getCreateChart() {
        
        if (chartPanel == null) {
            ChartPanel chartPanel = new ChartPanel(getJFreeChart());        
            chartPanel.setBounds(new java.awt.Rectangle(14,33,521,300));            
        }
        return chartPanel;
    }
    
    private void initialize() {
        this.setSize(547, 362);
        this.setContentPane(getJContentPane());
        this.setTitle("JFrame");
    }

    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            
            chartPanel = new ChartPanel(getJFreeChart());        
            chartPanel.setBounds(new java.awt.Rectangle(14,33,521,300));
            
            jContentPane.add(chartPanel, null);
            jContentPane.add(getJButton(), null);
        }
        return jContentPane;
    }
    
    public Test4() {
        super();
        initialize();
    }

    public static void main(String[] args) {
        Test4 app = new Test4();
        app.show();

    }
    
}  
Thanks a lot for helping !
Greets

javydreamercsw
Posts: 15
Joined: Fri Oct 07, 2005 2:32 pm

Post by javydreamercsw » Mon Oct 17, 2005 3:25 pm

For the refreshing problem add the following:

Code: Select all

chartPanel.repaint();
in the button action listener.

Still working on the axis resizing problem.

javydreamercsw
Posts: 15
Joined: Fri Oct 07, 2005 2:32 pm

Post by javydreamercsw » Mon Oct 17, 2005 3:54 pm

Found it! Here's my answer:

Code: Select all

jButton.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) { 
                    
                    dataset.getSeries(0).getDataItem(3).setY(400); 
                    chartPanel.firePropertyChange("a",true,true); 
                    chartPanel.repaint();                
                    chartPanel.zoomOutRange(1.1,1.1);
                    chartPanel.autoRangeBoth();
                } 
            }); 
Maybe isn't the best but it works. Somehow and/or for somereason the autoRangeBoth() method doesn't do a thing unless you zoom in or out previously. If you try the code the zoom out goes unnoticed.

Hope it helps! We both learned a new trick!

jotun
Posts: 4
Joined: Mon Oct 17, 2005 2:42 pm

Post by jotun » Mon Oct 17, 2005 7:54 pm

Hi,

Thanks for your answer. I tried your changes:.

Code: Select all

            
jButton.addActionListener(new java.awt.event.ActionListener() { 
                public void actionPerformed(java.awt.event.ActionEvent e) { 
                    
                    dataset.getSeries(0).getDataItem(3).setY(400); 
                    chartPanel.repaint();      
                    chartPanel.zoomOutRange(1.1,1.1); 
                    chartPanel.autoRangeBoth();
                } 
The method repaint() paint the new values, thats good ! But the axis still dont change.

The autoRangeBoth() ist underlined red "The method autoRangeBoth() is undefined for the type ChartPanel". Does it work for you ?

Only the zoomOutRange() changes the axis, but afer a few clicks on the button its zooms too far ?!

For your information, the finished programm should refresh the chart ever second.

Maybe you got an other idea ?

Thanks
Greets

javydreamercsw
Posts: 15
Joined: Fri Oct 07, 2005 2:32 pm

Post by javydreamercsw » Mon Oct 17, 2005 8:05 pm

What version of JFreeChart you have? It works for me. I'm in the latest version. My code is working.

About the refreshing period it depends on how you'll update the data. It doesn't make sense to update each sec for example. Use the events instead.

Give me more details to see if I can help.

jotun
Posts: 4
Joined: Mon Oct 17, 2005 2:42 pm

Post by jotun » Mon Oct 17, 2005 8:39 pm

hi,

i also use the lastest version, i downloaded it a few hours ago.
hm, strange. i dont get the problem right now.

hey ! i found a method calls restoreAutoBounds(). with this one it works. but also only when you call zoomOutRange() before it.


i didnt think much about the refreshing period yet. but it must update every second. what do you mean with "the events" ?

greets !

javydreamercsw
Posts: 15
Joined: Fri Oct 07, 2005 2:32 pm

Post by javydreamercsw » Mon Oct 17, 2005 8:48 pm

You have the developer's guide? There are events fired when certain things change, i.e. the data set. If you use those to your advantage you'll update each time the data changes. So you update when only when needed.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Post by skunk » Mon Oct 17, 2005 9:48 pm

Did you try calling

Code: Select all

dataset.getSeries(0).update(3, new Integer(400));
instead?

jotun
Posts: 4
Joined: Mon Oct 17, 2005 2:42 pm

Post by jotun » Tue Oct 18, 2005 11:47 am

@javydreamercsw

no, i dont have the developers guide. can you tell me the name of those events and an example who to use them ?

@skunk

yes, i called "dataset.getSeries(0).update(3, new Integer(400))". because i dont know a other way to update the dataset. is there one ?

javydreamercsw
Posts: 15
Joined: Fri Oct 07, 2005 2:32 pm

Post by javydreamercsw » Tue Oct 18, 2005 12:41 pm

For events see Chapter 26 in the guide.

Another way to update a series is

Code: Select all

public void setDataset(int index, CategoryDataset dataset);
Assigns a dataset to the plot. The new dataset replaces any existing
dataset at the specified index. It is permitted to set a dataset to null (in
that case, no data will be displayed on the chart).
See chapter 30 for more info.

look slike you need to do at least a bit of reading in the guide. I know is large but focus in your interests so you just read what you need at start, then you'll get a hang of it eventually.

Locked