2013-04-30 67 views
0

在這些附件中顯示的圖像旁邊是否有添加圖像的方法?JFreeChart類別圖像

電流:

current

期望:

desired

編輯:

基於kiwiwings的回答,我很容易地管理,以顯示我的圖像旁XYchart在他的演示中的標籤字符串:

protected AxisState drawTickMarksAndLabels(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, 
      RectangleEdge edge) { 

     if (isTickLabelsVisible()) { 
      Shape s = xyRend.getBaseShape(); 
      Paint oldPaint = g2.getPaint(); 
      g2.setPaint(xyRend.getSeriesPaint(0)); 
      Rectangle2D rect = s.getBounds2D(); 
      AffineTransform oldTrans = g2.getTransform(); 

      AxisState state = new AxisState(cursor); 
      @SuppressWarnings("unchecked") 
      List<ValueTick> ticks = (List<ValueTick>) refreshTicks(g2, state, dataArea, edge); 
      for (ValueTick tick : ticks) { 
       float[] anchorPoint = calculateAnchorPoint(tick, cursor, dataArea, edge); 
       g2.setTransform(oldTrans); 
       // TODO: replace "anchorPoint[0]-X" with a bounds based value 
       g2.translate(anchorPoint[0] - 28, anchorPoint[1] + 2); 
       BufferedImage bfrImg = * load img here *; 
       g2.drawImage(bfrImg, 0, 0, null); 
      } 

      g2.setPaint(oldPaint); 
      g2.setTransform(oldTrans); 
     } 

     return super.drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge); 
    } 

產地:

enter image description here

現在我試圖做我的JFreeChart使用CategoryPlot的CategoryAxis,並使用創建相同:

ChartFactory.createBarChart("", "", "Items", mDataset, PlotOrientation.VERTICAL, true, true, false); 
+0

我已經更新了我的版本...也許這將是更好的地方旗下 – kiwiwings 2013-05-02 23:06:56

+0

非常感謝!由於可變數量的類別及其可變字符數量,我嘗試顯示圖像和字符串時遇到了一些麻煩。爲了簡單起見,我放棄了字符串,只在類別空間的中心顯示標誌。 – Buffalo 2013-05-03 12:02:06

回答

2

是有...

形狀位置的計算有點襯衫袖子,但我希望你的問題沒問題。如果你想要具有圖例的形狀,你需要使用

Shape s = xyRend.getLegendItem(0, 0).getShape(); 

並調整y位置。

import java.awt.*; 
import java.awt.geom.*; 
import java.util.*; 
import java.util.List; 

import org.jfree.chart.*; 
import org.jfree.chart.axis.*; 
import org.jfree.chart.plot.*; 
import org.jfree.chart.renderer.xy.XYItemRenderer; 
import org.jfree.data.time.*; 
import org.jfree.ui.*; 

public class CategoryLabelWithShape extends ApplicationFrame { 

    public CategoryLabelWithShape(final String title) { 
     super(title); 
     final JFreeChart chart = constructChart(); 
     final ChartPanel panel = new ChartPanel(chart); 
     setContentPane(panel); 
    } 

    JFreeChart constructChart() { 
     JFreeChart chart = ChartFactory.createXYBarChart(
      "State Executions","Year",true,"Number of people",getDataSet(),PlotOrientation.VERTICAL,true,true,false 
     ); 

     XYPlot xyPlot = (XYPlot)chart.getPlot(); 
     final XYItemRenderer xyRend = xyPlot.getRenderer(); 

     DateAxis daNew = new DateAxis(){ 
      /** 
      * Draws the axis line, tick marks and tick mark labels. 
      * 
      * @param g2 the graphics device. 
      * @param cursor the cursor. 
      * @param plotArea the plot area. 
      * @param dataArea the data area. 
      * @param edge the edge that the axis is aligned with. 
      * 
      * @return The width or height used to draw the axis. 
      */ 
      protected AxisState drawTickMarksAndLabels(Graphics2D g2, 
        double cursor, Rectangle2D plotArea, Rectangle2D dataArea, 
        RectangleEdge edge) { 

       if (isTickLabelsVisible()) { 
        Shape s = xyRend.getBaseShape(); 
        Paint oldPaint = g2.getPaint(); 
        g2.setPaint(xyRend.getSeriesPaint(0)); 
        Rectangle2D rect = s.getBounds2D(); 
        AffineTransform oldTrans = g2.getTransform(); 

        AxisState state = new AxisState(cursor); 
        @SuppressWarnings("unchecked") 
        List<ValueTick> ticks = (List<ValueTick>)refreshTicks(g2, state, dataArea, edge); 
        for (ValueTick tick : ticks) { 
         float[] anchorPoint = calculateAnchorPoint(tick, cursor, dataArea, edge); 
         g2.setTransform(oldTrans); 
         // TODO: replace "anchorPoint[0]-X" with a bounds based value 
         g2.translate(anchorPoint[0]-17, anchorPoint[1]+rect.getHeight()); 
         g2.fill(s); 
        } 


        g2.setPaint(oldPaint); 
        g2.setTransform(oldTrans); 
       } 

       return super.drawTickMarksAndLabels(g2,cursor,plotArea,dataArea,edge); 
      } 
     }; 

     daNew.setLabel(xyPlot.getDomainAxis().getLabel()); 
     xyPlot.setDomainAxis(daNew); 

     return chart; 
    } 

    private TimeSeriesCollection getDataSet() { 
     TimeSeriesCollection ds = new TimeSeriesCollection(); 

     final TimeSeries s1 = new TimeSeries("Executions"); 
     ds.addSeries(s1); 

     Calendar cal = Calendar.getInstance(); 
     cal.clear(); 
     cal.set(Calendar.YEAR, 1970); 
     Random rand = new Random(); 
     for (int i=0; i<(2010-1970); i++) { 
      s1.add(new Year(cal.getTime()), rand.nextInt(100)); 
      cal.add(Calendar.YEAR, 1); 
     } 

     return ds; 
    } 


    public static void main(String[] args) { 
     CategoryLabelWithShape demo = new CategoryLabelWithShape(
       "Category-label with a shape"); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 
    } 
} 

更新:新版本類別的數據集... 它的稀有性纔是最重要;)

import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.awt.geom.Rectangle2D; 
import java.io.IOException; 
import java.net.URL; 
import java.util.List; 
import javax.imageio.ImageIO; 
import org.jfree.chart.*; 
import org.jfree.chart.axis.*; 
import org.jfree.chart.plot.*; 
import org.jfree.data.category.*; 
import org.jfree.ui.*; 

@SuppressWarnings("serial") 
public class CategoryLabelWithShape extends ApplicationFrame { 

    public CategoryLabelWithShape(final String title) { 
     super(title); 
     final JFreeChart chart = constructChart(); 
     final ChartPanel panel = new ChartPanel(chart); 
     setContentPane(panel); 
    } 

    JFreeChart constructChart() { 
     JFreeChart chart = ChartFactory.createBarChart(
      "MEP seats","country","# seats",getDataSet(),PlotOrientation.VERTICAL,true,true,false 
     ); 

     CategoryPlot cp = chart.getCategoryPlot(); 

     CategoryAxis daNew = new CategoryAxis(){ 
      protected AxisState drawCategoryLabels(Graphics2D g2, 
        Rectangle2D plotArea, 
        Rectangle2D dataArea, 
        RectangleEdge edge, 
        AxisState state, 
        PlotRenderingInfo plotState) { 

       if (isTickLabelsVisible()) { 
        List<CategoryTick> ticks = (List<CategoryTick>)refreshTicks(g2, state, plotArea, edge); 

        int categoryIndex = 0; 
        for (CategoryTick tick : ticks) { 
         double x0 = getCategoryStart(categoryIndex, ticks.size(), dataArea, edge); 
         double y0 = state.getCursor() + getCategoryLabelPositionOffset(); 


         CountryFlag cf = (CountryFlag)((CategoryPlot)getPlot()).getDataset().getColumnKey(categoryIndex); 
         try { 
          BufferedImage img = ImageIO.read(new URL(cf.flagUrl)); 

          g2.drawImage(img, (int)(x0-img.getWidth()), (int)(y0), img.getWidth(), img.getHeight(), Color.black, null); 
         } catch (IOException e) { 
          // skip flag 
         } 

         categoryIndex++; 
        } 
       }    
       return super.drawCategoryLabels(g2, plotArea, dataArea, edge, state, plotState); 
      } 
     }; 

     daNew.setLabel(cp.getDomainAxis().getLabel()); 
     cp.setDomainAxis(daNew); 

     return chart; 
    } 

    static class CountryFlag implements Comparable<CountryFlag> { 
     String name, flagUrl; 
     CountryFlag(String name, String flagUrl) { 
      this.name = name; 
      this.flagUrl = flagUrl; 
     } 
     public int compareTo(CountryFlag o) { 
      return name.compareTo(o.name); 
     } 
     public String toString() { 
      return name; 
     } 
    } 

    private CategoryDataset getDataSet() { 
     DefaultCategoryDataset ds = new DefaultCategoryDataset(); 
     ds.addValue(99, "MEP seats", new CountryFlag("Germany","http://upload.wikimedia.org/wikipedia/en/thumb/b/ba/Flag_of_Germany.svg/22px-Flag_of_Germany.svg.png")); 
     ds.addValue(72, "MEP seats", new CountryFlag("France","http://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/22px-Flag_of_France.svg.png")); 
     ds.addValue(72, "MEP seats", new CountryFlag("Italy","http://upload.wikimedia.org/wikipedia/en/thumb/0/03/Flag_of_Italy.svg/22px-Flag_of_Italy.svg.png")); 
     ds.addValue(72, "MEP seats", new CountryFlag("United Kingdom","http://upload.wikimedia.org/wikipedia/en/thumb/a/ae/Flag_of_the_United_Kingdom.svg/22px-Flag_of_the_United_Kingdom.svg.png")); 
     ds.addValue(50, "MEP seats", new CountryFlag("Spain","http://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/22px-Flag_of_Spain.svg.png")); 
     ds.addValue(50, "MEP seats", new CountryFlag("Poland","http://upload.wikimedia.org/wikipedia/en/thumb/1/12/Flag_of_Poland.svg/22px-Flag_of_Poland.svg.png")); 
     ds.addValue(33, "MEP seats", new CountryFlag("Romania","http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Flag_of_Romania.svg/22px-Flag_of_Romania.svg.png")); 
     ds.addValue(25, "MEP seats", new CountryFlag("Netherlands","http://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Flag_of_the_Netherlands.svg/22px-Flag_of_the_Netherlands.svg.png")); 
     ds.addValue(22, "MEP seats", new CountryFlag("Belgium","http://upload.wikimedia.org/wikipedia/commons/thumb/9/92/Flag_of_Belgium_%28civil%29.svg/22px-Flag_of_Belgium_%28civil%29.svg.png")); 
     ds.addValue(22, "MEP seats", new CountryFlag("Czech Republic","http://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Flag_of_the_Czech_Republic.svg/22px-Flag_of_the_Czech_Republic.svg.png")); 
     return ds; 
    } 


    public static void main(String[] args) { 
     CategoryLabelWithShape demo = new CategoryLabelWithShape(
       "Category-label with a shape"); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 
    } 
} 
+0

我只需要使用隨機圖像,而不是圖例。我會研究你的代碼。謝謝! – Buffalo 2013-05-02 06:01:57

+0

我想這會是某種pol。派對的標誌......今天晚上我會看看它。請說明圖像和年份如何記錄,以及是否重要,因爲圖表寬度限制會跳過一年 – kiwiwings 2013-05-02 06:54:03

+0

警方標誌?這僅僅是我從JFreeChart網站下載的一個演示截圖,我的圖表只是一個帶X軸國家字符串的條形圖,接下來我要添加國家的標誌,每個值下面都有不同的圖像。 – Buffalo 2013-05-02 07:07:59