Hello everybody
I'm trying to make a line chart from JFreechart, but I just can't get it to work. The graph get its data from at 2-columun database (String, double). (The string is a date, represented as a String). The X-axis should be the different string, and the Y-axis should be the double values. I tried DefaultCategoryDataset, but every time I run it, an error occurs (i tried an example from here, with XY, and it didn't work either)
I could really use some help on this, as i'm completely lost. (It has been checked that the database contains the data)
public class Chart {
Connection conn;
Statement stmt;
ResultSet rs;
Chart(){
try{
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:projectdb.db");
System.out.println("Database connection established");
String dateinput;
double temp;
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM data");
while(rs.next()){
dateinput = rs.getString(1);
temp = rs.getDouble(2);
dataset.addValue(temp, "", dateinput);
}
JFreeChart chart = ChartFactory.createLineChart(null,
"Time",
"Temperature",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false);
JFrame test = new JFrame();
test.setVisible(true);
ChartPanel chpanel = new ChartPanel(chart);
chpanel.setPreferredSize(new Dimension(785, 440));
chpanel.setMouseWheelEnabled(true);
JPanel panel = new JPanel();
panel.add(chpanel);
test.add(panel);
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args){
new Chart();
}
}