2011-07-22 55 views
4

我有一個XYPlot的系列和幾個動態添加形狀註釋沒有填充(因此每個系列點都可見)。是否可以在註釋上顯示系列工具提示(顯示鼠標指針當前指向的系列點的座標)?或者,如何重新排列元素以使工具提示可見。JFreechart系列工具提示上面的形狀註釋

+0

你的意思是一個_LABEL_,看到[這裏](http://sites.google.com/site/drjohnbmatthews/jfreechartdemo)? – trashgod

+0

我有一個情節,我添加一個圓作爲形狀註釋。但是,在我的系列文章中加入這些內容(儘管沒有填充),因此使工具提示無法工作。如果可能的話,我希望能夠有註釋並仍然有我的工具提示 – jpo

回答

9

我懷疑你是將形狀註釋添加到繪圖最後繪製的地方。請將它們添加到Layer.BACKGROUND中的渲染器中。如下所示,在(20,20)處,圓圈不會而是會遮擋工具尖端。另請注意,(10,10)是不是受線路註釋影響,而(30,30)被弧線遮蔽。

image

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.geom.Arc2D; 
import java.awt.geom.Ellipse2D; 
import java.util.Random; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartFrame; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.annotations.XYLineAnnotation; 
import org.jfree.chart.annotations.XYShapeAnnotation; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.data.xy.XYSeriesCollection; 
import org.jfree.ui.Layer; 

/** 
* @see http://stackoverflow.com/questions/6797012 
* @see http://stackoverflow.com/questions/6604211 
*/ 
public class ArcTest { 

    private static final Random r = new Random(); 
    private static final Color blue = Color.blue; 
    private static final BasicStroke stroke = new BasicStroke(2.0f); 
    private static final double PI = 180d; 
    private static final int X = 8; 
    private static final int Y = 0; 
    private static final int W = 6 * X; 
    private static final int H = 3 * X; 

    public static void main(String[] args) { 
     JFreeChart chart = ChartFactory.createXYLineChart(
      "ArcTest", "X", "Y", createDataset(), 
      PlotOrientation.VERTICAL, true, true, false); 
     XYPlot plot = chart.getXYPlot(); 

     XYLineAndShapeRenderer renderer = 
      (XYLineAndShapeRenderer) plot.getRenderer(); 
     renderer.setBaseShapesVisible(true); 
     Ellipse2D.Double circle = new Ellipse2D.Double(X, X, 20, 20); 
     renderer.addAnnotation(new XYShapeAnnotation(
      circle, stroke, blue), Layer.BACKGROUND); 

     XYLineAnnotation line = new XYLineAnnotation(X, Y, X, H, stroke, blue); 
     plot.addAnnotation(line); 
     Arc2D.Double arc = new Arc2D.Double(X, Y, W, 2 * H, PI, PI, Arc2D.OPEN); 
     plot.addAnnotation(new XYShapeAnnotation(arc, stroke, blue)); 

     ChartFrame frame = new ChartFrame("Test", chart); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private static XYDataset createDataset() { 
     XYSeriesCollection result = new XYSeriesCollection(); 
     XYSeries series = new XYSeries("ArcTest"); 
     series.add(0, 0); 
     series.add(10, 10); 
     series.add(20, 20); 
     series.add(30, 30); 
     series.add(W, W); 
     result.addSeries(series); 
     return result; 
    } 
} 
+0

感謝您隨時附上一些示例代碼的答覆 – jpo

+0

另請參見此[示例](http://stackoverflow.com/questions/6604211/jfreechart -draw弧上圖表/ 6620017#6620017)。 – trashgod