2013-01-10 90 views
1

我想繪製一個Dirac(或Kronecker符號,如果您願意的話),我的意思是使用像這樣的垂直條(不帶標籤)在我的圖表上繪製離散值:如何使用JFreeChart繪製一個Dirac

Good example 我可以用BarChart製作它,但它不是很好看......有沒有一種很好的方法來做到這一點?

編輯:我用你的方法和我進行如下設置:

enter image description here

但正如你所看到的也許是顏色漸變的一些酒吧有不同的顏色,?你知道如何讓酒吧全紅嗎?

下面是代碼:

public class XYDiscreteBarChart { 

    private final Color CHART_BACKGROUND_COLOR = new JPanel().getBackground(); 
    private final Color CHART_FOREGROUND_COLOR = new JLabel().getForeground(); 
    private final Color CROSSHAIR_COLOR = Color.BLUE; 
    private final String title; // Titre du graphique 
    private final String xTitle; // Titre de l'axe des abscisses 
    private final String yTitle; // Titre de l'axe des ordonnées 
    private final XYDataset dataset; // Les données affichées dans le graphique 
    private final boolean isLegendVisible; // Affichage de la légende 
    private final boolean isTooltipsVisible; // Affichge des tooltips 
    private final boolean isUrlVisible; // Affichage des urls 
    private final boolean isGridXVisible; // Affichage de la grille verticale 
    private final boolean isGridYVisible; // Affichage de la grille horizontale 
    private JFreeChart chart; // Le graphique 
    private XYPlot plot; // La zone de dessin des courbes 

    public XYDiscreteBarChart(String title, String xAxisLabel, String yAxisLabel, XYDataset dataset, boolean legend, 
     boolean tooltip, boolean url) { 
     this.title = title; 
     this.xTitle = xAxisLabel; 
     this.yTitle = yAxisLabel; 
     this.isLegendVisible = legend; 
     this.isTooltipsVisible = tooltip; 
     this.isUrlVisible = url; 
     this.dataset = dataset; 
     this.isGridXVisible = true; 
     this.isGridYVisible = true; 

     createChart(); 
     setChartStyle(); 
    } 

    private void createChart() { 
     this.chart = ChartFactory.createXYBarChart(
      this.title, 
      this.xTitle, 
      false, 
      this.yTitle, 
      formatDataset(), 
      PlotOrientation.VERTICAL, 
      this.isLegendVisible, 
      this.isTooltipsVisible, 
      this.isUrlVisible); 
     this.plot = (XYPlot) this.chart.getPlot(); 
    } 

    private void setChartStyle() { 
     this.plot.setBackgroundAlpha((float) 0.0); 
     this.plot.setDomainCrosshairVisible(this.isGridXVisible); 
     this.plot.setDomainCrosshairLockedOnData(true); 
     this.plot.setRangeCrosshairVisible(this.isGridYVisible); 
     this.plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT); 
     this.plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); 

     this.chart.setBackgroundPaint(this.CHART_BACKGROUND_COLOR); 
     this.plot.getDomainAxis(0).setAxisLinePaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.getDomainAxis(0).setLabelPaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.getDomainAxis(0).setTickLabelPaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.getDomainAxis(0).setTickMarkPaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.getRangeAxis(0).setAxisLinePaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.getRangeAxis(0).setLabelPaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.getRangeAxis(0).setTickLabelPaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.getRangeAxis(0).setTickMarkPaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.setBackgroundPaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.setDomainGridlinePaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.setRangeGridlinePaint(this.CHART_FOREGROUND_COLOR); 
     this.plot.setDomainCrosshairPaint(this.CROSSHAIR_COLOR); 
     this.plot.setRangeCrosshairPaint(this.CROSSHAIR_COLOR); 

     XYBarRenderer renderer = (XYBarRenderer) this.plot.getRenderer(); 
     renderer.setBarPainter(createBarPainter()); 
     renderer.setSeriesPaint(0, Color.RED); 
    } 

    private IntervalXYDataset formatDataset() { 
     final XYSeries series = new XYSeries("Filter Coefficients"); 

     for (int i = 0; i < this.dataset.getItemCount(0); i++) { 
      series.add(this.dataset.getX(0, i), this.dataset.getY(0, i)); 
     } 

     final XYSeriesCollection dataset = new XYSeriesCollection(series); 
     return dataset; 
    } 

    private GradientXYBarPainter createBarPainter() { 
     return new GradientXYBarPainter() { 

      private static final long serialVersionUID = -1997018568242678921L; 

      @Override 
      public void paintBar(Graphics2D g2, XYBarRenderer renderer, int row, int column, 
       RectangularShape bar, RectangleEdge base) { 
       double wCoeff = 0.25; 
       double hCoeff = 1.0; 
       double newWidth, deltaW, deltaX; 

       Rectangle2D rect = bar.getFrame(); 
       newWidth = rect.getWidth() * wCoeff; 
       deltaW = rect.getWidth() - newWidth; 
       deltaX = deltaW/2; 
       rect.setRect(rect.getX() + deltaX, rect.getY(), newWidth, rect.getHeight() * hCoeff); 
       bar.setFrame(rect); 
       super.paintBar(g2, renderer, row, column, bar, base); 
      } 
     }; 
    } 
} 

回答

1

假設你指的箭頭,XYBarRenderer似乎是一個可能的出發點。您可以將其XYBarPainter替換爲自定義實施,適用於example

+0

好的,謝謝你,它工作:)但你認爲有可能在箭頭的頂部畫一個圓或三角形? – AwaX

+0

我編輯了我的問題,向您展示了另一個問題 – AwaX

+0

我想要擴展'RectangularShape'或使用['XYShapeAnnotation'](http://stackoverflow.com/a/11751239/230513) – trashgod