为什么时间序列收集图中的数据显示不正确? [英] Why is the data in a timeseriescollection chart don't appear correctly?

查看:244
本文介绍了为什么时间序列收集图中的数据显示不正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被设为图表时间序列集合类型.我正在将以下值提取到数据库中.

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.

非常感谢大家.

数据库中的值.

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

图表

如您所见,仅显示日期.显示的时间未在数据库中注册.谁能帮我吗?

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

图表代码

JFreeChart createChart(XYDataset数据集){

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?

非常感谢大家.

推荐答案

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

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

在视图中,您可以setDateFormatOverride()到域轴上的所需格式,如

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"));

这篇关于为什么时间序列收集图中的数据显示不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆