Why is the data in a timeseriescollection chart do

2019-08-04 05:27发布

问题:

I'm made ​​a chart timeseriescollection type. I'm fetching the following values ​​to the database.

However when creating the graph, the values ​​shown are not entirely accurate. Can someone tell me why this happens?

Only shows the date. Hours not appear correctly. Does anyone can help me solve this little problem please? I would be eternally grateful.

Thank you all very much.

Values from Database.

select (CONCAT(data_registo, ' ', hora_registo)) as data,  temperatura from registos where idSensor like 'Thermomether001' and data_registo between '2014-07-20' and '2014-07-24'

2014-07-20 00:26:03 19.4
2014-07-20 00:55:07 18.4
2014-07-20 01:58:14 18.4
2014-07-20 03:03:02 18.4
2014-07-20 04:40:13 19.3
2014-07-20 05:10:56 18.4
2014-07-20 05:41:40 19.3
. 
.
.
2014-07-24 21:40:04 19.3
2014-07-24 22:09:42 19.2
2014-07-24 22:39:20 18.9
2014-07-24 23:02:19 19.8
2014-07-24 23:38:37 19.7

Chart

As you can see only shows the date. The time that appears is not registered in the database. Can anyone help me?

Code of chart

JFreeChart createChart(XYDataset dataset) {

    JFreeChart chart = ChartFactory.createTimeSeriesChart(
        "Chart",  // title
        "Date",             // x-axis label
        "Temperature",     // 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 HH:mm:ss"));

    return chart;

}


 XYDataset createDataset() throws SQLException, ParseException {
    Connection con = null;
    String databaseURL = "jdbc:sqlserver://-----;IntegratedSecurity=true";
    String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    try {
        Class.forName(driverName).newInstance();
    } catch (Exception ex) {
        System.out.println("");
    }

        con = (Connection) DriverManager.getConnection(databaseURL);

        if (!con.isClosed()) {
            System.out.println("Successfully connected to the DataBase Server...");
        }

       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d1 = sdf.format(jDateChooser1.getDate());
        String d2 = sdf.format(jDateChooser2.getDate());
        String c1 = jComboBoxSensores.getSelectedItem().toString();

        Statement statement;
        statement = (Statement) con.createStatement();
        String selectQuery ="select (CONCAT(data_registo, ' ', hora_registo)) as data,  temperatura from registos where idSensor like '"+c1+"' and temperatura not in ('0.0') and data_registo between '"+d1+"' and '"+d2+"'";
        ResultSet resultSet = null;
        resultSet = statement.executeQuery(selectQuery);

    TimeSeries s1 = new TimeSeries(c1);
    while (resultSet.next()) {

                String data = (String) resultSet.getObject("data");
                String temperatura = (String) resultSet.getObject("temperatura");

                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date dateI = sdf2.parse(data);

                double value = Double.parseDouble(temperatura);



               s1.addOrUpdate(new Hour(dateI), value);               

            } 

           resultSet.close();


    TimeSeriesCollection dataset = new TimeSeriesCollection();

    dataset.addSeries(s1);

       return dataset;


 }

 JPanel createDemoPanel() throws SQLException, ParseException {
    JFreeChart chart = createChart(createDataset());
    ChartPanel panel = new ChartPanel(chart);
    panel.setFillZoomRectangle(true);
    panel.setMouseWheelEnabled(true);
    return panel;
}

}

As you can see only shows the date. The time that appears is not registered in the database. Can anyone help me?

Very thanks everyone.

回答1:

You're truncating dateI to the nearest hour. Instead, use Second to preserve the full resolution retrieved from the query and store the result in the model.

s1.addOrUpdate(new Second(dateI), value);

In the view, you can setDateFormatOverride() to the desired format on the domain axis, as shown here.

axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd HH"));