装饰 JFreeChart

发布于 2024-10-02 14:09:24 字数 149 浏览 7 评论 0原文

我想用角落里的时间戳来装饰我生成的所有 JFreeCharts。 JFreeChart 框架内是否有一种方法可以在生成图表后在图像上进行绘制?

编辑:请注意,这些图表是在后台线程中生成并通过 servlet 分发的,因此没有 GUI,我不能只在单独的组件上显示时间戳。

I would like to decorate all my generated JFreeCharts with a timestamp in the corner. Is there a way within the JFreeChart framework to draw on the image after the chart has been generated?

EDIT: Note that these charts are being generated in a background thread and distributed via a servlet, so there is no GUI and I can't just display the timestamp on a separate component.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月如刀 2024-10-09 14:09:24

一种方法是子类化 ChartPanel 并重写 paint(Graphics) 方法,首先链接到 super.paint(Graphics),然后在图表顶部渲染附加文本。

不过,这对我来说有点老套,我个人更喜欢简单地将 ChartPanel 添加到另一个容器 JPanel 以及表示时间戳的 JLabel

One approach would be to subclass ChartPanel and override the paint(Graphics) method to first chain to super.paint(Graphics) and subsequently render the additional text on top of the chart.

This feels a bit hacky to me though and I'd personally favour simply adding the ChartPanel to another container JPanel along with a JLabel representing the timestamp.

携君以终年 2024-10-09 14:09:24

经过一番研究后,我找到了一个解决方案,可以让您在 JFreeChart 完成后在图像上任意绘图,因此我将其发布在这里以供后代使用。就我而言,我将图表写入 OutputStream,因此我的代码如下所示:

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

After dinking around with it some more, I found a solution that lets you draw arbitrarily on the image after JFreeChart is done with it, so I'll post it here for posterity. In my case, I was writing the chart to an 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);
信仰 2024-10-09 14:09:24

请在此处查看此论坛帖子:

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);

Take a look at this forum post here:

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

That uses an ImageIcon as a watermark:

ImageIcon icon = new ImageIcon(new URL(watermarkUrl));
Image image = icon.getImage();
chart.setBackgroundImage(image);
chart.setBackgroundImageAlignment(Align.CENTER);
chart.getPlot().setBackgroundAlpha(0.2f);
风和你 2024-10-09 14:09:24

org.jfree.chart.JFreeChart 可能是一个合适的替代方案。

The addSubtitle() method of org.jfree.chart.JFreeChart may be a suitable alternative.

人间☆小暴躁 2024-10-09 14:09:24

根据我的经验,向 JFreeChart 添加自定义信息的最佳方法是:
- 实例化一个与图表相同类型的BufferedImage;
- 在 BufferedImage 上绘制;
- 使用 XYImageAnnotation 将图像添加到当前绘图。

这是代码指南:

// 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 );

In my Experience the best way to add custom information to JFreeChart is:
- Instantiate a BufferedImage of the same type of the chart;
- draw on the BufferedImage ;
- add the image to the current Plot, by using an XYImageAnnotation.

This is a guideline 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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文