I'm trying to make a chart in my application that returns me the temperature of days during the months.
This chart is a JFreechart TimeSeriesCollection, and I am unable to have the graph read correct data from the database.
It shows some of the values, but not all of them, and does not show the correct time.
To fix this, I tried to implement the graph as posted here, but still could not solve my problem, even having gone to see this question, as people suggested
public class NewClass extends ApplicationFrame {
Connection conexao = null;
PreparedStatement pst= null;
ResultSet rs = null;
public NewClass(String title) throws SQLException, ParseException {
super(title);
ChartPanel chartPanel = (ChartPanel) createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Temperatura/Date", // title
"Date", // x-axis label
"Temperatura", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
return chart;
}
private static XYDataset createDataset() throws SQLException, ParseException {
Connection con = null;
String databaseURL = "jdbc:mysql://localhost:3306/world";
String driverName = "com.mysql.jdbc.Driver";
String user = "root";
String password = "rootadmin";
try {
Class.forName(driverName).newInstance();
} catch (Exception ex) {
System.out.println("");
}
con = (Connection) DriverManager.getConnection(databaseURL, user, password);
if (!con.isClosed()) {
System.out.println("Successfully connected to the DataBase Server...");
}
Statement statement;
statement = (Statement) con.createStatement();
String selectQuery = "select (CONCAT(`data_registo`, ' ', hora_registo)) as data, temperatura, idSensor from registos where idSensor like 'BrgTH001' ";
ResultSet resultSet = null;
resultSet = statement.executeQuery(selectQuery);
TimeSeries s1 = new TimeSeries("Thermomether01");
while (resultSet.next()) {
String idSensor = (String) resultSet.getObject("idSensor");
String data = (String) resultSet.getObject("data");
String temperatura = (String) resultSet.getObject("temperatura");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date dateI = sdf2.parse(data);
System.out.println("" + idSensor + " " + data + " " + temperatura+ " | " );
s1.addOrUpdate(new Hour(dateI), value);
}
resultSet.close();
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
public static JPanel createDemoPanel() throws SQLException, ParseException {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setFillZoomRectangle(true);
panel.setMouseWheelEnabled(true);
return panel;
}
public static void main(String[] args) throws SQLException, ParseException {
NewClass demo = new NewClass("Chart");
demo.pack();
demo.setVisible(true);
}
}
This is the chart that results of my code. But the result of query is that:
Thermomether01 2014-04-01 08:47:11 23.8 |
Thermomether01 2014-04-01 08:47:11 23.8 |
Thermomether01 2014-04-01 08:51:20 23.9 |
Thermomether01 2014-04-01 08:53:24 23.9 |
Thermomether01 2014-04-01 08:55:28 23.9 |
Thermomether01 2014-04-01 09:43:26 24.1 |
Thermomether01 2014-04-01 09:48:39 24.0 |
Thermomether01 2014-04-01 09:50:44 24.1 |
Thermomether01 2014-04-01 09:52:48 24.0 |
Thermomether01 2014-04-01 09:54:52 24.1 |
Thermomether01 2014-04-01 09:56:56 24.1 |
Thermomether01 2014-04-01 09:59:01 24.1 |
.
.
.
Thermomether01 2014-06-13 09:35:36 19.2 |
Thermomether01 2014-06-13 10:03:00 18.7 |
Thermomether01 2014-06-13 10:33:41 19.0 |
Thermomether01 2014-06-16 08:57:57 19.1 |
Thermomether01 2014-06-16 09:07:54 18.9 |
Thermomether01 2014-06-16 09:08:40 19.0 |
Thermomether01 2014-06-16 09:36:28 19.1 |
Thermomether01 2014-06-16 10:03:51 18.8 |
Thermomether01 2014-06-16 10:31:14 19.2 |
Thermomether01 2014-06-16 11:00:17 19.1 |
Thermomether01 2014-06-16 11:27:38 19.2 |
Thermomether01 2014-06-16 11:54:59 19.1 |
Thermomether01 2014-06-16 12:51:24 18.5 |
ETC...
As shown here, it doesn't show all values and doesn't list hours.
----------------------EDIT----------------------
select (CONCAT(`data_registo`, ' ', hora_registo)) as data, temperatura, idSensor from registos where idSensor like 'Thermometer01' and temperatura not in ('---') and data_registo between '2014-06-01' and '2014-06-10'
Implementing this did not change the output.