2013-08-20 15 views
1

我試圖在DynamicTimeSeriesCollection中實現XYTextAnnotation。 我不知道如何在DynamicTimeSeriesCollection中找到系列的X值。我的代碼到目前爲止:DynamicTimeSeriesCollection中的XYTextAnnotation

DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(1, 60, new Minute()); 
final JFreeChart result = ChartFactory.createTimeSeriesChart(TITLE, "A", "B", dataset, true, true, false); 
float[] series1Small = new float[10]; 
dataset.setTimeBase(new Minute(1, 1, 1, 1, 2013)); 
dataset.addSeries(series1Small,0,"1"); 
JFreeChart result = ChartFactory.createTimeSeriesChart(TITLE, "Время", "Платежи", dataset, true, true, false); 
final XYPlot plot = result.getXYPlot(); 
-----------------------------------------------------------Below line doesn't work. 

TimeSeriesDataItem item1 = series1.getDataItem(series1.getItemCount() - 1); 
createAnnotation(item1,plot); 

這是一個函數,用於使用TimeSeriesCollection進行註釋。

public static void createAnnotation(TimeSeriesDataItem item,XYPlot plot) 
{ 
    double xAnnotation = item.getPeriod().getFirstMillisecond(); 
    double yAnnotation = item.getValue().doubleValue(); 
    XYTextAnnotation annotation = new XYTextAnnotation(item.getValue().toString(), xAnnotation, yAnnotation); 
    annotation.setFont(new Font("Arial",Font.BOLD,11)); 
    plot.addAnnotation(annotation); 
} 
+0

什麼沒有它究竟是工作? 'series1'變量在哪裏? – Alex

回答

4

啓動形式是example,我添加了以下行createChart()獲得下面的圖片:

double x = dataset.getXValue(0, COUNT - 1); 
double y = dataset.getYValue(0, COUNT - 1); 
String s = dataset.getY(0, COUNT - 1).toString(); 
XYTextAnnotation a = new XYTextAnnotation(s, x, y); 
a.setFont(a.getFont().deriveFont(24f)); 
plot.addAnnotation(a); 

text annotation image

相關問題