簡體   English   中英

Jfreechart無法在X軸上設置時間

[英]Jfreechart unable to set time in X axes

我正在嘗試制作一個圖表,該圖表從串行端口獲取數據並在y軸上繪制它們,而我希望在x軸上顯示當前時間。我認為我正確設置了代碼,因為我現在設法將其作為XY圖表運行時間序列圖我唯一的問題是方法series.add(TIME,SERIALDATA); 我不知道如何初始化TIME,我知道我想要一個對象RegularTimePeriod,但我不知道該怎么做。

這是代碼。.我知道僅缺少一些行,請幫助我找到它們...

void initialize(){

    frame = new JFrame();
    frame.setBounds(100, 100, 817, 525);


    final TimeSeries series = new TimeSeries("Charts");
    final SerialDataReceived serialdataprint = new SerialDataReceived();


    final TimeSeriesCollection data = new TimeSeriesCollection(series);
    final JFreeChart chart = ChartFactory.createXYLineChart(
            "Tmperature IN",
            "Time", 
            "C", 
            data,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );




    final ChartPanel chartPanel = new ChartPanel(chart);
     chartPanel.setBounds(10, 11, 477, 224);
          chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
           chartPanel.setVisible(true);
           frame.getContentPane().setLayout(null);
         frame.getContentPane().add(chartPanel);
      chartPanel.setLayout(null);


     Thread outtempthread=new Thread() {  //THREAD THAT RUNS ALL THE TIME

        public void run() {

         try {
             while (true){
         Thread.sleep(2000);


     double intemp = serialdataprint.getintemp();  //THIS WHERE I TAKE MY SERIAL DATA
      series.add(I WANT TO  DISPLAY HERE LETS SAY 13:23:15, intemp);  //HERE IS MY PROBLEM



             }}
             catch (InterruptedException ie) {}
             }
             };

             outtempthread.start();

}

我只使用過以天為單位的TimeSeries ,所以我使用了org.jfree.data.time.Day類。

這是所有不同時間類的jfreechart javadoc: http : //www.jfree.org/jfreechart/api/javadoc/org/jfree/data/time/package-summary.html

嘗試一些,看看什么是適合您的。

由於您似乎只需要一天的小時,分​​鍾,因此您可以使用Second Class。

這是您以這種方式制作TimeSeries方法:

int todaysDay =...
int todaysMonth =...
int todaysYear =...

TimeSeries series = new TimeSeries(name, Second.class);

    //this should mark 'inTemp' as 13:23:15
    series.add(new Second(15,23,13,todaysDay, todaysMonth, todaysYear), 
               inTemp);

好!! 終於我找到了解決方案! 我不知道這是不是正確的方法,但是它能正常工作,現在每次我的串行端口更新在我的圖表中都是實時的,這里是修復代碼!

String timeStamp1 = new SimpleDateFormat("mm").format(Calendar.getInstance().getTime());
         int minute = Integer.parseInt(timeStamp1);


double intemp = serialdataprint.getintemp();    
     series.addOrUpdate(new Minute(minute,hour), intemp);

幾個指針:

  1. ChartFactory.createXYLineChart()方法將創建一個折線圖,其中X軸和Y軸均為數字。 嘗試使用createTimeSeriesChart()獲取一個在X軸上顯示日期的圖表(或創建一個新的DateAxis()實例並調用plot.setDomainAxis()來更改X軸);
  2. 如果需要它提供的結構,TimeSeriesCollection類是一個很好的數據集,可用於時間序列數據(它強制執行規則的時間段並防止重復)。 但是,請記住,它只是XYDataset接口的實現,其中返回的x值是“自1970年1月1日以來的毫秒數”(Java中“日期”的標准編碼)。 您可以使用XYSeriesCollection(它也實現XYDataset接口)來簡化代碼,並在輸入新數據時調用System.currentTimeInMillis()以獲取當前的x值。圖表上的日期軸將負責顯示一個此數據的日期范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM