dynamically update

A discussion forum for Orson Charts (a 3D chart library for the Java platform).

dynamically update

Postby marcio.nandi » Thu Sep 15, 2016 4:26 am

Hello, I'm using OrsonCharts library and it is really amazing, but I have 2 questions.
In my application I use a chart that updates dinamically, but when I add a new value it simply don't update. In JFreechart I used "dataset.setNotify(true)" but here it updates the chart, but fires a null pointer exception in some drawComponent method in Chart3DPanel class. Am I doing something wrong or is there a method that I can call to update the chart safely?
Second: How can I hide the legend?

Thank you in advance!!!
marcio.nandi
 
Posts: 5
Joined: Wed Sep 09, 2015 2:47 pm

Re: dynamically update

Postby david.gilbert » Thu Sep 15, 2016 11:03 am

Please send the stack trace you are seeing and, if possible, a small demo program that illustrates the problem. In general, the update mechanism should work just as in JFreeChart (that is, you update the dataset and all the chart refresh is taken care of automatically).
david.gilbert
JFreeChart Project Leader
 
Posts: 11653
Joined: Fri Mar 14, 2003 10:29 am

Re: dynamically update

Postby marcio.nandi » Tue Sep 20, 2016 1:13 am

Hello. Ok.
Here is a demo version of the problem. It represents the class that is having problems. In the class, I implement a SerieChangeListener (JFreechart interface) to build 3D points from 3 TimeSeries classes. Each time the series change, a new 3D point is added. In the demo, the exception fires sometimes, but in the program it fires each time a serieChangeEvent is dispatched.

Code: Select all
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Random;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.SeriesChangeEvent;
import org.jfree.data.general.SeriesChangeListener;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;

import com.orsoncharts.Chart3D;
import com.orsoncharts.Chart3DFactory;
import com.orsoncharts.Chart3DPanel;
import com.orsoncharts.data.xyz.XYZSeries;
import com.orsoncharts.data.xyz.XYZSeriesCollection;
import com.orsoncharts.graphics3d.swing.DisplayPanel3D;

public class Test3d implements SeriesChangeListener {
   //3d elements
   XYZSeries serie3d = new XYZSeries("Test");
   XYZSeriesCollection data3d = new XYZSeriesCollection();
   Chart3D chart3d;
   DisplayPanel3D panel3d;
   JFrame frame3d;
   //2d elements
   TimeSeries serie2d = new TimeSeries("xAxis");
   TimeSeriesCollection data2d = new TimeSeriesCollection();
   JFreeChart chart2d;
   ChartPanel panel2d;
   JFrame frame2d;
   double counter = 0;
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Test3d test = new Test3d();
      test.data3d.add(test.serie3d);
      test.chart3d = Chart3DFactory.createXYZLineChart("", "", test.data3d, "x", "y", "z");
      test.panel3d = new DisplayPanel3D(new Chart3DPanel(test.chart3d));
      test.frame3d = new JFrame("3d test");
      test.frame3d.getContentPane().add(test.panel3d);
      test.frame3d.setPreferredSize(new Dimension(300, 300));
      
      test.data2d.addSeries(test.serie2d);
      test.chart2d = ChartFactory.createTimeSeriesChart("", "time", "value", test.data2d);
      test.panel2d = new ChartPanel(test.chart2d);
      test.frame2d = new JFrame();
      test.frame2d.getContentPane().setLayout(new GridBagLayout());
      test.frame2d.getContentPane().add(test.panel2d, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));
      
      test.frame2d.setVisible(true);
      test.frame2d.pack();
      test.frame3d.setVisible(true);
      test.frame3d.pack();
      
      test.serie2d.addChangeListener(test);
      test.serie2d.add(new TimeSeriesDataItem(new Millisecond(), 0.5));
      try {
         Thread.sleep(200);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      while(true) {
         test.serie2d.add(new TimeSeriesDataItem(new Millisecond(),(new Random()).nextDouble()));
         test.data3d.setNotify(true);
         try {
            Thread.sleep(500);
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
   }
   @Override
   public void seriesChanged(SeriesChangeEvent arg0) {
      // TODO Auto-generated method stub
      serie3d.add(((TimeSeries)arg0.getSource()).getValue(((TimeSeries)arg0.getSource()).getItemCount() - 1).doubleValue(), counter, 0.0);
      counter++;
   }

}
marcio.nandi
 
Posts: 5
Joined: Wed Sep 09, 2015 2:47 pm

Re: dynamically update

Postby david.gilbert » Mon Sep 26, 2016 6:43 pm

At least in your demo program, the problem appears to be that you are updating the dataset from off the event-dispatch-thread (EDT) (your while loop that updates the dataset is in the main() method, and that runs on it's own thread). It's important to update datasets on the EDT for synchronisation.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11653
Joined: Fri Mar 14, 2003 10:29 am

Re: dynamically update

Postby marcio.nandi » Tue Sep 27, 2016 8:51 pm

Ohhh great. I used a invokeLater() instance and the problem was solved.

Many thanks.
marcio.nandi
 
Posts: 5
Joined: Wed Sep 09, 2015 2:47 pm


Return to Orson Charts

Who is online

Users browsing this forum: No registered users and 2 guests