
Good evening everybody
So i'm trying to make a XYLine chart with JFreechart. The data comes from 2 arraylist, so a for-loop should do the trick parsing the values into the dataset. The horizontal axis are going to be represented by a string (showing day and time) and the vertical indicates temperature (double)
Now my question is what do i do now, i've searched for several examples, i've tried different kind of dataset (different kind of XY collections) and I can't get any thing to work!
Does anybody know a way to make this work?
Frank

import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
public class XYJfreechart {
public static void main(String args []){
TimeSeries series = new TimeSeries( "", Day.class );
series.add(new Day(new Date("2012/03/01")), 50);
series.add(new Day(new Date("2012/03/02")), 40);
series.add(new Day(new Date("2012/03/03")), 30);
series.add(new Day(new Date("2012/03/04")), 35);
series.add(new Day(new Date("2012/03/05")), 20);
series.add(new Day(new Date("2012/03/06")), 25);
series.add(new Day(new Date("2012/03/07")), 32);
TimeSeriesCollection dataset=new TimeSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart
("User Participation Chart",
"Day",
"Temperature",
dataset,
true,
true,
false
);
try {
ChartUtilities.saveChartAsJPEG(new File("c:/chart.jpg"), chart, 800, 600);
System.out.println("Chart is created successfully.");
} catch (IOException e) {
e.printStackTrace();
System.err.println("Problem occurred creating chart.");
}
}
}

Okay, I tried to make it work, but it just leaves me with a bunch of error messages. The JPanel looks like this:
print("private JPanel createGraphArea() throws ClassNotFoundException, FileNotFoundException, IOException, ParseException {
JPanel graph = new JPanel();
graph.setLayout(new java.awt.BorderLayout());
ArrayList<Double> graphtemperature = new ArrayList<Double>();
ArrayList<Date> clockset = new ArrayList<Date>();
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 0; i < data.size(); i++) {
String sp = data.get(i);
String[] tokens = sp.split("\t");
if (tokens.length == 2) {
Date date = dt.parse(tokens[0]);
clockset.add(date);
graphtemperature.add(Double.parseDouble(tokens[1]));
}
}
TimeSeries series = new TimeSeries("",Day.class);
for(int i = 0; i < data.size(); i++){
series.add(new Day(clockset.get(i)), graphtemperature.get(i));
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart(null,
"Time",
"Temperature",
dataset,
true,
true,
false);
ChartPanel CP = new ChartPanel(chart);
graph.add(CP, BorderLayout.CENTER);
graph.validate();*/
return graph;
}");
And the error messages:
print("Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/date/MonthConstants
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Caused by: java.lang.ClassNotFoundException: org.jfree.date.MonthConstants
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 12 more
Java Result: 1");
Somehow I think the error is at Day.class?
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.