如何将MySQL时间戳转换为JFreeChart TimeSeries图X轴值?

时间:2016-06-06 09:04:14

标签: java mysql jdbc jfreechart timeserieschart

我在StackOverflow上经历了几个问题但是我仍然无法使用JfreeCharts将时间戳从mysql数据库正确转换为TimeSeries Graph。我尝试转换为毫秒或使用XYplot,但它不起作用。

import java.awt.Color;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.Month;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
//Part of my sql code
import java.sql.*;
import java.text.SimpleDateFormat;

public class Graph1 extends ApplicationFrame {
    @SuppressWarnings("deprecation")
    static TimeSeries s1 = new TimeSeries("Value", Month.class);
    public Graph1(String  title) {
        super(title);
        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);
    }

    private static JFreeChart createChart(XYDataset dataset) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
        "Sensor Data Values", // title
        "Time", // x-axis label
        "Value", // y-axis label
        dataset, // data
        true, // create legend?
        true, // generate tooltips
        false // generate URLs?
        );

        chart.setBackgroundPaint(Color.white);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        XYItemRenderer r = plot.getRenderer();
        if (r instanceof XYLineAndShapeRenderer) {
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
        renderer.setBaseShapesVisible(true);
        renderer.setBaseShapesFilled(true);
        }
        DateAxis axis = (DateAxis)plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy HH-mm-ss"));
        return chart;
    }

    private static XYDataset createDataset() 
    { 
    Connection conn = null;
    Statement stmt = null;
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/agriculture","root","root");
        stmt = conn.createStatement();
        String sql;
        sql = "select date_time,data_value from sensor_data order by date_time desc limit 10";
        ResultSet rs = stmt.executeQuery(sql);

        while(rs.next())
        {
            Timestamp now  = rs.getTimestamp("date_time");
            double value = rs.getInt("data_value");
            //SimpleDateFormat df = new SimpleDateFormat("YYYY.MM.dd HH:mm:ss");
            //String s = df.format(now);
            //System.out.print("Time: " + s);
            //System.out.print(", Value: " + value + "\n");
            s1.add(new Millisecond(now), value);
        }
        rs.close();
        stmt.close();
        conn.close();
        }
        catch(SQLException se)
        {
            se.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(stmt!=null)
                stmt.close();
            }
            catch(SQLException se2)
            {
            }
            try
            {
                if(conn!=null)
                    conn.close();
            }
            catch(SQLException se)
            {
            se.printStackTrace();
            }//end finally try
        }//end try
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(s1);
    //dataset.setDomainIsPointsInTime(true);
    return dataset;
    }

    public static JPanel createDemoPanel() 
    {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }

    public static void main(String[] args) {
    Graph1 demo = new Graph1("Time Series Demo 1");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);
    }
}

数据库截图

Database Screenshot

图表截图

Graph Screenshot

1 个答案:

答案 0 :(得分:1)

问题在于您使用了已弃用的TimeSeries构造函数。您已告知它接受Month.class类型的值,但您正在添加Millisecond.class类型的值。

我不确定MySQL驱动程序使用datetime检索的getTimestamp()值的作用,但下面的完整示例有效。

graph

import java.awt.Dimension;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Graph extends JFrame {

    public Graph(String title) {
        super(title);
        JFreeChart chart = createChart(createDataset());
        ChartPanel chartPanel = new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(650, 400);
            }
        };
        chartPanel.setMouseZoomable(true, false);
        add(chartPanel);
    }

    private JFreeChart createChart(XYDataset dataset) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Sensor Data Values", "Time", "Value", dataset, true, true, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        XYItemRenderer r = plot.getRenderer();
        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            renderer.setBaseShapesVisible(true);
            renderer.setBaseShapesFilled(true);
        }
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy HH:mm"));
        return chart;
    }

    private static XYDataset createDataset() {
        TimeSeries series = new TimeSeries("Value");
        try {
            Connection conn = DriverManager.getConnection("jdbc:h2:mem:test", "", "");
            Statement st = conn.createStatement();
            st.execute("create table sensor(when datetime, value integer)");
            PreparedStatement ps = conn.prepareStatement(
                "insert into sensor values (?, ?)");
            Calendar c = Calendar.getInstance();
            for (int i = 0; i < 10; i++) {
                ps.setTimestamp(1, new Timestamp(c.getTimeInMillis()));
                ps.setInt(2, i * i + 1);
                ps.execute();
                c.add(Calendar.HOUR_OF_DAY, 1);
            }
            st = conn.createStatement();
            String sql;
            sql = "select when, value from sensor order by when desc";
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                Timestamp time = rs.getTimestamp("when");
                int value = rs.getInt("value");
                series.add(new Millisecond(time), value);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
        TimeSeriesCollection dataset = new TimeSeriesCollection(series);
        return dataset;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Graph demo = new Graph("Time Series Demo 1");
                demo.setDefaultCloseOperation(EXIT_ON_CLOSE);
                demo.pack();
                demo.setLocationRelativeTo(null);
                demo.setVisible(true);
            }
        });
    }
}