jfree chart draw image on a category plot

2019-09-16 12:52发布

问题:

Heyy folks I have a pretty strange requirement.I need to draw an arrow shaped image beside a stacked bar chart in a category plot .The arrow image has to be red or green depending on some pre defined condition.I just need help to find a method to draw an image outside the stacked bar on the plot using jfree chart.Please help me out with this guys .Being stuck with this for days !!!!

回答1:

You could implement the CategoryAnnotation interface:

plot.addAnnotation(new CategoryAnnotation(){

   Image anImage = ImageIO.read(new File("anImage.jpg"));

   @Override
   public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea, CategoryAxis domainAxis, ValueAxis rangeAxis){

       int x = ...  // determine where you want to draw the image inside the dataArea rectangle
       int y = ...

       g2.drawImage(anImage, x, y, null);

   }
});


回答2:

Do you need an arrow, an image or an image cropped to an arrow? In the first case, I would look at CategoryPointerAnnotation. In the second case, look at the source of XYImageAnnotation and wrap the logic in a custom CategoryAnnotation. An XYImageAnnotation itself won´t work, if your plot is indeed a CategoryPlot.