如何在的JFreeChart的TimeSeries的Y值增加一个简单的水平线

问题描述:

我已经创建了一个图表,像这样:如何在的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

看起来你想要一个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