JFreeChart Decoration

I would like to decorate all my created JFreeCharts with a time stamp in the corner. Is there a way within JFreeChart to draw an image after creating a chart?

EDIT: note that these charts are generated in the background thread and distributed through the servlet, so there is no graphical interface, and I cannot just display the timestamp on a separate component.

+3
source share
5 answers

After I tricked him again, I found a solution that allows me to draw randomly on the image after JFreeChart is done with it, so I will post it here for posterity. In my case, I wrote a chart for OutputStream, so my code looked something like this:

BufferedImage chartImage = chart.createBufferedImage(width, height, null);
Graphics2D g = (Graphics2D) chartImage.getGraphics();
/* arbitrary drawing happens here */
EncoderUtil.writeBufferedImage(chartImage, ImageFormat.PNG, outputStream);
+1
source

One approach is to subclass ChartPanel and override the method paint(Graphics)for the first chain before super.paint(Graphics)and then displaying additional text on top of the chart.

This annoys me a bit, and I personally would just like to add ChartPanelto another container JPanelalong with a JLabelrepresenting timestamp.

+4
source

:

http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=27939

ImageIcon :

ImageIcon icon = new ImageIcon(new URL(watermarkUrl));
Image image = icon.getImage();
chart.setBackgroundImage(image);
chart.setBackgroundImageAlignment(Align.CENTER);
chart.getPlot().setBackgroundAlpha(0.2f);
+1

In my experience, the best way to add user information to JFreeChart is to: - create an instance of BufferedImage of the same chart type; - draw a BufferedImage; - add the image to the current chart using XYImageAnnotation.

This is the guide for the code:

// retrieve image type and create another BufferedImage
int imgType = chart.createBufferedImage(1,1).getType();
BufferedImage bimg = new BufferedImage(width, height, bimg.getType);

// here you can draw inside the image ( relative x & y  )
Graphics2D g2 = (Graphics2D) bimg.getGraphics();
g2.drawString("Hello, JFreeChart " + timestamp, posX, posY );

// instantiate the image annotation, then add to the plot
XYImageAnnotation a = new XYImageAnnotation( x, y, bimg, RectangleAnchor.LEFT );
chart.getPlot().addAnnotation( a );
0
source

Source: https://habr.com/ru/post/1775236/


All Articles