I need help plotting a time series with java.
Here's what I have so far:
- a java internal frame where I want to display the graph
- a JFreeChart chart
- a ChartPanel panel added into the internal frame, initialized with my chart
- a TimeSeries series
- a XYDataset initialized with the series
- i create and instantiate everything so that a nice empty chart shows up when i make a new frame
now what I want to do is add data to it, ive already formatted my data into a vector of vectors of float values so my data is essentially gonna have a float value to represent seconds and the other values representing the values,
now the problem is series.add() wants me to give it weird stuff like RegularTimePeriod (or the subclasses) or i dunno a whole bunch of different options, none of them, so far, which have worked for me.
I tried reading the documentation for all these classes and without avail. All I want to do is just add my data to the series, I have time formatted in float, representing seconds and floats for all my other data,
I just need to know how to format the stuff so series.add() will like it.
I've also tried to follow the same way the examples show how to add data, like creating a java Time object etc and it still wouldnt like it.
I'll post my internal frame class with all the JFreeChart stuff here, its incomplete and probably doesn't work right now.
Code: Select all
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.Time;
import java.util.Vector;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class ChartFrame extends JInternalFrame implements ActionListener{
Vector<Vector<Float>> plotData;
XYDataset dataset;
JFreeChart chart;
ChartPanel chartPanel;
DataDisplayGui parent;
TimeSeries series;
private static final long serialVersionUID = -4724656187624775258L;
public ChartFrame(DataDisplayGui parent){
super("time series");
plotData = null;
setIconifiable(true);
setResizable(true);
setSize(800,600);
setVisible(true);
setMaximizable(true);
setClosable(true);
this.parent = parent;
createMenubar(this);
createTimeSeries();
}
private void createMenubar(ActionListener listener) {
JMenuBar mainMenubar = new JMenuBar();
this.setJMenuBar(mainMenubar);
JMenu fileMenu = new JMenu("file");
mainMenubar.add(fileMenu);
JMenuItem plot = new JMenuItem("plot", KeyEvent.VK_A);
fileMenu.add(plot);
plot.addActionListener(listener);
}
private void plotData(Vector<Vector<Float>> data){
System.out.println("trying to plot data");
plotData = data;
if(plotData == null) return;
for(int i = 1; i < plotData.size(); i++){
for(int j = 0; j < plotData.get(1).size(); j++){
series.add(Second.parseSecond(plotData.get(0).get(j).toString()), plotData.get(i).get(j));
}
}
}
private void createTimeSeries(){
series = new TimeSeries("no name");
dataset = new TimeSeriesCollection(series);
chart = ChartFactory.createTimeSeriesChart(
"test",
"time",
"value",
dataset,
false,
false,
false
);
chartPanel = new ChartPanel(chart);
this.getContentPane().add(chartPanel, BorderLayout.CENTER);
chartPanel.setMouseZoomable(true, false);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource() instanceof JMenuItem) {
JMenuItem menuItem = (JMenuItem)arg0.getSource();
if(menuItem.getText().equalsIgnoreCase("plot")){
plotData(parent.getPlotData());
}
}
}
public void testFunction(){
System.out.println("test");
}
}
