2

我已經創建了一個圖表,像這樣:如何在的JFreeChart的TimeSeries的Y值增加一個簡單的水平線

enter image description here

用於添加和/或更新信息

主代碼:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:mm:ss"); 
Date date = simpleDateFormat.parse(dateAsStringToParse); 
Second second = new Second(date); 
myInfo.getSeries().addOrUpdate(second, maxValue); // maxValue is an Integer 

併爲創建實際圖表:

final XYDataset dataset = new TimeSeriesCollection(myInfo.getSeries()); 
JFreeChart timechart = ChartFactory.createTimeSeriesChart(myInfo.getName() 
    + " HPS", "", "HPS", dataset, false, false, false); 

我想簡單地添加一個n水平線(平行於X(時間)軸)爲一個常數值,假設爲10,000。所以圖形看起來就像這樣:

enter image description here

什麼是最簡單的(最正確)的方式與我的代碼來實現這一目標?

+0

也許'XYLineAnnotation',對於[示例](https://stackoverflow.com/search?tab=votes&q=%5bjfreechart%5d%20XYLineAnnotation),用重' Stroke'? – trashgod

+0

@trashgod這是一個奇妙的建議,但是當我嘗試'timechart.getXYPlot()。addAnnotation(new XYLineAnnotation(0,1.5,100000,1.5));'應該是'x1,y1,x2,y2'值儘管要像在我的照片中那樣穿過圖表? – Idos

回答

2

看起來你想要一個XYLineAnnotation,但是TimeSeries的座標可能會很麻煩。從TimeSeriesChartDemo1開始,我做了以下更改以顯示圖表。

  1. 首先,我們需要在TimeSeries第一個和最後一個RegularTimePeriodx值。

    long x1, x2; 
    … 
    x1 = s1.getTimePeriod(0).getFirstMillisecond(); 
    x2 = s1.getNextTimePeriod().getLastMillisecond(); 
    
  2. 然後,恆定y值是容易的;我選擇140.

    double y = 140; 
    

    或者,你可以從你的TimeSeries得出一個值,例如。

    double y = s1.getMinY() + ((s1.getMaxY() - s1.getMinY())/2); 
    
  3. 最後,我們構造註釋並將其添加到圖中。

    XYLineAnnotation line = new XYLineAnnotation(
        x1, y, x2, y, new BasicStroke(2.0f), Color.black); 
    plot.addAnnotation(line); 
    

image

+0

一如既往的鼓舞,非常感謝! – Idos

相關問題