2014-02-25 80 views
1

末,我有以下情節:的JFreeChart:如何把箭頭的軸線在散點圖

Scatter plot

如何把箭頭領導線和性能行的結束?

這裏是一個演示代碼:

import java.awt.BasicStroke; 
import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GradientPaint; 
import java.awt.Paint; 
import java.awt.Shape; 
import java.awt.geom.Ellipse2D; 
import java.util.*; 
import javax.swing.JFrame; 
import org.jfree.chart.*; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.renderer.xy.XYItemRenderer; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.data.xy.XYSeriesCollection; 

public class Test extends JFrame { 

    private static final int N = 3; 
    private static final int SIZE = 345; 
    private static final String title = "Scatter Plot Demo"; 
    private final XYSeries series = new XYSeries("0"); 

    protected Map<String, Color> colors = new HashMap<String, Color>(); 
    protected Map<Integer, Shape> shapes = new HashMap<Integer, Shape>(); 
    private Color bckColor1 = Color.decode("#4282CE"); 
    private Color bckColor2 = Color.decode("#9BC1FF"); 
    public static final Shape BASE_SHAPE = new Ellipse2D.Float(0, 0, 12, 12); 

    public Test(String s) { 
     super(s); 
     colores.put("0", Color.decode("#FFAC59")); //Orange 
     colores.put("1", Color.decode("#D6FC93"));//Clear green 
     colores.put("2", Color.decode("#C0E975"));//Dark green 

     for(int i = 0; i < 3; i++) 
      shapes.put(i, BASE_SHAPE); 

     final ChartPanel chartPanel = createDemoPanel(); 
     chartPanel.setPreferredSize(new Dimension(SIZE, SIZE)); 
     this.add(chartPanel, BorderLayout.CENTER); 
    } 

    protected void processPlot(XYPlot plot) { 
     Paint p = new GradientPaint(0,0,bckColor1,0,0,bckColor2); 
     Color axisColor = Color.decode("#DD0010"); //Red 

     NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 
     rangeAxis.setTickLabelsVisible(false); 
     rangeAxis.setMinorTickMarksVisible(false); 
     rangeAxis.setTickMarksVisible(false); 
     rangeAxis.setAxisLinePaint(axisColor); 
     rangeAxis.setAxisLineStroke(new BasicStroke(2)); 

     NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(); 
     domainAxis.setTickLabelsVisible(false); 
     domainAxis.setMinorTickMarksVisible(false); 
     domainAxis.setTickMarksVisible(false); 
     domainAxis.setAxisLinePaint(axisColor); 
     domainAxis.setAxisLineStroke(new BasicStroke(2)); 

     plot.setBackgroundPaint(p); 
     plot.setDomainGridlinesVisible(false); 
     plot.setRangeGridlinesVisible(false); 
    } 

    private ChartPanel createDemoPanel() { 
     JFreeChart jfreechart = ChartFactory.createScatterPlot(
      title, "Performance", "Leadership", createSampleData(), 
      PlotOrientation.VERTICAL, true, true, false); 

     XYPlot plot = (XYPlot) jfreechart.getPlot(); 
     XYItemRenderer renderer = (XYItemRenderer) plot.getRenderer(); 
     renderer.setBaseShape(BASE_SHAPE); 

     processPlot(plot); 
     XYDataset cd = (XYDataset)plot.getDataset(); 

     if (cd != null) { 
      int rc = cd.getSeriesCount(); 
      for (int i = 0; i < rc; i++) { 
       String key = (String) cd.getSeriesKey(i); 

       Color color = colors.get(key); 
       Paint p = new GradientPaint(0, 0, color.brighter() 
         , 0, 0, color.darker()); 

       renderer.setSeriesPaint(i, p); 
       renderer.setSeriesOutlinePaint(i, color); 
       renderer.setSeriesShape(i, BASE_SHAPE); 
      } 
     } 

     return new ChartPanel(jfreechart); 
    } 

    private XYDataset createSampleData() { 
     XYSeriesCollection xySeriesCollection = new XYSeriesCollection(); 

     for (int i = 0; i < N; i++) { 
      series.add(randomDouble(0D, 100D) 
        , randomDouble(0D, 100D)); 
     } 
     xySeriesCollection.addSeries(series); 
     return xySeriesCollection; 
    } 

    private double randomDouble(double min, double max) { 
     Random r = new Random(); 
     double randomValue = min + (max - min) * r.nextDouble(); 
     return randomValue; 
    } 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       Test demo = new Test(title); 
       demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       demo.pack(); 
       demo.setLocationRelativeTo(null); 
       demo.setVisible(true); 
      } 
     }); 
    } 

我發現類XYPointerAnnotation在http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/annotations/XYPointerAnnotation.html,但它僅積裏面,而不是軸線。

謝謝。

回答

1

將一個RIGHTWARDS ARROW (U+2192) →添加到軸標籤。

JFreeChart jfreechart = ChartFactory.createScatterPlot(
    title, "Performance →", "Leadership →", createSampleData(), 
    PlotOrientation.VERTICAL, true, true, false); 

image

+0

嗯...似乎是一種巧妙的方式來放置箭頭,雖然不在軸線上。 – dovahkiin

+0

我可以重寫'drawAxisLine()',但我沒有嘗試過。 – trashgod

1

我沒有在Java jfree工作;我通過Incanter在Clojure中使用它。毫無疑問,你已經從這個問題轉向了dohvakiin,但是由於2.5年後沒有人回答,所以我會提出一個部分未經測試的解決方案。我還沒有在純Java試過,但它是一個翻譯成什麼樣,我用Clojure與jfree做的Java:

plot.getDomainAxis().setPositiveArrowVisible(true); 
plot.getDomainAxis().setPositiveArrowVisible(true); 

這裏plot是在你的代碼中定義的XYPlot對象。這將在右側和橫軸上以及垂直軸的頂端放置一個箭頭。如果你想在另一端使用箭頭,還有一個setNegativeArrowVisible方法。

在你的榜樣,

rangeAxis.setPositiveArrowVisible(true); 
domainAxis.setPositiveArrowVisible(true); 

image

的箭頭是相當小的,但是,取代它似乎需要編碼了一個新的Shape對象。

相關問題