装饰 JFreeChart
我想用角落里的时间戳来装饰我生成的所有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一种方法是子类化 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 tosuper.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 containerJPanel
along with aJLabel
representing the timestamp.经过一番研究后,我找到了一个解决方案,可以让您在 JFreeChart 完成后在图像上任意绘图,因此我将其发布在这里以供后代使用。就我而言,我将图表写入 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:
请在此处查看此论坛帖子:
http://www. jfree.org/phpBB2/viewtopic.php?f=3&t=27939
使用 ImageIcon 作为水印:
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:
org.jfree.chart.JFreeChart
可能是一个合适的替代方案。The
addSubtitle()
method oforg.jfree.chart.JFreeChart
may be a suitable alternative.根据我的经验,向 JFreeChart 添加自定义信息的最佳方法是:
- 实例化一个与图表相同类型的BufferedImage;
- 在 BufferedImage 上绘制;
- 使用 XYImageAnnotation 将图像添加到当前绘图。
这是代码指南:
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: