为什么timeseriescollection图表中的数据显示不正确?

时间:2014-07-28 11:55:43

标签: java netbeans jfreechart timeserieschart

我做了一个图表时间表收集类型。我将以下值提取到数据库中。

但是,在创建图表时,显示的值并不完全准确。有人能告诉我为什么会这样吗?

仅显示日期。小时不正确。有人能帮我解决这个小问题吗?我会永远感激。

非常感谢你。

数据库中的值。

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

图表

enter image description here

如您所见,只显示日期。出现的时间未在数据库中注册。任何人都可以帮助我吗?

图表代码

JFreeChart createChart(XYDataset数据集){

    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;
}

}

如您所见,只显示日期。出现的时间未在数据库中注册。任何人都可以帮助我吗?

非常感谢大家。

1 个答案:

答案 0 :(得分:1)

您将dateI截断到最接近的小时。而是使用Second来保留从查询中检索到的完整分辨率,并将结果存储在模型中。

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

在视图中,您可以setDateFormatOverride()在域轴上获得所需的格式,如图here所示。

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