2011-07-06 71 views
1

我有2個問題的JFreeChart畫弧上圖

1)我試圖使用形狀標註的XYplot劃出一道弧線。我使用XYLine註釋來繪製一條線,並且我希望弧線在線條結束處開始。我在參數上遇到了一些問題。我想讓弧的高度爲17,寬度爲44,並從圖的點(3.0,17)開始(這是線的終點)。但下面的代碼不起作用。有人可以告訴我什麼是錯誤的代碼?

Arc2D.Double arc = new Arc2D.Double(3.0, 
         16.9, 
         44.0, 
         17.04, 
         180.0, 
         180.0, 
         Arc2D.OPEN 
       ); 
plot.addAnnotation(new XYShapeAnnotation(arc, 
         new BasicStroke(2.0f), Color.white)); 
XYLineAnnotation a1 = new XYLineAnnotation(3.0, 0.0, 3.0, 
         16.9, new BasicStroke(2.0f), Color.white); 

2)如何在極座標圖上繪製類似的圖形?

感謝

回答

3
  1. 關鍵件事Arc2D是邊框。爲了使單位高的半圓H,邊界必須是2 * H單位高。

  2. AFAIK,PolarPlot不支持註釋。

enter image description here

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.geom.Arc2D; 
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.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.data.xy.XYSeriesCollection; 

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

    private static final Random r = new Random(); 
    private static final double PI = 180d; 
    private static final int X = 3; 
    private static final int Y = 0; 
    private static final int W = 44; 
    private static final int H = 17; 

    public static void main(String[] args) { 
     JFreeChart chart = ChartFactory.createXYLineChart(
      "ArcTest", "X", "Y", createDataset(), 
      PlotOrientation.VERTICAL, true, true, false); 
     XYPlot plot = chart.getXYPlot(); 
     XYLineAnnotation line = new XYLineAnnotation(
      X, Y, X, H, new BasicStroke(2f), Color.blue); 
     plot.addAnnotation(line); 
     Arc2D.Double arc = new Arc2D.Double(
      X, Y, W, 2 * H, PI, PI, Arc2D.OPEN); 
     plot.addAnnotation(new XYShapeAnnotation(arc, 
      new BasicStroke(2.0f), Color.blue)); 
     ChartFrame frame = new ChartFrame("First", 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(W, W); 
     result.addSeries(series); 
     return result; 
    } 
} 
+0

方便的是,可以使用實現['Shape']任何類(http://download.oracle.com/javase/6/docs/api/java/ awt/Shape.html)接口。 – trashgod

+0

另請參閱此[示例](http://stackoverflow.com/questions/6797012/jfreechart-series-tool-tip-above-shape-annotation/6802375#6802375)。 – trashgod